reading data from a file

Discussion in 'C Programming' started by jj_76, Aug 3, 2006.

  1. jj_76

    jj_76 Guest

    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);
    }
    -----------------------
     
    jj_76, Aug 3, 2006
    #1
    1. Advertising

  2. jj_76

    Tom St Denis Guest

    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
     
    Tom St Denis, Aug 3, 2006
    #2
    1. Advertising

  3. 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)
     
    Richard Heathfield, Aug 3, 2006
    #3
  4. jj_76

    Default User Guest

    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
     
    Default User, Aug 3, 2006
    #4
  5. jj_76

    Ronald Bruck Guest

    In article <>,
    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);
    > }


    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
     
    Ronald Bruck, Aug 3, 2006
    #5
  6. "jj_76" <> 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) <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.
     
    Keith Thompson, Aug 3, 2006
    #6
  7. jj_76

    Old Wolf Guest

    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.
     
    Old Wolf, Aug 4, 2006
    #7
  8. jj_76

    jj_76 Guest

    > > 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 ?
     
    jj_76, Aug 4, 2006
    #8
  9. 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)
     
    Richard Heathfield, Aug 4, 2006
    #9
  10. jj_76

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
     
    CBFalconer, Aug 5, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    689
    Kevin Spencer
    Nov 11, 2004
  2. Replies:
    0
    Views:
    799
  3. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    383
    Hrvoje Niksic
    May 25, 2007
  4. Stephen Moon

    reading output file data as input data

    Stephen Moon, Feb 28, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    183
    Tad McClellan
    Feb 29, 2004
  5. Replies:
    5
    Views:
    95
    Chris Angelico
    May 14, 2014
Loading...

Share This Page