Aaron Turner wrote:
>
I am working on some cross-platform code and using read() and
write() on binary streams.
Binary streams are not portable. The only guarantee you have is that if
you write a file in binary mode you can read it with code compiled with
the same compiler (which implies the same compiler switches).
What I would like to do is to
determine whether a stream has been opened as binary to avoid
the problem where a file has been opened as text and then is
used on a Windows system as Windows 'helpfully' inserts extra
characters when it sees some control characters.
Different operating systems have different conventions for marking line
endings, and the C and C++ runtime systems convert '\n' characters into
the appropriate character sequence when you write a file in text mode.
When you read it in binary mode you get whatever bytes were written to
the file, which means you get different line ending sequences on Unix,
Windows, and Mac. If you're serious about writing cross-platform code,
lose the Unix jingoism.
To answer your question: no, you can't directly determine after the fact
whether a file was opened in the mode you wanted.
--
-- Pete
Roundhouse Consulting, Ltd. (
www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (
www.petebecker.com/tr1book)