How Binary Output works

Posted on 2006-05-15
Medium Priority
Last Modified: 2010-04-01
I am outputting to a file in binary, however when I do a comand like "less" or "cat" to view the file it looks as if I wrote it out like normal text..

ofstream myFile ("filename", ios::out | ios::binary);
char buffer[5];
buffer[0] = 'H';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'l';
buffer[4] = 'o';
buffer[5] = '\0';
myFile.write (buffer, 4);

when i look at the file.. it simply says "Hello".
I thought that when I outputted it to binary, it would be gibberish I couldn't read??
Question by:cfans
LVL 12

Expert Comment

ID: 16687575
>> however when I do a comand like "less" or "cat" to view the file it looks as if I wrote it out like normal text..
Text will always look normal
LVL 12

Expert Comment

ID: 16687578
To see the difference put some integers and floats in it
LVL 12

Accepted Solution

rajeev_devin earned 400 total points
ID: 16687606
Something like this

ofstream myFile ("filename", ios::out | ios::binary);
int i = 100;
double f = 23.45;

myFile.write ((char*)&i, sizeof(int));
myFile.write ((char*)&f, sizeof(double));
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 16687631
http://www.felgall.com/cplus4.htm -- Here is something you can read on the matter.
LVL 23

Expert Comment

ID: 16687645
The "binary" value of ASCII text is the ASCII codes of the characters.
You do have a bug, however.  Notice that you defined buffer as a 5 character array, but write 6 characters to it.  This is known as a buffer overflow (or buffer overrun), a common cause of memory corruption and security vulnerability.


Author Comment

ID: 16687788
I did realize that after I posted I had an error, that was just me not paying attention.

Thanks guys for explaining this!  Much appericated!

Expert Comment

ID: 16687804
Cfans, while you have accepted an answer, I would highly suggest you still read that link I posted if you have not.
Rajeev's explanation will show you how it looks, my link will actually explain it for you.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question