473,473 Members | 4,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Open a file in binary mode

I have written the following code in DEV C++ but the produced file is a
normal text file (you can type it in command line). I was expecting that the
numeric values would have written in their binary internal representation
and not as pure text.

What have I done wrong?

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

main ()
{
ofstream myfile;
myfile.open("datafile",ios::out|ios::binary);
myfile<<123<<34<<12.123<<"start"<<endl;
myfile<<"end";
myfile.close();
}
Nov 13 '07 #1
9 7006
Hatzigiannakis Nikos wrote:
I have written the following code in DEV C++ but the produced file is a
normal text file (you can type it in command line). I was expecting that the
numeric values would have written in their binary internal representation
and not as pure text.

What have I done wrong?

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

main ()
{
ofstream myfile;
myfile.open("datafile",ios::out|ios::binary);
This sets the file mode, not the stream.

--
Ian Collins.
Nov 13 '07 #2
and how can I have a binary stream?!

? "Ian Collins" <ia******@hotmail.com?????? ??? ??????
news:5p************@mid.individual.net...
Hatzigiannakis Nikos wrote:
>I have written the following code in DEV C++ but the produced file is a
normal text file (you can type it in command line). I was expecting that
the
numeric values would have written in their binary internal representation
and not as pure text.

What have I done wrong?

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

main ()
{
ofstream myfile;
myfile.open("datafile",ios::out|ios::binary);

This sets the file mode, not the stream.

--
Ian Collins.

Nov 13 '07 #3
Hatzigiannakis Nikos wrote:
and how can I have a binary stream?!
Usually you use the read() and write() methods to do that.

It can be tricky to get it right, what exactly are you trying to do ?
Nov 13 '07 #4
was trying to demonstrate the difference between the two modes. I cannot
see any since the output is exactly the same either in text or binary mode,
in the given code

? "Gianni Mariani" <gi*******@mariani.ws?????? ??? ??????
news:47********@news.eftel.com.au...
Hatzigiannakis Nikos wrote:
>and how can I have a binary stream?!

Usually you use the read() and write() methods to do that.

It can be tricky to get it right, what exactly are you trying to do ?

Nov 13 '07 #5
On 2007-11-13 06:39:45 -0500, "Hatzigiannakis Nikos" <ni***@ypai.grsaid:
? "Gianni Mariani" <gi*******@mariani.ws?????? ??? ??????
news:47********@news.eftel.com.au...
>Hatzigiannakis Nikos wrote:
>>and how can I have a binary stream?!

Usually you use the read() and write() methods to do that.

It can be tricky to get it right, what exactly are you trying to do ?
was trying to demonstrate the difference between the two modes. I cannot
see any since the output is exactly the same either in text or binary mode,
in the given code
Inserters do formatting, regardless of whether the stream is in binary
or text mode. write() outputs binary data, regardless of whether the
stream is in binary or text mode.

The difference between the two modes is whether the stream translates
"special" characters. In particular, '\n' represents a newline, and in
text mode the stream writes whatever is appropriate on the system where
it's running to represent the end of a line. In binary mode it's just
the value \x0D.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 13 '07 #6
Pete Becker wrote:
The difference between the two modes is whether the stream translates
"special" characters. In particular, '\n' represents a newline, and in
text mode the stream writes whatever is appropriate on the system where
it's running to represent the end of a line. In binary mode it's just
the value \x0D.
Uh, Pete, isn't that '\x0a'? I thought '\r' was '\x0d'.
Nov 13 '07 #7
On 2007-11-13 09:13:58 -0500, red floyd <no*****@here.dudesaid:
Pete Becker wrote:
>The difference between the two modes is whether the stream translates
"special" characters. In particular, '\n' represents a newline, and in
text mode the stream writes whatever is appropriate on the system where
it's running to represent the end of a line. In binary mode it's just
the value \x0D.

Uh, Pete, isn't that '\x0a'? I thought '\r' was '\x0d'.
You're probably right. But on further reflection, it's neither. For
some reason I thought the standard specified the value of '\r' and
'\n'. But that's a hallucination. The value depends on the compiler.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 13 '07 #8
Pete Becker wrote:
On 2007-11-13 09:13:58 -0500, red floyd <no*****@here.dudesaid:
>Pete Becker wrote:
>>The difference between the two modes is whether the stream translates
"special" characters. In particular, '\n' represents a newline, and in
text mode the stream writes whatever is appropriate on the system where
it's running to represent the end of a line. In binary mode it's just
the value \x0D.

Uh, Pete, isn't that '\x0a'? I thought '\r' was '\x0d'.

You're probably right. But on further reflection, it's neither. For some
reason I thought the standard specified the value of '\r' and '\n'. But
that's a hallucination. The value depends on the compiler.
That makes sense, since it would be dependent on the execution character
set (0x0a and 0x0d are ASCII, but EBCDIC would probably be quite different).
Nov 13 '07 #9
On 2007-11-14 05:18:49 -0500, James Kanze <ja*********@gmail.comsaid:
On Nov 13, 2:26 pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2007-11-13 06:39:45 -0500, "Hatzigiannakis Nikos" <ni...@ypai.grsaid:
>>? "Gianni Mariani" <gi4nos...@mariani.ws?????? ??? ??????
news:47********@news.eftel.com.au...
Hatzigiannakis Nikos wrote:
and how can I have a binary stream?!
>>>Usually you use the read() and write() methods to do that.
>>>It can be tricky to get it right, what exactly are you trying to do ?
was trying to demonstrate the difference between the two modes. I cann
ot
>>see any since the output is exactly the same either in text or binary m
ode,
>>in the given code
>Inserters do formatting, regardless of whether the stream is in binary
or text mode. write() outputs binary data, regardless of whether the
stream is in binary or text mode.

And when writing to a file, the embedded locale does code
translation, regardless of the mode or whether you use inserts
or unformatted output functions.
And since it's not affected by the mode, it's irrelevant to this discussion.
>
>The difference between the two modes is whether the stream translates
"special" characters.

There's a lot more to it than that.
Indeed. But giving long technical explanations to beginners is a good
way to discourage them.
>In particular, '\n' represents a newline,

Internally.
>and in text mode the stream writes whatever is appropriate on
the system where it's running to represent the end of a line.

Again, it's more subtle than that.
Again, fully detailed explanations are often counterproductive.
>
>In binary mode it's just the value \x0D.

Not on my machines. It's been 0x0A on every system I've seen.
This has already been dealt with in two followup messages. But thanks
for piling on.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 14 '07 #10

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

Similar topics

3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
8
by: Lucas | last post by:
I need print a file in binary mode . f = f.open('python.jpg','rb') bytes = f.read() f.close() print(bytes) I can't get any binary code.
4
by: nguser3552 | last post by:
Hello everyone, I'm wondering if someone out there knows how in a visual c++ console application how I can do the following, and man I've tried, it seems simple really: I need to open up any...
2
by: tubby | last post by:
Does a py script written to open and read binary files on Windows affect files on a Linux or Mac machine in a negative way? My concern is portability and safety. I want scripts written within...
2
by: parasprajapati2001 | last post by:
how can we use file in binary mode ?
10
by: Shraddha | last post by:
Why we should use binary mode of file? Where it is going to be actually needed???
3
by: BatthJeet | last post by:
Hi all ! Can we have some way to make a program in C-Language which opens an Image(.jpeg/.bmp ) file in binary mode like:- FILE *sourcefile; sourcefile=fopen("d:\\mypic.jpeg","rb"); Then...
1
by: vijaylaxmi | last post by:
hello, i want to open my audio file in binary mode and want to perform action of reading and writing on binary mode. then after whole process i wish that my file is save in its own format(audio)...
0
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,...
0
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.