是否在两个不同的fstream中打开SAME文件未定义的行为? [英] Is opening the SAME file in two different fstreams Undefined Behaviour?

查看:219
本文介绍了是否在两个不同的fstream中打开SAME文件未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个最近问到的问题提出了另一个有趣的问题,如和写入数据时:

 ifstream infile;
infile.open("accounts.txt");

ofstream outfile;
outfile.open("accounts.txt");
 

尽管该问题本身已成功解决,但提出了一个我无法找到权威答案的问题(并且我对Stack Overflow和更广泛的网络进行了相当广泛的搜索).

非常清楚地说明,调用已经与文件相关联的open()方法时会发生什么(可以解释所述代码中遇到的问题).

  • 两个流都保持打开状态,但在内部文件指针和缓冲区方面陷入了相互破坏"的局面.
  • 我们进入未定义(或实现定义)行为的领域.
  • 请注意,由于第一个open()调用是由输入流进行的,因此操作系统不一定会锁定"文件,就像对输出流一样.

    那么,有人对此有明确的答案吗?还是来自标准的引用(如果找不到更多权威,则引用将是可接受的")?

    解决方案

    basic_filebuf::open(以及依赖它的所有事物,例如fstream::open)都没有声明这种情况下会发生什么.文件系统可能允许也可能不允许.

    标准所说的是,如果文件成功打开,则可以根据界面进行播放.如果未成功打开,则将出现错误.也就是说,该标准允许文件系统允许或禁止它,但是它没有说明必须发生哪种情况.该实现甚至可以随机禁止它.或禁止您以任何方式打开任何文件.从理论上讲,所有这些都是有效的.

    This recently asked question has raised another interesting issue, as discussed in the comments to one of its answers.

    To summarize: the OP there was having issues with code like that below, when subsequently attempting to read and write data from/to the two streams 'concurrently':

    ifstream infile;
    infile.open("accounts.txt");
    
    ofstream outfile;
    outfile.open("accounts.txt");
    

    Although the issue, in itself, was successfully resolved, it has raised a question to which I cannot find an authoritative answer (and I've made some quite extensive searches of Stack Overflow and the wider web).

    It is very clearly stated what happens when calling the open() method of a stream that is already associated with a file (cppreference), but what I cannot find an answer to is what happens when (as in this case) the file is already associated with a (different) stream.

    If the stream is already associated with a file (i.e., it is already open), calling this function fails.

    I can see several possible scenarios here:

    1. The second open call will fail and any attempted writes to it will also fail (but that is not the case in the cited question).
    2. The second open call will 'override' the first, effectively closing it (this could explain the issues encountered in said code).
    3. Both streams remain open but enter into a 'mutual clobbering' match regarding their internal file pointers and buffers.
    4. We enter the realm of undefined (or implementation-defined) behaviour.

    Note that, as the first open() call is made by an input stream, the operating system will not necessarily 'lock' the file, as it probably would for an output stream.

    So, does anyone have a definitive answer to this? Or a citation from the Standard (cppreference will be 'acceptable' if nothing more authoritative can be found)?

    解决方案

    basic_filebuf::open (and all things that depend on it, like fstream::open) has no statement about what will happen in this case. A filesystem may allow it or it may not.

    What the standard says is that, if the file successfully opens, then you can play with it in accord with the interface. And if it doesn't successfully open, then there will be an error. That is, the standard allows a filesystem to permit it or forbid it, but it doesn't say which must happen. The implementation can even randomly forbid it. Or forbid you from opening any files in any way. All are (theoretically) valid.

    这篇关于是否在两个不同的fstream中打开SAME文件未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆