无法打开文件 [英] fail to open the file

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

问题描述



我想知道为什么我无法打开或执行

文件....如何在c ++中完成?

ifsteam in(somefile);


if(!in)


//为什么?

谢谢


I would like to find out why exactly I wasn''t able to open or execute
the file.... how is it done in c++?
ifsteam in("somefile");

if(!in)

//why?
Thanks

推荐答案

该代码应该可以工作。

但你应该注意一些要点:

1.确保文件存在,并且在你的

程序的当前目录中。

如果你正在使用调试器它可能是项目的工作目录。

2.确保该文件未被其他应用程序使用。


如果你选中此项,尝试使用ios :: in和

ios :: nocreate标志打开文件,并在调用后检查ios :: failbit标志。如果它设置了
,则发生以下三种情况之一:找不到文件,

filebuf对象已经附加到打开文件或filebuf调用

失败。

无论如何,如果您使用的是Windows,请尝试使用win32api CreateFile或

OpenFile来获取扩展错误报告。


以后的错误,请看看函数的文档,以便找出错误的原因如何报错。

that code should work.
but you should pay attention to some points:
1. make sure the file exists, and is in the current directory of your
program.
if you are using a debugger it might be a working dir of the project.
2. make sure the file is not in use by another application.

if you checked this, try to open the file using ios::in and
ios::nocreate flags and after the call check ios::failbit flag. if it
is set, one of 3 things have happened: the file is not found, the
filebuf object already attached to an open file or a filebuf call
fails.
anyway, if you are using windows, try the win32api CreateFile or
OpenFile to get an extended error report.

for future errors, try look at the documentation of the function to
find out how errors are reported the the reasons for the errors.



gu*****@gmail.com 写道:
代码应该有效。
但是你应该注意以下几点:
1.确保文件存在,并且在你的
程序的当前目录中。
如果您使用的是调试器,它可能是项目的工作目录。
2.确保该文件未被其他应用程序使用。

如果您选中此项,尝试使用ios :: in和
打开文件os :: nocreate标志和调用后检查ios :: failbit标志。如果设置好了,就会发生以下三种情况之一:找不到文件,
filebuf对象已经附加到打开文件或者文件调用
失败。
无论如何,如果您正在使用Windows,请尝试使用win32api CreateFile或
OpenFile来获取扩展错误报告。

以备将来出现错误,请尝试查看该功能的文档以查找
如何报告错误的错误原因。
that code should work.
but you should pay attention to some points:
1. make sure the file exists, and is in the current directory of your
program.
if you are using a debugger it might be a working dir of the project.
2. make sure the file is not in use by another application.

if you checked this, try to open the file using ios::in and
ios::nocreate flags and after the call check ios::failbit flag. if it
is set, one of 3 things have happened: the file is not found, the
filebuf object already attached to an open file or a filebuf call
fails.
anyway, if you are using windows, try the win32api CreateFile or
OpenFile to get an extended error report.

for future errors, try look at the documentation of the function to
find out how errors are reported the the reasons for the errors.



我在unix下工作并需要检查两次失败(需要

确切知道哪些错误一个发生了):许可被拒绝或文件没有

存在。


怎么会这样做?

我'我很确定它已被std覆盖了。

谢谢


I am working under unix and need to check for two failures (need to
know exactly which one took place): permission denied or file doesn''t
exist.

How would do that?
I''m pretty sure it is covered in the std.
Thanks


>
I我在unix下工作,需要检查两次失败(需要确切知道发生了哪一次失败):权限被拒绝或文件不存在。
I am working under unix and need to check for two failures (need to
know exactly which one took place): permission denied or file doesn''t
exist.



1.在尝试打开具有访问权限的文件之前检查权限

(在shell上输入man 2 access以获取更多信息)


1. Checking the permissions berfore trying to open the file with access
(type man 2 access on your shell for more information)

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

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