question about gets() and feof().

Discussion in 'C Programming' started by sunnyboyGuo@gmail.com, Sep 16, 2005.

  1. Guest

    hello everyone,
    my code is like this:
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    FILE *fp;
    char str[128];

    if((fp=fopen(argv[1], "r"))==NULL) {
    printf("Cannot open file.\n");
    exit(1);
    }
    while(!feof(fp)) {
    if(fgets(str, 126, fp))
    printf("%s", str);
    else
    printf("error.\n");
    }
    fclose(fp);
    return 0;
    }
    ////////////////////////////////////////////
    if the input file is like this:
    abc //first line
    ddd //second line. and there is no "enter" in the end.
    our code will run correctly.
    if the input file is like this:
    abc //first line
    ddd //second line. in the end, press "enter"
    //last line, this line is press "enter" in the second line.
    //and there is none other key in the last line.
    in this case, my code will output "error".

    what's wrong?

    sincerely

    guo
     
    , Sep 16, 2005
    #1
    1. Advertising

  2. wrote:

    > #include <stdio.h>
    > #include <stdlib.h>


    > int main(int argc, char *argv[])
    > {
    > FILE *fp;
    > char str[128];


    > if((fp=fopen(argv[1], "r"))==NULL) {


    What if argc is 1?

    > printf("Cannot open file.\n");
    > exit(1);


    exit( EXIT_FAILURE );

    > }
    > while(!feof(fp)) {
    > if(fgets(str, 126, fp))


    if( fgets(str,sizeof str,fp) )

    > printf("%s", str);
    > else
    > printf("error.\n");
    > }
    > fclose(fp);
    > return 0;
    > }


    > what's wrong?


    You have asked a FAQ:

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

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Sep 16, 2005
    #2
    1. Advertising

  3. wrote:
    [...]
    > while(!feof(fp)) {
    > if(fgets(str, 126, fp))
    > printf("%s", str);
    > else
    > printf("error.\n");
    > }

    [...]
    > if the input file is like this:
    > abc //first line
    > ddd //second line. in the end, press "enter"
    > //last line, this line is press "enter" in the second line.
    > //and there is none other key in the last line.
    > in this case, my code will output "error".
    >
    > what's wrong?


    A NULL return from fgets does not necessarily mean "error".

    fgets() returns NULL if EOF is hit before reading anything. Since there
    is nothing between the newline after "ddd" on the second line and EOF,
    there are no more characters left to read. Hence NULL is returned even
    though no error has occurred.

    See also <http://www.eskimo.com/~scs/C-faq/q12.2.html> for further info
    on another problem which you haven't hit yet.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Sep 16, 2005
    #3
  4. Simon Biber Guest

    wrote:
    > hello everyone,
    > my code is like this:
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char *argv[])
    > {
    > FILE *fp;
    > char str[128];
    >
    > if((fp=fopen(argv[1], "r"))==NULL) {
    > printf("Cannot open file.\n");
    > exit(1);
    > }
    > while(!feof(fp)) {
    > if(fgets(str, 126, fp))
    > printf("%s", str);
    > else
    > printf("error.\n");
    > }


    The logic of this while loop is wrong. Replace it by:

    while(fgets(str, sizeof str, fp))
    {
    printf("%s", str);
    }
    if(ferror(fp))
    {
    printf("error.\n");
    }

    fgets will also return NULL if end-of-file occurs before reading any
    data in. This is not an error condition. You can tell if an error
    occurred by calling ferror.

    > fclose(fp);
    > return 0;
    > }


    --
    Simon.
     
    Simon Biber, Sep 16, 2005
    #4
    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. Mantorok Redgormor

    feof usage

    Mantorok Redgormor, Sep 20, 2003, in forum: C Programming
    Replies:
    28
    Views:
    1,150
    Dave Thompson
    Sep 29, 2003
  2. ehui928

    A question about fscanf and feof !

    ehui928, Oct 15, 2006, in forum: C Programming
    Replies:
    7
    Views:
    3,159
    Joe Wright
    Nov 7, 2006
  3. rCs

    EOF vs. feof() and ferror()

    rCs, Oct 31, 2006, in forum: C Programming
    Replies:
    8
    Views:
    1,772
    Barry Schwarz
    Nov 1, 2006
  4. Willem

    Question on while(!feof(fp))

    Willem, Jan 30, 2008, in forum: C Programming
    Replies:
    5
    Views:
    394
    Keith Thompson
    Feb 1, 2008
  5. Andy
    Replies:
    3
    Views:
    3,422
Loading...

Share This Page