low level eof()

U

Ulterior

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

Pascal J. Bourguignon

Ulterior said:
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.
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top