472,777 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,777 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 3403
"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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.