C ++文件流打开模式歧义

编程入门 行业动态 更新时间:2024-10-27 04:25:14
本文介绍了C ++文件流打开模式歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 为了在C ++中执行文件IO,我们使用了ofstream,ifstream和fstream类。 $ b $ ul
  • ofstream:Stream class to write on files <
  • ifstream:要从文件中读取的流类
  • fstream:要从文件读取和写入的流类
  • 将文件与流对象相关联的过程称为打开文件。 当打开一个文件时,我们可以指定打开文件的模式。 我的查询与 ios :: out 和 ios:in 模式有关。

    创建一个 ofstream 对象并用 ios :: in 模式,我能写入文件,但只有当它已经创建(与 ios :: out 模式文件也创建,如果它不'已经存在)。 但是,当我创建 ifstream 对象并用 ios :: out 模式打开文件时,我可以从文件中读取。

    我的问题是为什么这些模式( ios :: in / <当流的类型( ifstream / )被语言提供时, ofstream )本身指定正在执行哪种类型的操作(输入/输出)?

    为什么这个模糊的用法( 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 ++文件流打开模式歧义

    本文发布于:2023-10-08 13:59:53,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:歧义   模式   文件

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!