提问人:Debanjan Mondal 提问时间:1/28/2021 最后编辑:Debanjan Mondal 更新时间:3/3/2021 访问量:405
当我们在不打开文件的情况下写入文件时,里面会发生什么?
What happens inside when we write to a file without opening it?
问:
例如,如果我写入一个文件,而该文件不在那里,我<<给定的要写入的字符串去了那里?
int main(int argc, char** argv) {
fstream my_file;
if (argc>1)
{
my_file.open("my_file.txt", ios::out);
cout << "File1 created successfully!"<<endl;
}
my_file << "Guru99";
my_file.close();
cout<<"Done"<<endl;
return 0; }
它会把它放在内存中还是放在 dev/null 类型的区域?
答:
0赞
vitaut
3/3/2021
#1
如果您尝试写入未与文件关联(未打开)的对象,则您的输出将被丢弃,并将设置:fstream
failbit
如果无法生成输出,请设置 failbit,如果在此流的异常掩码中启用了 failbit 上的异常,则抛出 ios_base::failure。
如 https://en.cppreference.com/w/cpp/named_req/FormattedOutputFunction 所述。
评论
"it does not give any error"
my_file.fail()
true
my_file.is_open()
false