473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2035
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.a c.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.a c.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.********@com Acast.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
implementati on 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
3485
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 possible to change the behavior of operator << so it will stream numeric values as their actual values when an ofstream is in binary mode. I did not, however, find any information on how this can be accomplished. What is involved in getting this...
103
48649
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 says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
28
2789
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
5
7493
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, and changes the iostream to binary like this: cout << "this is text" << eol; binary(cout); cout << "this is binary" << eol; text(cout); cout << "back to text mode" << eol;
5
5309
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 quotes), and also some more text like this and file name (e.g. "Content-Disposition: form-data; name="upload_file"; filename="C:\testing\myfile.da<WBR>­t" Content-Type: application/octet-stream")
3
3480
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. -Regards Arun
31
3148
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 file. Then after compilation, I got a "Test" file and it was a binary executable file. The problem is, I know the type of those two files in my mind because I executed the process of compilation, but how can I make the computer know the type of a...
12
16154
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 result in premature end of input. Instead, you can use the library function feof(), which can be used for both binary- and text-mode files: int feof(FILE *fp);
33
2047
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: // ... bool good() const; // next operation might succeed bool eof() const; // end of input seen
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.