Am I being Stupid?!

M

Mr John FO Evans

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
 
E

Eric Sosman

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.)
 
F

Fred Kleinschmidt

Mr John FO Evans said:
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
 
K

Knemon

Mr said:
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.
 
M

Mr John FO Evans

Knemon <[email protected]> said:
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
 
C

CBFalconer

*** Fixed rude top-posting ***
mospehraict said:
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.
 
S

Spiros Bousbouras

mospehraict said:
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>
 
A

Andrew Poelstra

mospehraict said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top