无法打开流 [英] failed to open a stream

查看:89
本文介绍了无法打开流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






请帮忙。


std :: fstream iofs(f.c_str (),std :: ios :: in | std :: ios :: out);

std :: cout<< f<< ''\ n''<< iofs.is_open()<< std :: endl;


推出

*********************** *************************** **************

pair_status / myPair

0

******************************* ******************* **************


而文件不是创建,为什么is_open()报告失败?

如果除了我已经完成的
之外我怎么能创建文件?
<很多,谢谢


Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << ''\n'' << iofs.is_open() << std::endl;

puts out
************************************************** **************
pair_status/myPair
0
************************************************** **************

and the file is not created, why is_open() is reporting a failure?
how can I create the file if it is not there besides what I have
already done?

many thanks

推荐答案

Gary Wessle写道:
Gary Wessle wrote:




请帮忙。


std :: fstream iofs(f.c_str(),std :: ios :: in | std :: ios :: out);

std :: cout<< f<< ''\ n''<< iofs.is_open()<< std :: endl;


推出

*********************** *************************** **************

pair_status / myPair

0

******************************* ******************* **************


而文件不是创建,为什么is_open()报告失败?

如果除了我已经完成的
之外我怎么能创建文件?


非常感谢
Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << ''\n'' << iofs.is_open() << std::endl;

puts out
************************************************** **************
pair_status/myPair
0
************************************************** **************

and the file is not created, why is_open() is reporting a failure?
how can I create the file if it is not there besides what I have
already done?

many thanks



std :: fstream iofs(f.c_str(),std :: ios :: out | std :: ios: :app);

if(iofs)

{

iofs.close();

iofs.clear ();

iofs.open(f.c_str(),std :: ios :: in | std :: ios:out);

}


if(!iofs)

{

//创建或打开失败。

}

std::fstream iofs( f.c_str(), std::ios::out | std::ios::app );
if ( iofs )
{
iofs.close();
iofs.clear();
iofs.open( f.c_str(), std::ios::in|std::ios:out );
}

if ( !iofs )
{
// create or open failed.
}


Gary Wessle写道:
Gary Wessle wrote:




请帮帮忙。


std :: fstream iofs(f.c_str(),std :: io s :: in | std :: ios :: out);

std :: cout<< f<< ''\ n''<< iofs.is_open()<< std :: endl;


推出

*********************** *************************** **************

pair_status / myPair

0

******************************* ******************* **************


而文件不是创建,为什么is_open()报告失败?

如果除了我已经完成的
之外我怎么能创建文件?
<非常感谢
非常感谢

Hi

please help with this.

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
std::cout << f << ''\n'' << iofs.is_open() << std::endl;

puts out
************************************************** **************
pair_status/myPair
0
************************************************** **************

and the file is not created, why is_open() is reporting a failure?
how can I create the file if it is not there besides what I have
already done?

many thanks



您真的需要阅读本手册。我会推荐一个

http://www.dinkumware.com/manuals/de..._filebuf::open


非常明确不同的标志意味着他们做了什么。


在你的情况下std :: ios :: in | std :: ios :: out意味着打开

阅读和写作。它不会创建文件,如果文件不存在,它会报告失败

。这正是你所看到的。


我可以;记住你正在尝试做什么,但我想你已经选择了一些东西不可能一步到位。您必须尝试使用​​一组标志打开
然后如果失败尝试不同的

设置。类似


std :: fstream iofs(f.c_str(),std :: ios :: in | std :: ios :: out);

if(iofs.is_open())

{

iofs.open(f.c_str(),ios_base :: in | ios_base :: out |

ios_base :: app);

}


john

You really need to read the manual. I''d recommend this one

http://www.dinkumware.com/manuals/de..._filebuf::open

it very clear about what the different flags mean an what they do.

In your case std::ios::in|std::ios::out means open existing file for
reading and writing. It does not create a file, and it reports a failure
if the file does not exist. Which is exactly what you are seeing.

I can;t remember exactly what you are trying to do but I think you''ve
picked something that cannot be accomplished in one step. You must try
to open with one set of flags and then if that fails try a different
set. Something like

std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}

john


typo
typo

>

std :: fstream iofs(f.c_str(),std :: ios :: in | std :: ios :: out);

if(iofs.is_open())
>
std::fstream iofs( f.c_str(), std::ios::in|std::ios::out );
if (iofs.is_open())



if(!iofs.is_open())

if (!iofs.is_open())


{

iofs.open(f.c_str(),ios_base :: in | ios_base :: out |

ios_base :: app);

}


john
{
iofs.open( f.c_str(), ios_base::in | ios_base::out |
ios_base::app );
}

john


这篇关于无法打开流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆