Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   reading data from a file (http://www.velocityreviews.com/forums/t443836-reading-data-from-a-file.html)

jj_76 08-03-2006 07:51 PM

reading data from a file
 
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);
}
-----------------------


Tom St Denis 08-03-2006 07:56 PM

Re: reading data from a file
 
jj_76 wrote:
> --------------------------------
> #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


Richard Heathfield 08-03-2006 08:34 PM

Re: reading data from a file
 
Tom St Denis said:

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

>
> Wrong.


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.

<snip>

>> while(!feof(fp1))
>> {
>> fscanf(fp1,"%f",&mtot);

>
> 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Default User 08-03-2006 08:35 PM

Re: reading data from a file
 
jj_76 wrote:

> 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

Ronald Bruck 08-03-2006 09:15 PM

Re: reading data from a file
 
In article <1154634673.670052.304310@m73g2000cwd.googlegroups .com>,
jj_76 <kravi76@yahoo.com> wrote:

> 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".

--
Ron Bruck

Keith Thompson 08-03-2006 10:22 PM

Re: reading data from a file
 
"jj_76" <kravi76@yahoo.com> writes:
> 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>


You don't use anything from <math.h>.

> main()


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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Old Wolf 08-03-2006 11:19 PM

Re: reading data from a file
 
Keith Thompson wrote:
>
> 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.


jj_76 08-03-2006 11:28 PM

Re: reading data from a file
 
> > 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.


-----------------------------------
pardon my ignorance, but what is the alternative ?


Richard Heathfield 08-04-2006 12:47 AM

Re: reading data from a file
 
jj_76 said:

>> > 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.

>
> -----------------------------------
> pardon my ignorance, but what is the alternative ?


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

CBFalconer 08-05-2006 12:31 AM

Re: reading data from a file
 
jj_76 wrote:
>
>>> 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.

>
> pardon my ignorance, but what is the alternative ?


Please don't delete attribution lines for quoted material.

One alternative are the routines I have published in:

<http://cbfalconer.home.att.net/download/txtio.zip>

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE maineline address!



All times are GMT. The time now is 04:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.