Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in C++ files: what a file opened as an ios::binary differs from one opened as ios::binary | ios::out?

Tags:

c++

fstream

if i opened a file like:

ofstream file("file.dat",ios::binary);

or

ofstream file("file.dat",ios::binary | ios::out);

what can i do with a file opened in the latter form that i can't do with the former form and vice versa

thank you

like image 227
Alan_AI Avatar asked Feb 09 '10 17:02

Alan_AI


People also ask

What does ios :: binary mean in C++?

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.

When a file is opened in ios :: out mode it is automatically opened using which mode?

8. Which of the following is the default mode of the opening using the ofstream class? Explanation: By default, the file is opened in ios::out mode if the file object we are using is of ofstream class. 9.


2 Answers

For an ofstream, ios::out is the default, so there's no difference. I believe the only time specifying ios::out makes a difference is if you use an fstream, which can be opened for reading or writing, or both.

like image 80
Jerry Coffin Avatar answered Oct 15 '22 17:10

Jerry Coffin


In most cases I would expect there to be no difference, though it seems like this could technically be implementation specific.

In my implementation (gcc 3.4.3) the open for the ofstream uses the ios:::out mode in the ofstream->open() call regardless of what is specified via the constructor so it's purely optional. If using fstream, this is not the case and would need to be specified explicitly.

like image 45
RC. Avatar answered Oct 15 '22 18:10

RC.