Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

fscanf

Posted on 1998-12-14
9
Medium Priority
?
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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 600 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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

704 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