C ++文件流打开模式歧义 [英] C++ file stream open modes ambiguity
问题描述
$ b $ ul
将文件与流对象相关联的过程称为打开文件。
当打开一个文件时,我们可以指定打开文件的模式。
我的查询与 ios :: out
和 ios:in
模式有关。
创建一个 ofstream
对象并用 ios :: in $ c $打开文件c>模式,我能
写入文件,但只有当它已经创建(与 ios :: out
模式文件也创建,如果它不'已经存在)。
但是,当我创建 ifstream
对象并用 ios :: out
模式打开文件时,我可以从文件中读取。
我的问题是为什么这些模式( ios :: in
/ <当流的类型( ifstream
/ )被语言提供时, ofstream
)本身指定正在执行哪种类型的操作(输入/输出)?
为什么这个模糊的用法( 根据标准打开和
ifstream
with > ios :: out
)在一种情况下工作,并在另一种情况下失败(但只有在文件不存在的情况下)? $ ,
ifstream
和 fstream
类是下级 filebuf
的高级接口,可以通过 rdbuf()
流的成员函数。
ofstream
与某些模式 mode
,它会打开下划线的流缓冲区,如 mode |的ios_base ::出
。类似地 ifstream
使用 mode |的ios_base ::在
。 fstream
将模式
参数逐字传递给下划线的流缓冲区。
fstream f( a.txt,ios_base :: in | ios_base :: out);
ifstream g(a.txt,ios_base :: out);
ofstream h(a.txt,ios_base :: in);
在这些行之后,您可以使用 f.rdbuf )
, g.rdbuf()
和 h.rdbuf()
三个行为就好像你用C调用 fopen(a.txt,r +)
打开文件,它给你读/写权限,不截断文件,如果文件不存在则失败。
那么,为什么我们有三个不同的类?正如我已经说过的那样,这些高层次的类提供了低层流缓冲区的高级接口。这个想法是, ifstream
具有输入成员函数(如 read()
), ofstream
具有用于输出的成员函数(如 write()
),而 fstream
同时具有。例如,你不能这样做:
g.write(abc,3); //错误:g没有写函数
但是这个工作,因为虽然 g
是 ifstream
,我们用 ios_base :: out
:
g.rdbuf() - > sputn(abc,3); //我们仍然有写权限
For performing file IO in C++ we use the ofstream, ifstream and fstream classes.
- ofstream: Stream class to write on files
- ifstream: Stream class to read from files
- fstream: Stream class to both read and write from/to files
The process of associating a file with a stream object is called "opening the file".
When opening a file we can specify the mode in which the file is to be opened.
My query is related to the ios::out
and ios:in
modes.
When I create an ofstream
object and open the file with ios::in
mode, I am able to
write into the file but only if its created already(with ios::out
mode file is also created if it doesn't already exist).
But when I create ifstream
object and open the file with ios::out
mode, I am able to read from the file.
My question is why these modes (ios::in
/ios::out
) are supplied by the language when the type of the stream(ifstream
/ofstream
) itself specifies as to which type of operation(input/output) is being performed ?
Also why this ambiguous usage(ofstream
with ios::in
and ifstream
with ios::out
) works in one case and fails(though only if file is not already present) in another ?
The ofstream
, ifstream
and fstream
classes are high level interfaces for the underling filebuf
, which one can get through the rdbuf()
member function of the stream.
According to the standard when you open an ofstream
with some mode mode
, it opens the underlining stream buffer as with mode | ios_base::out
. Analogously ifstream
uses mode | ios_base::in
. fstream
passes the mode
parameter verbatim to the underlining stream buffer.
What the above implies is that the following code opens the file with exactly the same open flags:
fstream f("a.txt", ios_base::in | ios_base::out);
ifstream g("a.txt", ios_base::out);
ofstream h("a.txt", ios_base::in);
After these lines you can do exactly the same things with f.rdbuf()
, g.rdbuf()
and h.rdbuf()
, and all the three act as if you opened the file with the C call fopen("a.txt", "r+")
, which gives you read/write access, does not truncate the file, and fails if the file does not exist.
So, why do we have three different classes? As I've already said, these are high level classes providing a high-level interface over the lower-level stream buffer. The idea is that ifstream
has member functions for input (like read()
), ofstream
has member functions for output (like write()
) while fstream
has both. For example you cannot do this:
g.write("abc", 3); // error: g does not have a write function
But this works, because, although g
is an ifstream
, we had opened it with ios_base::out
:
g.rdbuf()->sputn("abc", 3); // we still have write access
这篇关于C ++文件流打开模式歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!