Link to home
Start Free TrialLog in
Avatar of qvicksilver
qvicksilver

asked on

fscanf

Could someone please show me how a routine for reading float values from a file using fscanf. The values are separated by newline characters.
 
I need a routine to solve this problem really fast, all help is greatly apreciated.
Avatar of imladris
imladris
Flag of Canada image

You should probably delete this question. It's the same as the other one.....

Avatar of toannds
toannds

Hi,

Maybe I misunderstand your question, but do you need this code?

#include <stdio.h>

main()
      {
            FILE *in;
            float f;

            if ( (in = fopen("DATA.TXT", "rt")) == NULL ){
                  fprintf( stderr, "Cannot open input file.\n");
                  return 1;
            }

            while (!feof(in)){
                  if ( fscanf( in, "%f", &f )) printf( "The float read was: %f\n", f );
            }
            fclose(in);
            return 0;
      }
slight modification:

int main(void)
{
FILE *infile;
float f;

if ( (infile = fopen("DATA.TXT", "rt")) == NULL ){
fprintf( stderr, "Cannot open input file.\n");
return -11;
}

while (!feof(infile)){
if ( fscanf( infile, "%f\n", &f )) printf( "The float read was: %f\n", f );
}
fclose(in);
return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of ATucker
ATucker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for quoting toannds and me!
that's a pretty low way to swipe points for an answer that is not really yours!
why did you accept his answer? toannds was supposed to get the points and ATucker misbehaved a lot by plagiarising him/her.

ATucker: that's so low, but if you need the points that much! Fortunately not all experts here are like you!
Didn't mean to offend anyone, MadDuck.  I just noticed that there were only comments and no answers yet, so I answered it as best I could.  Sorry if you feel like I stole your points.

If you felt your response was a solution, why didn't you propose it as an answer instead of a comment?
I usually add comments first to keep the question open. If the poster feels that I answered it then I will answer it later. This is the best way I think.

Anyway, I accept your apology, although it should go out to toannds who really answered it. I just corrected his/her response.
Didn't mean to offend anyone, MadDuck.  I just noticed that there were only comments and no answers yet, so I answered it as best I could.  Sorry if you feel like I stole your points.

If you felt your response was a solution, why didn't you propose it as an answer instead of a comment?