How to check for an end of file without feof() - Linux

This is a discussion on How to check for an end of file without feof() - Linux ; Hello All, Is there any way to check for the end of file without using feof(). In fact, I have opened a file using fildes = open ( "myfile", O_RDONLY ); therefore, I have with me int fildes instead of ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: How to check for an end of file without feof()

  1. How to check for an end of file without feof()

    Hello All,

    Is there any way to check for the end of file without using feof().

    In fact, I have opened a file using

    fildes = open ( "myfile", O_RDONLY );

    therefore, I have with me int fildes instead of FILE * fileptr which is
    required to check for end of file
    using feof (fileptr);

    So, my question is again as noted in the first line of this post.

    Thanks for any help.

    A.


  2. Re: How to check for an end of file without feof()

    "anonymous" writes:

    > Hello All,
    >
    > Is there any way to check for the end of file without using feof().
    >
    > In fact, I have opened a file using
    >
    > fildes = open ( "myfile", O_RDONLY );
    >
    > therefore, I have with me int fildes instead of FILE * fileptr which is
    > required to check for end of file
    > using feof (fileptr);
    >
    > So, my question is again as noted in the first line of this post.


    Ask yourself what a EOF is?

    For example:

    ( while sleep $(random 10) ; do echo some data ; done ) > file &
    sleep $(random 10)
    while sleep $(random 10) ; do read line ; echo $line ; done < file


    When will the second while find the end of the file 'file'?


    In conclusion, the only sure way to know you've reached the end of the
    file, is to try to read it and get 0 byte without other error:

    {
    int res;
    tryagain:
    res=read(fd,buffer,sizeof(buffer));
    switch(res){
    case 0:
    printf("We've found the end of file.");
    break;
    case EINTR:
    goto tryagain;
    case EAGAIN:
    do_something_else_waiting_for_data();
    goto tryagain;
    default:
    if(res<0){
    perror("read");
    }else{
    printf("We got %d bytes.",res);
    }
    }
    }


    --
    __Pascal Bourguignon__ http://www.informatimago.com/

    "What is this talk of "release"? Klingons do not make software
    "releases". Our software "escapes" leaving a bloody trail of
    designers and quality assurance people in its wake."

  3. Re: How to check for an end of file without feof()

    anonymous wrote:
    > Hello All,
    >
    > Is there any way to check for the end of file without using feof().
    >
    > In fact, I have opened a file using
    >
    > fildes = open ( "myfile", O_RDONLY );
    >
    > therefore, I have with me int fildes instead of FILE * fileptr which is
    > required to check for end of file
    > using feof (fileptr);


    In a nutshell: you've reached the end-of-file if you cannot read any
    more, i.e. if read(fildes, buffer, nbytes) returns 0.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  4. Re: How to check for an end of file without feof()


    Josef Moellers wrote:
    > anonymous wrote:
    > > Hello All,
    > >
    > > Is there any way to check for the end of file without using feof().
    > >
    > > In fact, I have opened a file using
    > >
    > > fildes = open ( "myfile", O_RDONLY );
    > >
    > > therefore, I have with me int fildes instead of FILE * fileptr which is
    > > required to check for end of file
    > > using feof (fileptr);

    >
    > In a nutshell: you've reached the end-of-file if you cannot read any
    > more, i.e. if read(fildes, buffer, nbytes) returns 0.
    >



    many thanks.

    > --
    > Josef Möllers (Pinguinpfleger bei FSC)
    > If failure had no penalty success would not be a prize
    > -- T. Pratchett



  5. Re: How to check for an end of file without feof()

    anonymous wrote:
    > therefore, I have with me int fildes instead of FILE * fileptr which is
    > required to check for end of file
    > using feof (fileptr);


    By the way, your understanding of int feof may be off.

    The stdio routines report the end of file or other conditions in a
    similar way to the lower level POSIX system functions: a value is
    returned from the operation which indicates that it did not complete.

    The purpose of the feof and ferror functions is to distinguish whether
    the failure occured due to an end-of-file condition or due to error.
    They are mere accessors which look at information that was recorded in
    the stream object by a previous operation.

    Bad example:

    while (!feof(stream)) {
    fgets(buffer, size, stream);
    ...
    }

    The end-of-file condition won't automatically be set when the last byte
    of input is read from a file. The underlying attempt to read bytes has
    to fail, and that failure event will set the condition. Abover, the
    fgets function actually returns an indicator whether it failed, which
    is being ignored on the assumption that feof actually detects the end
    of the stream. (And of course, the read can fail for for reasons other
    than end-of-file). Thus, the body of the loop can mistakenly process
    data which was already in the buffer previously, as it if was new
    input. What's worse, if an error occurs, it will fail to terminate.

    Example:

    int ch;

    /* ANSI C doesn't require I/O functions to set errno on error. */
    errno = 0;

    /* Copy stdin to stdout until something happens. */

    while ((ch = getchar() != EOF)
    if (putchar(ch) == EOF)
    break;

    /* Now determine why the loop terminated */

    if (feof(stdin)) {
    /* ran out of input on stdin */
    } else if (ferror(stdin)) {
    /* encountered error on stdin */
    if (errno != 0) {
    /* errno has something to say */
    } else {
    /* library did not set errno. Ah well. */
    }
    } else if (ferror(stdout)) {
    /* encountered error on stdout */
    /* errno can be analyzed here too */
    } else {
    /* "Impossible" condition, WTF? */
    }

    >From this you can see the advantage of having the error state

    remembered in the stream object. Your main logic simply has to ensure
    that it bails when it hits an error, yet subsequent code can analyze
    quite exactly what happened in what stream.


  6. Re: How to check for an end of file without feof()


    Kaz Kylheku wrote:
    > anonymous wrote:
    > > therefore, I have with me int fildes instead of FILE * fileptr which is
    > > required to check for end of file
    > > using feof (fileptr);

    >
    > By the way, your understanding of int feof may be off.
    >
    > The stdio routines report the end of file or other conditions in a
    > similar way to the lower level POSIX system functions: a value is
    > returned from the operation which indicates that it did not complete.
    >
    > The purpose of the feof and ferror functions is to distinguish whether
    > the failure occured due to an end-of-file condition or due to error.
    > They are mere accessors which look at information that was recorded in
    > the stream object by a previous operation.
    >
    > Bad example:
    >
    > while (!feof(stream)) {
    > fgets(buffer, size, stream);
    > ...
    > }
    >
    > The end-of-file condition won't automatically be set when the last byte
    > of input is read from a file. The underlying attempt to read bytes has
    > to fail, and that failure event will set the condition. Abover, the
    > fgets function actually returns an indicator whether it failed, which
    > is being ignored on the assumption that feof actually detects the end
    > of the stream. (And of course, the read can fail for for reasons other
    > than end-of-file). Thus, the body of the loop can mistakenly process
    > data which was already in the buffer previously, as it if was new
    > input. What's worse, if an error occurs, it will fail to terminate.
    >
    > Example:
    >
    > int ch;
    >
    > /* ANSI C doesn't require I/O functions to set errno on error. */
    > errno = 0;
    >
    > /* Copy stdin to stdout until something happens. */
    >
    > while ((ch = getchar() != EOF)
    > if (putchar(ch) == EOF)
    > break;
    >
    > /* Now determine why the loop terminated */
    >
    > if (feof(stdin)) {
    > /* ran out of input on stdin */
    > } else if (ferror(stdin)) {
    > /* encountered error on stdin */
    > if (errno != 0) {
    > /* errno has something to say */
    > } else {
    > /* library did not set errno. Ah well. */
    > }
    > } else if (ferror(stdout)) {
    > /* encountered error on stdout */
    > /* errno can be analyzed here too */
    > } else {
    > /* "Impossible" condition, WTF? */
    > }
    >
    > >From this you can see the advantage of having the error state

    > remembered in the stream object. Your main logic simply has to ensure
    > that it bails when it hits an error, yet subsequent code can analyze
    > quite exactly what happened in what stream.


    Many thanks. I appreciate all the help that I got. It has solved my
    problem and helped me
    get a better understanding of various file operations. Thanks again.


+ Reply to Thread