ios::binary not working

I've started getting used to working with files, and have just learnt about the modifiers I can give when initializing fstreams. I've tried practicing with the std::ios::binary modifier, and it is to my understanding that when trying to output to the file, all escape characters won't be taken as escape characters but will be outputted to the file as if they were normal text:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <string>

int main()
{
      std::fstream openFile("text.txt", std::ios::out | std::ios::binary);

      try
      {
            if (!openFile.is_open())
            {
                  throw 1;
            }
      }
      catch (int x)
      {
            std::cout << "Error code " << x << ": text.txt not found" << std::endl;
      }

      openFile << "I am testing the\nios::binary\tmodifier";

      system("pause");
      return 0;
}


The program runs, but when I check the text.txt file in the project folder, it will have written in it:
I am testing theios::binary	modifier

When I'm expecting it to say "I am testing the\nios::binary\tmodifier".

Is there something wrong with the way I wrote the program? Or am I misinformed as to how the ios::binary modifier works?

Thanks in advance :)
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

Last edited on
I see. Thanks for your help :)
So then how do you suggest is the best way to use ios::binary to one's advantage?
Well, not all files are ordinary text. For example there are images *.bmp or *.jpg and sounds *.mp3 or *.wav and so on. If you found a need to read or write from those types of file then binary mode would be appropriate.

There are other uses, such as saving the data contained within an object belonging to a C++ class which you might be working with. However such usage requires care and is not something you'd necessarily want to do until there was a definite need to do so.
Topic archived. No new replies allowed.