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.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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
orstd::ifstream
), the openmodesios_base::in
andios_base::out
are specified by default. This means that this:is the same as
One would specify both options if they needed to also add some non-default openmodes such as
trunc
,ate
,app
, orbinary
. Theios_base::trunc
openmode is needed if you intend to create a new file for bidirectional I/O, because theios_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:This only refers to
std::fstream
. The above rules are not needed forstd::stringstream
.I hope these answer your questions. If you have any more, you can just ask in the comments.