[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Am I being Stupid?!

Mr John FO Evans

5/21/2016 3:25:00 AM

The following code snippet fails on my system

double dd;
FILE * inf;
inf=fopen(<filename>,"r");
fscanf(inf,"%g",&dd);

The text file opened is as follows:-

123.6
etc

The file opens correctly but the value of dd returned is nonsense!!

If this should work I must look elsewhere for the solution!

NB If I change this to reading integer values (from a file of integers) all is well!!

John



--
_ _________________________________________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_> / 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinte...


9 Answers

Eric Sosman

8/29/2006 9:52:00 PM

0



Mr John FO Evans wrote On 08/29/06 17:14,:
> The following code snippet fails on my system
>
> double dd;
> FILE * inf;
> inf=fopen(<filename>,"r");
> fscanf(inf,"%g",&dd);
>
> The text file opened is as follows:-
>
> 123.6
> etc
>
> The file opens correctly but the value of dd returned is nonsense!!

Change "%g" to "%lg" or change `double dd' to `float dd'.
(Make one change or the other, but not both.)

--
Eric.Sosman@sun.com

Fred Kleinschmidt

8/29/2006 10:21:00 PM

0


"Mr John FO Evans" <mijas@orpheusmail.co.uk> wrote in message
news:na.bd2d724e5e.a903c0mijas@orpheusmail.co.uk...
> The following code snippet fails on my system
>
> double dd;
> FILE * inf;
> inf=fopen(<filename>,"r");
> fscanf(inf,"%g",&dd);

%g is for float. Use %lg for double

>
> The text file opened is as follows:-
>
> 123.6
> etc
>
> The file opens correctly but the value of dd returned is nonsense!!
>
> If this should work I must look elsewhere for the solution!
>
> NB If I change this to reading integer values (from a file of integers)
> all is well!!
>
> John

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


Knemon

8/29/2006 11:13:00 PM

0

Mr John FO Evans wrote:
> The following code snippet fails on my system
>
> double dd;
> FILE * inf;
> inf=fopen(<filename>,"r");
> fscanf(inf,"%g",&dd);
>
> The text file opened is as follows:-
>
> 123.6
> etc
>
> The file opens correctly but the value of dd returned is nonsense!!

The correct specifier for a double in the fscanf specification string is
%lg, not %g.

This time you lucked out. Your uncompilable snippet with no main, no
#include <stdio.h>, and the illegal (and undeclared and uninitialized)
<filename> identifier could easily have been worthless to anyone trying
to help you. The fact that you accidentally included at least one of
your errors was just dumb luck. You had no idea what was wrong, so
failing to post a minimal compilable program showing the problem
behavior is just trying not to get a coherent answer.

mospehraict

8/30/2006 4:17:00 PM

0

what's the problem to write up main(){} and include stdio manually?

Knemon wrote:
> Mr John FO Evans wrote:
> > The following code snippet fails on my system
> >
> > double dd;
> > FILE * inf;
> > inf=fopen(<filename>,"r");
> > fscanf(inf,"%g",&dd);
> >
> > The text file opened is as follows:-
> >
> > 123.6
> > etc
> >
> > The file opens correctly but the value of dd returned is nonsense!!
>
> The correct specifier for a double in the fscanf specification string is
> %lg, not %g.
>
> This time you lucked out. Your uncompilable snippet with no main, no
> #include <stdio.h>, and the illegal (and undeclared and uninitialized)
> <filename> identifier could easily have been worthless to anyone trying
> to help you. The fact that you accidentally included at least one of
> your errors was just dumb luck. You had no idea what was wrong, so
> failing to post a minimal compilable program showing the problem
> behavior is just trying not to get a coherent answer.

CBFalconer

8/30/2006 8:38:00 PM

0

*** Fixed rude top-posting ***
mospehraict wrote:
> Knemon wrote:
>> Mr John FO Evans wrote:
>
>>> The following code snippet fails on my system
>>>
>>> double dd;
>>> FILE * inf;
>>> inf=fopen(<filename>,"r");
>>> fscanf(inf,"%g",&dd);
>>>
>>> The text file opened is as follows:-
>>>
>>> 123.6
>>> etc
>>>
>>> The file opens correctly but the value of dd returned is nonsense!!
>>
>> The correct specifier for a double in the fscanf specification
>> string is %lg, not %g.
>>
>> This time you lucked out. Your uncompilable snippet with no main,
>> no #include <stdio.h>, and the illegal (and undeclared and
>> uninitialized) <filename> identifier could easily have been
>> worthless to anyone trying to help you. The fact that you
>> accidentally included at least one of your errors was just dumb
>> luck. You had no idea what was wrong, so failing to post a
>> minimal compilable program showing the problem behavior is just
>> trying not to get a coherent answer.
>
> what's the problem to write up main(){} and include stdio manually?

Don't top-post. Your answer belongs after, or intermixed with, the
material to which you reply, after snipping anything not germane to
that reply.

The problem is that the OP didn't do it. Postings should be
complete and understandable.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.a... USE maineline address!


Spiros Bousbouras

8/30/2006 9:00:00 PM

0

mospehraict wrote:

> what's the problem to write up main(){} and include stdio manually?

If you mean problem for those trying to help the
opening poster then modifying the posted code
may alter the nature of the problem or make it
disappear. Plus why should they have to type
extra stuff ? Noone's getting paid to do this so
it's the obligation of the person who asks for help
to make things easier.

<Context is provided by mospehraict's top posted>
<quotes which follow below.>

>
> Knemon wrote:
> > Mr John FO Evans wrote:
> > > The following code snippet fails on my system
> > >
> > > double dd;
> > > FILE * inf;
> > > inf=fopen(<filename>,"r");
> > > fscanf(inf,"%g",&dd);
> > >
> > > The text file opened is as follows:-
> > >
> > > 123.6
> > > etc
> > >
> > > The file opens correctly but the value of dd returned is nonsense!!
> >
> > The correct specifier for a double in the fscanf specification string is
> > %lg, not %g.
> >
> > This time you lucked out. Your uncompilable snippet with no main, no
> > #include <stdio.h>, and the illegal (and undeclared and uninitialized)
> > <filename> identifier could easily have been worthless to anyone trying
> > to help you. The fact that you accidentally included at least one of
> > your errors was just dumb luck. You had no idea what was wrong, so
> > failing to post a minimal compilable program showing the problem
> > behavior is just trying not to get a coherent answer.

mospehraict

8/30/2006 10:44:00 PM

0

> The problem is that the OP didn't do it. Postings should be
> complete and understandable.

ok, what's not understandable in the related post?

Andrew Poelstra

8/30/2006 11:28:00 PM

0

"mospehraict" <mospehraict@gmail.com> writes:

>> The problem is that the OP didn't do it. Postings should be
>> complete and understandable.
>
> ok, what's not understandable in the related post?
>

Nothing's understandable in this post: What do you mean by
"related post"? All I see is this one.

--
Andrew Poelstra <http://www.wpsoftware.net/pr...
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.

Mr John FO Evans

5/21/2016 3:27:00 AM

0

In article <4ljvvnF2ajv4U1@individual.net>, Knemon <grouch@dyskolos.org>
wrote:
>
> Mr John FO Evans wrote:
> > The following code snippet fails on my system
> >
> > double dd;
> > FILE * inf;
> > inf=fopen(<filename>,"r");
> > fscanf(inf,"%g",&dd);
> >
> > The text file opened is as follows:-
> >
> > 123.6
> > etc
> >
> > The file opens correctly but the value of dd returned is nonsense!!
>
> The correct specifier for a double in the fscanf specification string is
> %lg, not %g.
>
> This time you lucked out. Your uncompilable snippet with no main, no
> #include <stdio.h>, and the illegal (and undeclared and uninitialized)
> <filename> identifier could easily have been worthless to anyone trying
> to help you. The fact that you accidentally included at least one of
> your errors was just dumb luck. You had no idea what was wrong, so
> failing to post a minimal compilable program showing the problem
> behavior is just trying not to get a coherent answer.
>
Sorry!!

I thought I had given the vital information when saying that the file
opened correctly and that equivalent integer code was OK. Only the formatted
read was failing.

Anyway thank you everybody - none of my reference notes had the 'l'
information.

John


--
_ _________________________________________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_> / 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinte...