本文介绍了std :: fstream不创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用std :: fstream作为io的文件,我想创建这个文件。 std :: fstream my_stream
my_stream.open(my_file_name,std :: fstream :: binary | std :: fstream :: in | std :: fstream :: out);
if(!my_stream)
std :: cout<<error<< strerror(errorno);
我得到这个结果:
没有这样的文件或目录。
如何在这种情况下创建文件?
解决方案
std :: fstream :: in
fstream :: open()。这是,强制它要求现有文件。
从模式
参数中删除 std :: fstream :: in
,或除其他标志外还指定 std :: fstream :: trunc
。
I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
I get this result: "No such file or directory."
How can I create the file in this case?
解决方案
You're specifying std::fstream::in
in your call to fstream::open(). This is known to force it to require an existing file.
Either remove std::fstream::in
from your mode
argument, or specify std::fstream::trunc
in addition to the other flags.
这篇关于std :: fstream不创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!