如何检查文件是否存在并且在C ++中可读? [英] How to check if a file exists and is readable in C++?

查看:147
本文介绍了如何检查文件是否存在并且在C ++中可读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个fstream my_file(test.txt),但我不知道test.txt是否存在。如果它存在,我想知道,如果我可以读它,也。


解决方案

/ div>

我可能会用:

  ifstream my_file(test.txt); 
if(my_file.good())
{
//读取
}

方法检查该流是否可以从中读取。


I've got a fstream my_file("test.txt"), but I don't know if test.txt exists. In case it exists, I would like to know if I can read it, too. How to do that?

I use Linux.

解决方案

I would probably go with:

ifstream my_file("test.txt");
if (my_file.good())
{
  // read away
}

The good method checks if the stream is ready to be read from.

这篇关于如何检查文件是否存在并且在C ++中可读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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