fstream不会创建文件 [英] fstream won't create a file
问题描述
我只是想创建一个文本文件,如果它不存在,我似乎不能得到 fstream
这样做。
I'm simply trying to create a text file if it does not exist and I can't seem to get fstream
to do this.
#include <fstream>
using std::fstream;
int main(int argc, char *argv[]) {
fstream file;
file.open("test.txt");
file << "test";
file.close();
}
我需要指定 open )
函数为了得到它创建文件?我已经阅读,你不能指定 ios :: in
,因为将期望一个已经存在的文件,但我不确定如果其他参数需要
Do I need to specify anything in the open()
function in order to get it to create the file? I've read that you can't specify ios::in
as that will expect an already existing file to be there, but I'm unsure if other parameters need to be specified for a file that does not already exist.
推荐答案
您应该添加fstream :: out打开方法,如下所示:
You should add fstream::out to open method like this:
file.open("test.txt",fstream::out);
有关fstream标志的更多信息,请查看以下链接: http://www.cplusplus.com/reference/fstream/fstream/open/
More information about fstream flags, check out this link: http://www.cplusplus.com/reference/fstream/fstream/open/
这篇关于fstream不会创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!