473,322 Members | 1,566 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,322 software developers and data experts.

Detecting binary verses text file streams

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 to work?

Given that it might be complicated to change the behavior of operator
<< for binary streams, I would like to be able to determine how an
object should serialize itself to a stream so that numeric values are
written as their actually value for binary streams and as their ASCII
representation for text streams. How can this be done?
Jul 22 '05 #1
3 3447
"Tron Thomas" writes:
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 to work?

Given that it might be complicated to change the behavior of operator
<< for binary streams, I would like to be able to determine how an
object should serialize itself to a stream so that numeric values are
written as their actually value for binary streams and as their ASCII
representation for text streams. How can this be done?


There is a lot of bad information on the Internet and it looks like you came
across some of it. << and >> both implicitly specify conversion to or from
the underlying character code. For binary files you should use read() and
write(). Does their use cause you problems? (Other than a presumed
elegance, that is)
Jul 22 '05 #2

"Tron Thomas" <tr*********@verizon.net> wrote in message
news:a4*************************@posting.google.co m...
What does binary mode for an ofstream object do anyway? Despite which
mode the stream uses, operator << writes numeric value as their ASCII
representation.
Yes << for for text output, write is for binary output.

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 to work?
That is garbage.

Given that it might be complicated to change the behavior of operator
<< for binary streams, I would like to be able to determine how an
object should serialize itself to a stream so that numeric values are
written as their actually value for binary streams and as their ASCII
representation for text streams. How can this be done?


There are no such things as binary streams and text streams. Streams are
just streams. File streams can be opened in binary mode or text mode, but
you can do text output on a stream opened in binary mode, and you can do
binary output on a stream opened in text mode (not advisable though).

Text mode, binary mode is commonly misunderstood. In text mode the
implementation is allowed to make certain transformations of characters read
and written, a common example is converting \n to \r\n on output on PC
systems. This makes perfect sense if you are doing text output but it is
disastrous if you are doing binary output.

Text output use <<, binary output use write, and if you are planning on
binary output then open in binary mode so you don't get any character
transformations.

john
Jul 22 '05 #3
"osmium" <r1********@comcast.net> wrote in message news:<2v*************@uni-berlin.de>...
There is a lot of bad information on the Internet and it looks like you came
across some of it. << and >> both implicitly specify conversion to or from
the underlying character code. For binary files you should use read() and
write(). Does their use cause you problems? (Other than a presumed
elegance, that is)


The user of read and write do not cause any problems. I justed wanted
to know whether I wanted to use operator << or the write method
depending on whether the file was meant to be written in binary format
or text format.

I was hoping the mode of the stream would help me make this
determination. It appears that there isn't a lot of difference
between a stream in binary mode and a stream in text mode. This looks
like knowing which method of writing to the stream is something I
would have to track outside of the stream.
Jul 22 '05 #4

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

Similar topics

3
by: Romain | last post by:
Hello, I am writing out a binary file. I figured that the number "10" is automaticaly converted to "OD OA" instead of "OD". "OD" and "OA" are line feed and carriage return. I know it does...
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); }
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
8
by: Aaron Turner | last post by:
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...
5
by: bwv539 | last post by:
I have to output data into a binary file, that will contain data coming from a four channel measurement instrument. Since those data have to be read from another C program somewhere else, the...
27
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type;...
2
by: subramanian100in | last post by:
In K & R ANSI C book(2nd Edition), in page 241, the following lines are mentioned. "The library supports text streams and binary streams, although on some systems, notably UNIX, these are...
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...
0
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...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.