Write char array to binary file

I need to write a char array to new binary file.
I tried to do something like this but without success.

My code:
1
2
3
4
        char ch[] = "My name is"
        fstream bin ("bin.txt",ios :: out | ios :: binary);
        bin.write(reinterpret_cast<char *> (&ch),sizeof(ch));
        bin.close();




If i open my bin.txt i see that string correctly. But I need to convert it to binary.
Or i missed something?
YOu would be surprized, but, 'M' is still looks as 'M' when written written in binary file and opened in text editor.
oh.. So can you explain me please why integers or double will shown as unknown characters in file and string not?
Last edited on
String is array of characters. Each character is on modern desktops is single 8-bit byte in size.
So string "Hello" in ASCII is sequence of bytes "48 65 6C 6C 6F 21 00" and will be written as such in binary file (which is too a sequence of byte)

Integers are several bytes in size (say 4 for Windows platform). So value of 4567784 will be saved as "00 45 B2 E8" or "E8 B2 45 00" depending on endianness of platform. When you will look in binary file, you will se series of 4 characters representing individual bytes: "E▓ш" or "ш▓E" (first — or last — symbol cannot be printed)
Last edited on
It depends what program you use in order to view the file contents. When i opened it in a text editor, it issued a warning that the file had not been completely loaded, and that the file might be damaged or not a plain text file.

The actual contents of the array is the same as if the code had been written like this:
 
    char ch[] = { 'M', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', 0 }; 

In this case, the terminating null byte is actually written to the file, which you can see if you use a hex editor rather than an ordinary text editor to view the file.
Chervil... i tried to open with windows notepad and it's open it correctly with my string.
and what's the difference if i write it in string "My name is".. in this way terminating null added too, no?
In your case, yes, only difference between binary and text mode is terminating null.

In general case, there can be some junk after terminating 0 if array size is less than full string length.
If you do this:
1
2
3
    char ch[] = "My name is"
    ofstream fout("text.txt");
    fout << ch;
then the terminating null character does not get written to the file, as it isn't considered a part of the string. But in the previous case, the null is written too.

I'd recommend a program such as hxd to view (or alter) the contents of binary files. It can show you content which text editors such as notepad cannot.
http://mh-nexus.de/en/hxd/

As a test, try repeating this line:
1
2
    bin.write(reinterpret_cast<char *> (&ch),sizeof(ch));
    bin.write(reinterpret_cast<char *> (&ch),sizeof(ch));

After re-running the program, what does notepad show this time?
Last edited on
oh god... 祍渠浡⁥獩䴀⁹慮敭椠s

why that?
Are you using Notepad? It probably decided that it is text in Unicode.
Read this article on famous auto encoding detection failure: http://en.wikipedia.org/wiki/Bush_hid_the_facts
yeah.. notepad
If you can fix the autoencoding problem, notepad is going to display the file as if it were ASCII.

If you have:
1
2
3
4
5
6
7
8
#include <fstream>

int main( void )
{
  std::ofstream output( "my_file.dat", std::ios::out | std::ios::binary );
  unsigned int i = 1;
  output.write( reinterpret_cast<char*>(&i), sizeof(i) );
}


Then notepad will try to show you 00 00 00 01 or 01 00 00 00. It won't look like anything.

However,
1
2
3
4
5
6
7
8
#include <fstream>

int main( void )
{
  std::ofstream output( "my_file.dat", std::ios::out | std::ios::binary );
  unsigned int i = 0x41424344;
  output.write( reinterpret_cast<char*>(&i), sizeof(i) );
}


Notepad will show you 41 42 43 44 or 44 43 42 41. Those are printable characters, you should see ABCD or DCBA
Topic archived. No new replies allowed.