我在使用fstream
和oftream
时发现它们的行为不同,但是我无法解释。
当我使用fstream
时,什么都没有发生,也就是说没有文件被创建:
int main()
{
std::fstream file("myfile.txt");
file << "some text" << std::endl;
return 0;
}
但是当我将fstream
更改为ofstream
时,它起作用了。
为什么?
fstream
CTOR的第二个参数是ios_base::openmode mode = ios_base::in | ios_base::out
,这让我觉得文件以读写模式打开,对吗?
if (!file) cout <<"错误";
。你尝试过使用std::ofstream file(...)
吗? - Bob__main
函数。 - Martin Bonner supports Monica