473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Binary stream detection

Dear All,

I am working on some cross-platform code and using read() and
write() on binary streams. 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.

Whilst there is a mechanism to obtain format flags I have been
unable to find any flag or mechanism to determine the flags
used when opening the stream. Is there such a mechanism that
I am missing?

Many thanks

Aaron Turner
Feb 23 '07 #1
8 2001
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)
Feb 23 '07 #2

"Aaron Turner" <aa***@cs.york.ac.ukwrote in message
news:er**********@netty.york.ac.uk...
Dear All,

I am working on some cross-platform code and using read() and
write() on binary streams. 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.

Whilst there is a mechanism to obtain format flags I have been
unable to find any flag or mechanism to determine the flags
used when opening the stream. Is there such a mechanism that
I am missing?
There are the flags which are set in the ctor, but I'm not sure whether you
can access them directly. You might have a look in the implementation of
stream in the standard library. However, I'm a little puzzled by what you
want to achieve because shouldn't you, being the one who opens the stream,
know which mode was used or am I missing something?

Cheers
Chris
Feb 23 '07 #3
Chris Theis wrote:
"Aaron Turner" <aa***@cs.york.ac.ukwrote in message
news:er**********@netty.york.ac.uk...
>Dear All,

I am working on some cross-platform code and using read() and
write() on binary streams. 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.

Whilst there is a mechanism to obtain format flags I have been
unable to find any flag or mechanism to determine the flags
used when opening the stream. Is there such a mechanism that
I am missing?

There are the flags which are set in the ctor, but I'm not sure
whether you can access them directly. You might have a look in the
implementation of stream in the standard library. However, I'm a
little puzzled by what you want to achieve because shouldn't you,
being the one who opens the stream, know which mode was used or am I
missing something?
It's possible that the stream is given as an argument. Of course,
in such case there should be a convention in place what type of
stream it should be...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 23 '07 #4
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:er**********@news.datemas.de...
Chris Theis wrote:
>"Aaron Turner" <aa***@cs.york.ac.ukwrote in message
news:er**********@netty.york.ac.uk...
>>Dear All,

I am working on some cross-platform code and using read() and
write() on binary streams. 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.

Whilst there is a mechanism to obtain format flags I have been
unable to find any flag or mechanism to determine the flags
used when opening the stream. Is there such a mechanism that
I am missing?

There are the flags which are set in the ctor, but I'm not sure
whether you can access them directly. You might have a look in the
implementation of stream in the standard library. However, I'm a
little puzzled by what you want to achieve because shouldn't you,
being the one who opens the stream, know which mode was used or am I
missing something?

It's possible that the stream is given as an argument. Of course,
in such case there should be a convention in place what type of
stream it should be...
I totally agree on this point. IMHO either a convention or a wrapper that
should provide the required information should do the job.

Chris

Feb 23 '07 #5
Pete Becker wrote:
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).
The possibilities of different sizes of data types and endianess have
been dealt with in a series of 1 byte data blocks at the beginning which
allows the system to reconfigure for all the supported options that
the system will be compiled for. So it is portable in the sense that
it can adjust to those supported options.
To answer your question: no, you can't directly determine after the fact
whether a file was opened in the mode you wanted.
Thanks

Aaron Turner

Feb 23 '07 #6
Chris Theis wrote:
There are the flags which are set in the ctor, but I'm not sure whether you
can access them directly. You might have a look in the implementation of
stream in the standard library. However, I'm a little puzzled by what you
want to achieve because shouldn't you, being the one who opens the stream,
know which mode was used or am I missing something?
The method that handles the stream and serialises information to/from it
is contained within a library. This takes into account differences of
datatype sizes, endianess, and so on. This has been tested on a wide
variety of Unix-like systems and 32 and 64 bit Windows of various
flavours. On Unix and Unix-like systems there isn't a problem within
the library code if the stream has been opened binary or text, but it
does present a problem on Windows system.

Whilst ideally users of the system should open the stream as binary I
can't necessarily enforce it, so detecting if it wasn't opened binary
and then throwing an error would be very useful.

Regards,

Aaron Turner
Feb 23 '07 #7
Chris Theis wrote:
I totally agree on this point. IMHO either a convention or a wrapper that
should provide the required information should do the job.
Perhaps this might be the best idea. Whilst the old function in the
library will still need to remain for backwards compatibility adding
a wrapper class to enforce binary streams and adding some additional
wrappers within the library would provide a neat solution to this
issue.

Many thanks

Aaron Turner
Feb 23 '07 #8
Aaron Turner wrote:
a wrapper class to enforce binary streams and adding some additional
wrappers within the library would provide a neat solution to this
issue.
Hmmm actually I spot a problem as the library code passes uses the
general istream and ostream, so a wrapper would have to allow
any classes derived from these as arguments too, which would
make a wrapper tricky.

Regards,

Aaron Turner

Feb 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
5
by: Charles F McDevitt | last post by:
I'm converting some old programs that use old iostreams. In one program, the program is using cout to output to the stdout stream. Part way through, the program wants to put some binary data out,...
5
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
31
by: Claude Yih | last post by:
Hi, everyone. I got a question. How can I identify whether a file is a binary file or an ascii text file? For instance, I wrote a piece of code and saved as "Test.c". I knew it was an ascii text...
12
by: Registered User | last post by:
I've read in a book: <quote> With a binary-mode stream, you can't detect the end-of-file by looking for EOF, because a byte of data from a binary stream could have that value, which would...
33
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: //...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.