I think your expectations of what should be happening are perhaps incorrect. The only significant difference in this example between binary and text mode is that on a windows system \n translates to two bytes in the output, a CR LF pair.
When using binary mode, you probably should not be naming the file with a .txt extension and opening in in an ordinary text editor, as you won't be able to see the file contents accurately.
i ran the above code and viewed output file in a hex editor, it looked like this:
Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 49 20 61 6D 20 74 65 73 74 69 6E 67 20 74 68 65 I am testing the
00000010 0A 69 6F 73 3A 3A 62 69 6E 61 72 79 09 6D 6F 64 .ios::binary.mod
00000020 69 66 69 65 72 ifier |
Notice the
0A
and
09
bytes which are the \n and \t characters.
hex editor for windows:
http://mh-nexus.de/en/hxd/
Edit note also that in the original code, the characters we are talking about are part of the string
"I am testing the\nios::binary\tmodifier"
. The translation of \n and \t to the newline and tab take place at compile time, the original text with the actual \n and \t no longer exists when the program is executed, as it is part of the source code in the cpp file, not the finished executable program.
Illustration:
1 2 3 4 5
|
std::string one("ABCDEFG");
std::string two("A\nD\tG");
std::cout << "Length one: " << one.size() << std::endl;
std::cout << "Length two: " << two.size() << std::endl;
|
Length one: 7
Length two: 5 |