[URGENT] fgets reading last line in file twice

Discussion in 'C++' 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);
    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 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
    #6
  7. "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
    #7
  8. 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
    #8
    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. Charles Erwin
    Replies:
    4
    Views:
    567
    Joe Wright
    Oct 6, 2003
  2. DJP
    Replies:
    16
    Views:
    959
    Villy Kruse
    Oct 21, 2004
  3. Trond Valen
    Replies:
    5
    Views:
    384
    Niklas Norrthon
    Dec 7, 2005
  4. ramana
    Replies:
    5
    Views:
    346
    ramana
    Dec 26, 2007
  5. Andy
    Replies:
    3
    Views:
    3,349
Loading...

Share This Page