low level eof()

Discussion in 'C++' started by Ulterior, Apr 12, 2008.

  1. Ulterior

    Ulterior Guest

    Hi, anyone knows eof() function substitute for linux using open, close
    and read functions? I am tired of searching in google, sorry
     
    Ulterior, Apr 12, 2008
    #1
    1. Advertising

  2. Ulterior <> writes:

    > Hi, anyone knows eof() function substitute for linux using open, close
    > and read functions? I am tired of searching in google, sorry


    Yes, and you could even be tired to death if you went on, since there
    is no such function.

    EOF is not a state, it's a condition that is encoutered when you _try_
    to read past the 'end of the file'.

    EOF is when read(2) returns 0.

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Apr 14, 2008
    #2
    1. Advertising

  3. Ulterior

    James Kanze Guest

    On Apr 14, 9:48 am, (Pascal J. Bourguignon)
    wrote:
    > Ulterior <> writes:
    > > Hi, anyone knows eof() function substitute for linux using open, close
    > > and read functions? I am tired of searching in google, sorry


    > Yes, and you could even be tired to death if you went on,
    > since there is no such function.


    > EOF is not a state, it's a condition that is encoutered when
    > you _try_ to read past the 'end of the file'.


    Just a nit, but EOF is a macro, defined to expand to an integral
    constant expression of type int and a negative value. Anytime
    you see a function which reads (narrow) characters, but returns
    an int, you can pretty much assume that it returns a value in
    the range 0...UCHAR_MAX (with the character), or EOF. (Note
    that this means that the results of assigning it to a char, even
    after testing for EOF, is implementation defined---and may raise
    an implementation defined signal! In practice, any
    implementation which didn't support this would break so much
    code it wouldn't be used.)

    If you want to be "in", in C++, you'll write
    "std::char_traits< char >::eof()" in its place. (I'm an old
    fogey, who's not in, so I still write EOF. Except in a
    template.)

    > EOF is when read(2) returns 0.


    Some filebuf implementations will also return EOF when read
    returns -1:). (This was, of course, about all they could do in
    the classical iostream, before exceptions.)

    The Unix file system, as such, doesn't recognize end of file as
    a particular condition. A function like read will read as many
    bytes as are available, up to a maximum that you specify, from
    the current position in the file. The exact conditions
    defining when it might return with less bytes than requested
    depend on the file type, and in the case of "streams", how the
    stream has been configured. For regular files, returning with
    less than you requested means that you have reached the current
    end of the file. The current end; if someone appends data to
    the file, a later read will return it.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 15, 2008
    #3
    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.

Share This Page