2013-10-09 56 views
0

我试图打开一个文件进行读操作:fstream的不打开文件

cout << file_path << endl; 
fstream myfile(file_path.c_str(), std::ios_base::in); 
if (myfile.is_open()) 
    cout << "Yahhy!! file Opend successfully" << endl; 
else 
    cout << "Wohoo!! Wrong path" << endl; 

输出是:

/home/umar/Desktop/DateFile/dha_dataset.gfp 

Wohoo!! Wrong path 

的路径是正确的 - 该文件存在,但它不开放文件。我的代码或其他地方存在问题吗?

我正在Ubuntu工作。

+1

可能你没有权限阅读? – codeling

+0

创建一个新文件来测试。 – DogDog

+0

@nyarlathotep我有权限 – OOkhan

回答

3

的语法看起来是正确的,这会使两种选择:

无论是文件不存在,或者您没有读取权限。

同时检查从终端执行以下命令:

ls -l /home/umar/Desktop/DateFile/dha_dataset.gfp 

看结果,他们应该是这个样子:

ls: cannot access /home/umar/Desktop/DateFile/dha_dataset.gfp: No such file or directory 

或:

-rw-rw-rw xxx yyy 1025 Oct 9 2013 dha_dataset.gfp 

的第一个结果意味着路径不正确。

第二个结果表示路径是正确的,所以看看权限。如果您没有读取权限...(r之一),那么您将无法打开文件。

+0

我做了你说的与困惑的答案。查看 umar @ umar-Machine:〜/ Desktop/DateFile $ ls -l /home/umar/Desktop/DateFile/dha_dataset.gfp ls:无法访问/ home /umar/Desktop/DateFile/dha_dataset.gfp:没有这样的文件或目录 umar @ umar-Machine:〜/ Desktop/DateFile $ ls dha_dataset.gfp umar @ umar-Machine:〜/ Desktop/DateFile $ pwd /home/umar/Desktop/DateFile umar @ umar-Machine:〜/ Desktop/DateFile $ – OOkhan

+0

请将此添加到您的问题并正确格式化!像这样它是不可读的。看起来像是该文件不存在,或者没有权限;) – codeling

+0

@ 100khan:看起来像路径是错误的,然后。 – Andrew