Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between opening a file with ios::binary or ios::out or both?

Tags:

People also ask

What is the difference between ios :: in and ios :: out?

ios::in allows input (read operations) from a stream. ios::out allows output (write operations) to a stream.

What is ios :: binary used for?

ios::binaryOpens a file in binary mode. The default is text mode. You can combine these flags using a bitwise or operation. The binary flag, while portable, only has an effect on some non-UNIX systems, such as operating systems derived from MS-DOS, that have special conventions for storing end-of-line delimiters.

What is use of ios :: app mode in file opening?

Both ios::ate and ios::app place the file pointer at the end of the file when it is opened. The difference between the two is that ios::app lets you add data to the end of the file only while the ios::ate mode when opened with ofstream allows you to write data anywhere in the file even over old data.

What happens when we open a file in ios :: trunc mode and ios :: app mode?

ios::app (short for append) means that instead of overwriting the file from the beginning, all output operations are done at the end of the file. This is only meaningful if the file is also open for output. ios::trunc (short for truncate) means that when the file is opened, the old contents are immediately removed.


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().