30

I'm trying to figure out the difference between opening a file like:

fstream *fileName*("FILE.dat",ios::binary);

or

fstream *fileName*("FILE.dat",ios::out);

or

fstream *fileName*("FILE.dat",ios::binary | ios::out);

I found that all of these forms are identical: in all cases, the same output on the file is produced using either *fileName*<< or *fileName*.write().

Jamal
  • 763
  • 7
  • 22
  • 32
Alan_AI
  • 1,529
  • 3
  • 22
  • 32

2 Answers2

37

ios::out opens the file for writing.

ios::binary makes sure the data is read or written without translating new line characters to and from \r\n on the fly. In other words, exactly what you give the stream is exactly what's written.

Nick Bedford
  • 4,365
  • 30
  • 36
  • and what does it mean to use both? – Alan_AI Feb 08 '10 at 23:36
  • Well, you'd be writing to a file without translating any characters. – Nick Bedford Feb 09 '10 at 00:04
  • 3
    So if I don't mark an ifstream as binary, and read, say 10 doubles from it, and Windows finds a random '\n' in the data, then the stream get expanded from `10*sizeof(double)` to `10*sizeof(double) +1` as a `\r` is inserted, and overwrites then end of my double buffer? – user14717 Apr 10 '15 at 13:46
10

Opening a file with ios::binary controls how newline characters are handled. On Windows, they are expanded to CRLF pairs. That's it - it has no effect on how things like operator<< work.

  • 3
    CLRF stands for carriage-return, line feed. These are the two bytes used to specify a new line in Windows text encoding. It's mostly redundant because on a computer, you really only need a new-line character. – Nick Bedford Feb 09 '10 at 00:05
  • 6
    Long time ago, in the days of Teletypes and typewriters, output machines had carriages that moved left to write as characters were printed. One command, `Carriage Return`, moved the carriage back to the left. Another command, `Linefeed`, advanced the paper to the next line. These two commands could be executed independently so that the paper advanced mid-line (using `Linefeed`) or rewriting the current line (using `Carriage Return`). As a pair, they cause the printing to start at the left margin of the next line. – Thomas Matthews Feb 09 '10 at 00:26
  • 9
    Unix people, being impatient typists, decided that the computer should handle both `Carriage Returns` and `Linefeeds`, improving productivity by typing less characters. This new command was called `Newline`. On some output systems you could see the carriage move left and the paper advance for each `Newline`, including blank lines. The C language decided to make peace and let the OS provide translations (without `ios::binary`) or provide no translations (with `ios::binary`). The `ios::out` determines data direction (*out from the computer*). – Thomas Matthews Feb 09 '10 at 00:32
  • thank u mr. Thomas so can u give me one difference between using ios::binary and ios::out | ios::binary for opening a file r nt they identical? – Alan_AI Feb 09 '10 at 01:22