I've just started wondering - how is actually std::fstream
opened with both std::ios::in
and std::ios::out
actually supposed to work? What should it do? Write something to (for example) an empty file, then read... what? Just written value? Where would the file "pointer"/"cursor" be? Maybe the answers already out there but I just couldn't have found it.
问题:
回答1:
What is std::fstream
?
std::fstream
is a bidirectional file stream class. That is, it provides an interface for both input and output for files. It is commonly used when a user needs to read from and write to the same external sequence.
When instantiating a bidirectional file stream (unlike std::ofstream
or std::ifstream
), the openmodes ios_base::in
and ios_base::out
are specified by default. This means that this:
std::fstream f("test.txt", std::ios_base::in | std::ios_base::out);
is the same as
std::fstream f("test.txt");
One would specify both options if they needed to also add some non-default openmodes such as trunc
, ate
, app
, or binary
. The ios_base::trunc
openmode is needed if you intend to create a new file for bidirectional I/O, because the ios_base::in
openmode disables the creation of a new file.
Bidirectional I/O
Bidirectional I/O is the utilization of a bidirectional stream for both input and output. In IOStreams, the standard streams maintain their character sequences in a buffer where it serves as a source or sink for data. For output streams, there is a "put" area (the buffer that holds characters for output). Likewise, for input streams, there is the "get" area.
In the case of std::fstream
(a class for both input and output), it holds a joint file buffer representing both the get and put area respectively. The position indicator that marks the current position in the file is affected by both input and output operations. As such, in order to perform I/O correctly on a bidirectional stream, there are certain rules you must follow:
- When you perform a read after a write or vice-versa, the stream should be repositioned back.
- If an input operation hit the end-of-file, performing a write directly thereafter is fine.
This only refers to std::fstream
. The above rules are not needed for std::stringstream
.
I hope these answer your questions. If you have any more, you can just ask in the comments.