Unable to write binary

I have the following C ++ code snippet.

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ofstream output("Sample.txt", ios::out | ios::binary);

for(int i = 0; i < 10; i++)
{
  output<<arr[i];
}

Now Sample.txt looks like this:

12345678910

Isn't "Sample.txt" supposed to be in binary format? Why does it not convert everything to binary when I opened the stream in binary mode. What should I do if I need the binary nature of each element of the array and then print it in a file.

+5
source share
4 answers

Shouldn't "Sample.txt" be in binary format?

. std::ios::binary, , '\n' / EOL . . , . ( , , .)

std::ostream::write(). , .

+3

, () , , , (, "\ r\n" Windows, "\ r" ).

, ofstream.write().

0

, / , . int ofstream ( ASCII).

, , char output.write (docs here).

0

, 'not text' , , ..

.:

File streams opened in binary mode perform input and output operations regardless of any format considerations. Non-binary files are known as text files, and some translation may occur due to the formatting of some special characters (for example, newlines and carriage returns).

It does not convert you to 0and format 1. Everything on the PC uses the format binary, but when you open the file and read it using the editor, the editor reads the binary data and displays the ASCII characters for you.

0
source

All Articles