Solved

fscanf

Posted on 1998-12-14
9
322 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ vs C compilers 13 157
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 60
Finding a good hash function 4 120
How to create frequencies of a variable from SAS dataset? 10 121
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion in the C programming language.

911 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

24 Experts available now in Live!

Get 1:1 Help Now