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 twoios
flags,
meaning that passingios::in | ios::out
to the constructor
ofstd::fstream
enables both input and output for the stream.
Important things to note:
std::ifstream
automatically has theios::in
flag set.std::ofstream
automatically has theios::out
flag set.std::fstream
has neitherios::in
orios::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
Critique My Non-Intrusive Heap Debugger
How to Restart My Own Qt Application
Detect Gcc Compile-Time Flags of a Binary
Write a File in a Specific Path in C++
Can a C++ Compiler Re-Order Elements in a Struct
Get Sum of Values Stored in _M256D with Sse/Avx
Is a Logical Right Shift by a Power of 2 Faster in Avr
Problems with Move Constructor and Move Overloaded Assignment Operator
Where Are the Man Pages for C++
How to Declare an Array of Objects Whose Class Has No Default Constructor
Call Destructor and Then Constructor (Resetting an Object)
Stl Containers Element Destruction Order
Is It Ok to Use C-Style Cast for Built-In Types
Auto' Specifier Type Deduction for References