Solved

fscanf

Posted on 1998-12-14
9
321 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now