reading data from a file

J

jj_76

Hello,
I have the following code and I try to read data from a file. But all I
get are zero's. Any help is appreciated.
--------------------------------
#include <stdio.h>
#include <math.h>
main()
{
double mtot;
FILE *fp1;
FILE *fp2;

fp1=fopen("initial_birth","r");

while(!feof(fp1))
{
fscanf(fp1,"%f",&mtot);
printf("%f\n",mtot);
}
fclose(fp1);
fclose(fp2);
}
-----------------------
 
T

Tom St Denis

jj_76 said:
--------------------------------
#include <stdio.h>
#include <math.h>
main()
Wrong.

double mtot;
FILE *fp1;
FILE *fp2;

fp1=fopen("initial_birth","r");

Check the return value.
while(!feof(fp1))
{
fscanf(fp1,"%f",&mtot);

Check the return value, also how are you handling whitespace?
fclose(fp2);

You never opened fp2, this is wrong.

I hate *scanf functions. They're basically begging for beginners to
abuse them. arrg...

Tom
 
R

Richard Heathfield

Tom St Denis said:

No, it's not wrong. It's valid K&R C, and it's valid C90. It's not good
style, and it's not terribly wise, but it's not actually wrong.

Check the return value, also how are you handling whitespace?

He's also misusing feof.
I hate *scanf functions. They're basically begging for beginners to
abuse them. arrg...

Yes, it is a source of wonder to me that C teachers (and C books) introduce
such a difficult function so early.
 
D

Default User

jj_76 said:
Hello,
I have the following code and I try to read data from a file. But all
I get are zero's. Any help is appreciated.
--------------------------------
#include <stdio.h>
#include <math.h>
main()
{
double mtot;
FILE *fp1;
FILE *fp2;

fp1=fopen("initial_birth","r");

while(!feof(fp1))
{
fscanf(fp1,"%f",&mtot);
printf("%f\n",mtot);
}
fclose(fp1);
fclose(fp2);
}


This may cover the problem:

<http://c-faq.com/stdio/scanf2.html>




Brian
 
R

Ronald Bruck

jj_76 said:
Hello,
I have the following code and I try to read data from a file. But all I
get are zero's. Any help is appreciated.
--------------------------------
#include <stdio.h>
#include <math.h>
main()
{
double mtot;
FILE *fp1;
FILE *fp2;

fp1=fopen("initial_birth","r");

while(!feof(fp1))
{
fscanf(fp1,"%f",&mtot);
printf("%f\n",mtot);
}
fclose(fp1);
fclose(fp2);
}

You're closing fp2 when you never opened it; but I understand what you
mean, you probably modified the original code to use printf instead of
fprintf, remembered to remove the "fopen" but forgot to remove the
"fclose". (The mind-reader is now OUT.)

You're trying to read a double value (mtot) using a float format
specifier; use "%lf" instead of "%f".
 
K

Keith Thompson

jj_76 said:
I have the following code and I try to read data from a file. But all I
get are zero's. Any help is appreciated.

You don't use anything from said:

int main(void)
{
double mtot;
FILE *fp1;
FILE *fp2;

fp1=fopen("initial_birth","r");

Always check the result of fopen().
while(!feof(fp1))

Don't use feof(); it doesn't do what you think it does.
{
fscanf(fp1,"%f",&mtot);

Always check the result of fscanf(). Also, you're using the wrong
format.
printf("%f\n",mtot);
}
fclose(fp1);
fclose(fp2);

You never opened fp2.

Add "return 0;" here.

Read section 12 of the comp.lang.c FAQ, <http://c-faq.com/>. Then
read the other sections.

As a matter of style, I always put a space after each comma, and I
usually put spaces around operators. Also, a consistent indentation
style is important. Ignoring the actual errors, here's how I would
have formatted your code:

#include <stdio.h>
#include <math.h>
main()
{
double mtot;
FILE *fp1;
FILE *fp2;

fp1 = fopen("initial_birth", "r");

while (! feof(fp1)) {
fscanf(fp1, "%f", &mtot);
printf("%f\n", mtot);
}

fclose(fp1);
fclose(fp2);
}

This is still wrong, but it's easier to read.

If you prefer to put your '{' characters by themselves on the
beginning of the next line, that's ok too.
 
O

Old Wolf

Keith said:
As a matter of style, I always put a space after each comma, and I
usually put spaces around operators. Also, a consistent indentation
style is important. Ignoring the actual errors, here's how I would
have formatted your code:

while (! feof(fp1)) {

For me, the ! gets lost in the ( with this style; I prefer

while ( !feof(fp1) )

But I use a syntax-highlighting editor so the ! now doesn't
get lost in the feof. My colleague has a macro which is
very readable:

while ( NOT feof(fp1) )

and doesn't take long to learn if you haven't seen it before.

NB. This is all subjective opinion of course.
 
R

Richard Heathfield

jj_76 said:

Read the data as a string using fgets (or whatever), and convert it yourself
if you need to after you've got it into memory, using strtol or strtoul or
strtod or whatever.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top