What's the Difference Between Opening a File with iOS::Binary or iOS::Out or Both

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

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.

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

thanks for all people who answered me:
i now tested several codes depending on what i have been answered and came up with this summary:

using ofstream:
ios::out is the default even if nothing is specified, but if you used only ios::in with ofstream, no compilation errors (unless you use read() or >> or some ifstream object) but no files would be written.

using ifstream:
ios::in is the default even if nothing is specified, but if you used only ios::out with ifstream, no compilation errors (unless you use write() or << or some ofstream object) but you can't read any information from the file.

using fstream:
no defaults, you have to explicitly determine what you are going to do. Otherwise, no compilation error but you don't get what you want simply.

as for the original question , both work exactly the same!

What is ios::in|ios::out?

  • ios::in allows input (read operations) from a stream.
  • ios::out allows output (write operations) to a stream.
  • | (bitwise OR operator) is used to combine the two ios flags,

    meaning that passing ios::in | ios::out to the constructor

    of std::fstream enables both input and output for the stream.

Important things to note:

  • std::ifstream automatically has the ios::in flag set.
  • std::ofstream automatically has the ios::out flag set.
  • std::fstream has neither ios::in or ios::out automatically

    set. That's why they're explicitly set in your example code.

C++ Filehandling: Difference between ios::app and ios::ate?

It’s the other way around. When ios::ate is set, the initial position will be the end of the file, but you are free to seek thereafter. When ios::app is set, all output operations are performed at the end of the file. Since all writes are implicitly preceded by seeks, there is no way to write elsewhere.

Difference between opening a file in binary vs text

The link you gave does actually describe the differences, but it's buried at the bottom of the page:

http://www.cplusplus.com/reference/cstdio/fopen/

Text files are files containing sequences of lines of text. Depending on the environment where the application runs, some special character conversion may occur in input/output operations in text mode to adapt them to a system-specific text file format. Although on some environments no conversions occur and both text files and binary files are treated the same way, using the appropriate mode improves portability.

The conversion could be to normalize \r\n to \n (or vice-versa), or maybe ignoring characters beyond 0x7F (a-la 'text mode' in FTP). Personally I'd open everything in binary-mode and use a good Unicode or other text-encoding library for dealing with text.

Difference in using read/write when stream is opened with/without ios::binary mode

The only difference between binary and text mode is how the '\n' character is treated.

In binary mode there is no translation.

In text mode \n is translated on write into a the end of line sequence.

In text mode end of line sequence is translated on read into \n.

The end of line sequence is platform dependant.

Examples:

ASCII based systems:

LF    ('\0x0A'):      Multics, Mac OS X, BeOS, Amiga, RISC OS
CRLF ('\0x0D\0x0A'): Microsoft Windows, DEC TOPS-10, RT-11
CR: ('\0x0D'): TRS-80, Mac OS Pre X
RS: ('\0x1E'): QNX pre-POSIX implementation.

Is there a difference between ifstream::binary and ios::binary?

There's no difference. These names are inherited from the virtual base std::ios_base from which the concrete stream classes derive.



Related Topics



Leave a reply



Submit