How Binary Output works

Posted on 2006-05-15
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

    >> 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

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

    Accepted Solution

    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));
    LVL 8

    Expert Comment

    by:List244 -- Here is something you can read on the matter.
    LVL 23

    Expert Comment

    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

    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!
    LVL 8

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    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 a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now