【发布时间】:2018-07-02 13:12:35
【问题描述】:
我有一个非常简单的程序
#include <iostream>
#include <fstream>
void CHECK(std::iostream& s)
{
std::cout << "good(): " << s.good()
<< " fail(): " << s.fail()
<< " bad(): " << s.bad()
<< " eof(): " << s.eof() << std::endl;
}
int main(int argc, const char * argv[])
{
std::fstream ofs("test.txt", std::ios::out | std::ios::trunc);
std::cout << "opened" << std::endl;
CHECK(ofs);
ofs << "Hello, World!\n";
CHECK(ofs);
ofs.close();
std::cout << "closed" << std::endl;
CHECK(ofs);
ofs << "Hello, World!\n";
std::cout << "after operation" << std::endl;
CHECK(ofs);
return 0;
}
libc++ 我得到以下最后一行:
good(): 1 fail(): 0 bad(): 0 eof(): 0
预期(或libstdc++):
good(): 0 fail(): 1 bad(): 1 eof(): 0
我已经在 OSX 上使用 Xcode 9.4.1(或在 Linux 上)进行了测试,但始终相同。谁能解释一下这里的情况?文件内容也没有更新,因为已经关闭。为什么关闭并进一步操作后流仍然良好?
【问题讨论】:
-
可能是一个错误?发布在 libc++ 列表中
-
您可以在bugs.llvm.org 提交针对 libc++ 的错误
-
好的,谢谢 - 我创建了一个ticket