tags:

views:

109

answers:

3

hello 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,i.e. in all cases ,the same output on the file is produced using either fileName<< or fileName.write()

so please tell me what is the real difference between them, i'm really confused!

THANKS

+2  A: 

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
and what does it mean to use both?
Ala ABUDEEB
Well, you'd be writing to a file without translating any characters.
Nick Bedford
A: 

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.

anon
but what are CLRF pairs?
Ala ABUDEEB
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
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
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
thank u mr. Thomasso can u give me one difference between usingios::binaryandios::out | ios::binaryfor opening a filer nt they identical?
Ala ABUDEEB
A: 

please make it clearer

Ala ABUDEEB
Please use comments to request further explanation. This is not an answer.
Nick Bedford
SORRYI'M NEW TO STACKOVERFLOW.COM
Ala ABUDEEB