[URGENT] fgets reading last line in file twice

Discussion in 'C Programming' started by DJP, Oct 18, 2004.

  1. DJP

    DJP Guest

    Hi,

    I need to read a file programmatically until end of file. My logic is as
    follows:

    while(!feof(Fp))
    {

    fgets(readLine,10000,Fp);

    .
    .
    .
    do something
    .
    .
    .
    }


    However with this logic, depending on the file I am reading my program
    sometime reads the last line twice. Can anyone please tell me why this is
    happenning?

    This is kind of urgent so your speedy help will be greatly appreciated.
    Thank you!!
     
    DJP, Oct 18, 2004
    #1
    1. Advertising

  2. DJP wrote:

    > Hi,
    >
    > I need to read a file programmatically until end of file. My logic is as
    > follows:
    >
    > while(!feof(Fp))
    > {
    >
    > fgets(readLine,10000,Fp);
    >
    > .
    > .
    > .
    > do something
    > .
    > .
    > .
    > }
    >
    >
    > However with this logic, depending on the file I am reading my program
    > sometime reads the last line twice. Can anyone please tell me why this is
    > happenning?
    >
    > This is kind of urgent so your speedy help will be greatly appreciated.
    > Thank you!!
    >
    >

    feof() returns true AFTER you try to read past the end of file.
    So this code should work better:
    while(true) {
    fgets(readLine,10000,Fp);
    if( feof(Fp) ) break;
    ...
    }

    --
    Regards,
    Slava
     
    Vyacheslav Kononenko, Oct 18, 2004
    #2
    1. Advertising

  3. DJP wrote:
    > Hi,
    >
    > I need to read a file programmatically until end of file. My logic is as
    > follows:
    >
    > while(!feof(Fp))

    Here you are testing for EOF before the flag is set.
    The flag is set by a read action. See the FAQ below.

    > {
    >
    > fgets(readLine,10000,Fp);
    >
    > .
    > .
    > .
    > do something
    > .
    > .
    > .
    > }
    >
    >
    > However with this logic, depending on the file I am reading my program
    > sometime reads the last line twice. Can anyone please tell me why this is
    > happenning?
    >
    > This is kind of urgent so your speedy help will be greatly appreciated.
    > Thank you!!
    >
    >


    You'll get faster results by consulting the FAQ first.
    I removed all but the C and C++ language newsgroups.


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Oct 18, 2004
    #3
  4. "DJP" <> wrote in message
    news:cl0sc1$g7v$...
    > Hi,
    >
    > I need to read a file programmatically until end of file. My logic is as
    > follows:
    >
    > while(!feof(Fp))
    > {
    >
    > fgets(readLine,10000,Fp);
    >
    > .
    > .
    > .
    > do something
    > .
    > .
    > .
    > }


    Your logic is very common but wrong

    while (fgets(readLine,10000,Fp), !feof(fp))
    {
    do something
    }

    >
    > However with this logic, depending on the file I am reading my program
    > sometime reads the last line twice. Can anyone please tell me why this is
    > happenning?


    Because the return of feof only accurately reflects the status of the
    previous read, not the next one.

    It sometimes seems that every single newbie in the world gets this wrong, so
    you are not alone.

    john
     
    John Harrison, Oct 18, 2004
    #4
  5. "John Harrison" <> wrote in message
    news:...
    >
    > "DJP" <> wrote in message
    > news:cl0sc1$g7v$...
    >> Hi,
    >>
    >> I need to read a file programmatically until end of file. My logic is as
    >> follows:
    >>
    >> while(!feof(Fp))
    >> {
    >>
    >> fgets(readLine,10000,Fp);
    >>
    >> .
    >> .
    >> .
    >> do something
    >> .
    >> .
    >> .
    >> }

    >
    > Your logic is very common but wrong
    >
    > while (fgets(readLine,10000,Fp), !feof(fp))
    > {
    > do something
    > }
    >


    Actually I don't think that code is correct. My unfamiliarity with C.

    Try this

    while (fgets(readLine,10000,Fp))
    {
    }

    john
     
    John Harrison, Oct 18, 2004
    #5
  6. DJP

    Mike Wahler Guest

    "DJP" <> wrote in message
    news:cl0sc1$g7v$...
    > Hi,
    >
    > I need to read a file programmatically until end of file. My logic is as
    > follows:
    >
    > while(!feof(Fp))
    > {
    >
    > fgets(readLine,10000,Fp);
    > }
    >
    >
    > However with this logic, depending on the file I am reading my program
    > sometime reads the last line twice. Can anyone please tell me why this is
    > happenning?


    Hmm, we see this question very frequently here. That's why it's cited
    in comp.lang.c's Frequently Asked Questions document:

    http://www.eskimo.com/~scs/C-faq/q12.2.html

    Once you read that and fix your code, start here:
    http://www.eskimo.com/~scs/C-faq/top.html

    and read the whole thing. It will save you hours of frustration
    and wasted time.

    -Mike
     
    Mike Wahler, Oct 18, 2004
    #6
  7. DJP

    Rookie Guest


    >> I need to read a file programmatically until end of file. My logic is as
    >> follows:
    >>
    >> while(!feof(Fp))
    >> {
    >>
    >> fgets(readLine,10000,Fp);
    >> }
    >>
    >>


    Is this how I should fix it?

    while( fgets(readLine,10000,Fp)!=EOF)
    {
    //do something
    }
     
    Rookie, Oct 18, 2004
    #7
  8. DJP

    Artie Gold Guest

    Rookie wrote:
    >>>I need to read a file programmatically until end of file. My logic is as
    >>>follows:
    >>>
    >>>while(!feof(Fp))
    >>> {
    >>>
    >>> fgets(readLine,10000,Fp);
    >>>}
    >>>
    >>>

    >
    >
    > Is this how I should fix it?
    >
    > while( fgets(readLine,10000,Fp)!=EOF)
    > {
    > //do something
    > }
    >
    >

    No. Please read the FAQ entry that Mike mentioned.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    "If you don't think it matters, you're not paying attention."
     
    Artie Gold, Oct 18, 2004
    #8
  9. "Rookie" <> writes:
    >>> I need to read a file programmatically until end of file. My logic is as
    >>> follows:
    >>>
    >>> while(!feof(Fp))
    >>> {
    >>>
    >>> fgets(readLine,10000,Fp);
    >>> }
    >>>
    >>>

    >
    > Is this how I should fix it?
    >
    > while( fgets(readLine,10000,Fp)!=EOF)
    > {
    > //do something
    > }


    No.

    You should have some documentation somewhere on the fgets() function,
    either a book or some form of online documentation such as a man page.
    This documentation should tell you the type of the result returned by
    the fgets() function. Go look it up; I'll wait.

    ....

    Ok, now you know that fgets() returns a char* value. EOF is an int,
    not a char*. That alone should tell you that EOF is not the
    appropriate value to compare the result of fgets() to.

    What is the appropriate value? Go back to your documentation and find
    out what fgets() returns when there's no more input.

    The types and values returned by functions in the C standard library
    are often not obvious. Any time you write code that looks at the
    result of a standard function, you need to have a copy of the
    documentation next to you so you know what to expect, at least until
    you have it all memorized (which you probably never will; I certainly
    don't).

    (By the way, I'm not trying to be condescending; I had to look up the
    definition of fgets() myself before I wrote this.)

    --
    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, Oct 18, 2004
    #9
  10. >Is this how I should fix it?

    No.

    >while( fgets(readLine,10000,Fp)!=EOF)

    while (fgets(readLine, 10000, Fp) != NULL)
    >{
    > //do something
    >}


    Gordon L. Burditt
     
    Gordon Burditt, Oct 18, 2004
    #10
  11. DJP

    Mike Wahler Guest

    "Rookie" <> wrote in message
    news:cl0u8e$hn3$...
    >
    > >> I need to read a file programmatically until end of file. My logic is

    as
    > >> follows:
    > >>
    > >> while(!feof(Fp))
    > >> {
    > >>
    > >> fgets(readLine,10000,Fp);
    > >> }
    > >>
    > >>

    >
    > Is this how I should fix it?
    >
    > while( fgets(readLine,10000,Fp)!=EOF)
    > {
    > //do something
    > }


    I assumed that you'd read the FAQ I cited, but also
    that you'd read the documentation for 'fgets()'.
    If you already have, please do so again, more carefully.
    If you haven't, well ... :)

    Specifically, what is the return type of 'fgets()'?
    What value does it return in which cases?

    Read *everything* your documentation says about 'fgets()'.

    Computers are stupid morons. You must be *very* careful
    and explicit in what you tell them -- they take everything
    literally, without question.


    -Mike
     
    Mike Wahler, Oct 18, 2004
    #11
  12. DJP wrote:

    > Hi,
    >
    > I need to read a file programmatically until end of file. My logic is as
    > follows:


    Instead of

    > while(!feof(Fp))
    > {
    > fgets(readLine,10000,Fp);

    /* do something */
    > }


    use
    while(fgets(readLine,10000,Fp))
    {
    /* do something */
    }

    If this seems off-topic in your newsgroup I apologize to comp.lang.c++,
    comp.protocols.tcp-ip, comp.unix.programmer, and comp.unix.solaris, all
    of which DJP cross-posted. I have no idea what newsgroups he actually
    reads, and so respond to all his listed newsgroups. It is, however,
    clear that he has not bothered to check the FAQs on past traffic in any
    of these before posting. I hope he corrects his shotgun posting and
    corrects his failure to behave like a human being by checking the FAQs
    and past traffic before posting.
     
    Martin Ambuhl, Oct 18, 2004
    #12
  13. DJP

    CBFalconer Guest

    Gordon Burditt wrote: ***(and stripped attributions)***
    >
    >> Is this how I should fix it?
    >>
    >> while( fgets(readLine,10000,Fp)!=EOF)
    >>{
    >> //do something
    >>}

    >
    > No.
    >
    > while (fgets(readLine, 10000, Fp) != NULL)


    or, simpler, and reads better:

    while (fgets(readline, 10000, Fp)) {
    /* do something */
    }

    --
    "I support the Red Sox and any team that beats the Yankees"

    "Any baby snookums can be a Yankee fan, it takes real moral
    fiber to be a Red Sox fan"
     
    CBFalconer, Oct 18, 2004
    #13
  14. "DJP" <> writes:

    > Hi,
    >
    > I need to read a file programmatically until end of file. My logic is as
    > follows:
    >
    > while(!feof(Fp))


    FAQ. From the comp.lang.c FAQ list,
    http://www.eskimo.com/~scs/C-faq/top.html :

    12.2: Why does the code

    while(!feof(infp)) {
    fgets(buf, MAXLINE, infp);
    fputs(buf, outfp);
    }

    copy the last line twice?

    A: In C, end-of-file is only indicated *after* an input routine has
    tried to read, and failed. (In other words, C's I/O is not like
    Pascal's.) Usually, you should just check the return value of
    the input routine -- fgets(), for example, returns NULL on end-
    of-file. In virtually all cases, there's no need to use feof()
    at all.

    References: K&R2 Sec. 7.6 p. 164; ISO Sec. 7.9.3, Sec. 7.9.7.1,
    Sec. 7.9.10.2; H&S Sec. 15.14 p. 382.

    --
    Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
    Dept. of Physics, Univ. of Padova, Italy ROT13:
     
    Maurizio Loreti, Oct 19, 2004
    #14
  15. On Mon, 18 Oct 2004 09:52:11 -0700, in comp.lang.c , "DJP"
    <> wrote:

    >Hi,
    >
    >I need to read a file programmatically until end of file. My logic is as
    >follows:
    >
    >while(!feof(Fp))


    This is a FAQ. You can't use feof like that.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Oct 20, 2004
    #15
  16. John Harrison wrote:
    > "John Harrison" <> wrote in message
    > news:...
    >
    >>"DJP" <> wrote in message
    >>news:cl0sc1$g7v$...


    >>>I need to read a file programmatically until end of file.
    >>> My logic is as follows:


    >>>while(!feof(Fp))
    >>>{
    >>> fgets(readLine,10000,Fp);

    (snip)
    >>Your logic is very common but wrong


    >>while (fgets(readLine,10000,Fp), !feof(fp))
    >>{


    > Actually I don't think that code is correct. My unfamiliarity with C.


    > Try this


    > while (fgets(readLine,10000,Fp))
    > {
    > }


    As someone else noted, sorry for posting to so many groups.

    The reason the latter is preferred is that it also takes
    care of the I/O error case, where fgets() returns null,
    but EOF has not been reached. An uncorrectable error would
    otherwise result in an infinite loop. After the loop one might
    test both feof() and ferror(), but most of the time I would
    prefer to exit in both cases.

    -- glen
     
    glen herrmannsfeldt, Oct 21, 2004
    #16
  17. DJP

    Villy Kruse Guest

    On Mon, 18 Oct 2004 12:56:45 -0400,
    Vyacheslav Kononenko <> wrote:


    > feof() returns true AFTER you try to read past the end of file.
    > So this code should work better:
    > while(true) {
    > fgets(readLine,10000,Fp);
    > if( feof(Fp) ) break;


    That may still give problem with the last line if it was terminated by
    end of file instead of end of line. I've seen implementation which does
    set feof(Fp) to true while fgets() did return the last line so breaking
    out in this situation will ignore the last line. Whether this behaviour
    is ANSI conform I can't say. However, reading the last line did actualy
    reach end of file in this situation as the end of line wasn't encountered
    before end of file.

    Anyway, if you use the return value of fgets() as the end of loop
    condition it would work either way. The feof() function can then be
    used after the loop has terminated.


    > ...
    > }
    >
     
    Villy Kruse, Oct 21, 2004
    #17
    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. DJP
    Replies:
    7
    Views:
    7,508
    glen herrmannsfeldt
    Oct 21, 2004
  2. Charles Erwin
    Replies:
    4
    Views:
    594
    Joe Wright
    Oct 6, 2003
  3. Trond Valen
    Replies:
    5
    Views:
    442
    Niklas Norrthon
    Dec 7, 2005
  4. ramana
    Replies:
    5
    Views:
    372
    ramana
    Dec 26, 2007
  5. Andy
    Replies:
    3
    Views:
    3,479
Loading...

Share This Page