std :: fstream如何同时进出? [英] How is std::fstream with both in and out supposed to work?
问题描述
我刚开始想知道 - 实际上 std :: fstream
是如何用 std :: ios :: in $ c打开的$ c>和
std :: ios :: out
实际上应该有效吗?它该怎么办?写一些东西(例如)一个空文件,然后读......什么?刚写的价值?文件指针/光标在哪里?也许答案已经在那里,但我无法找到它。
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.
推荐答案
什么是 std :: fstream
?
std :: fstream
是双向文件流类。也就是说,它为文件的输入和输出提供了一个接口。当用户需要读取和写入相同的外部序列时,通常会使用它。
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.
实例化双向文件流时(与 std不同: :ofstream
或 std :: ifstream
),openmodes ios_base :: in
和 ios_base :: out
默认指定 。这意味着:
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);
与
std::fstream f("test.txt");
如果他们还需要添加一些非默认的开放模式,例如<$ c,则可以指定这两个选项$ c> trunc , ate
, app
或二进制
。如果您打算创建新的双向I / O文件,则需要 ios_base :: trunc
openmode,因为 ios_base :: in
openmode禁止创建新文件。
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.
双向I / O是对输入和输出使用双向流。在IOStreams中,标准流将其字符序列保存在缓冲区中,在缓冲区中,它用作数据的源或接收器。对于输出流,有一个put区域(保存输出字符的缓冲区)。同样,对于输入流,还有获取区域。
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.
在 std :: fstream
的情况下(输入和输出的类),它包含一个联合文件缓冲区,分别代表get和put区域。标记文件中当前位置的位置指示器受输入和输出操作的影响。因此,为了在双向流上正确执行I / O,必须遵循一些规则:
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:
- 当您执行在写入后读取,反之亦然,应该重新定位流。
- 如果输入操作命中文件结尾,则此后直接执行写操作正常。
这仅指 std :: fstream
。 std :: stringstream
不需要上述规则。
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.
这篇关于std :: fstream如何同时进出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!