Solved

fscanf

Posted on 1998-12-14
9
332 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:qvicksilver
9 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 1255121
You should probably delete this question. It's the same as the other one.....

0
 

Expert Comment

by:toannds
ID: 1255122
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;
      }
0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1255123
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;
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
ATucker earned 200 total points
ID: 1255124
int main()
{
    FILE *fp = fopen("float.dat", "r");

    while ( !feof(fp) )
    {
        float flt;
        if ( fscanf(fp, "%f\n", &flt) == 1 )
            printf("%f\n", flt);
    }

       return 0;
}

0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1255125
thanks for quoting toannds and me!
that's a pretty low way to swipe points for an answer that is not really yours!
0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1255126
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!
0
 
LVL 1

Expert Comment

by:ATucker
ID: 1255127
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?
0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1255128
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.
0
 
LVL 1

Expert Comment

by:ATucker
ID: 1255129
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?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question