C语言编程之怎样判断某一文件是否存在

很简单的一种办法:
#include
#include
using namespace std;
#define FILENAME “stat.dat”
int main()
{
fstream _file;
_file.open(FILENAME,ios::in);
if(!_file)
{
cout
}
else
{
cout
}
return 0;
}
另外一种利用 c 语言的库的办法:
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
程序例:
#include
#include
int file_exists(char *filename);
int main(void)
{
printf(“Does NOTEXIST.FIL exist: %\n”,
file_exists(“NOTEXISTS.FIL”) ? “YES” : “NO”);
return 0;
}
int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}
access(filename, 0)0 表示判断文件是否存在
finename 文件名称 mode 模式,共5种模式:
0-检查文件是否存在
1-检查文件是否可运行
2-检查文件是否可写访问
4-检查文件是否可读访问
6-检查文件是否可读/写访问
~end~

文章最后

怎么快速学C/C++,有什么方法,打算深入了解这个行业的朋友,可以加C/C++学习群:648778840不管你是小白还是大牛,小编我都欢迎,不定期分享干货,包括小编自己整理的一份2019最新的C/C++资料和0基础入门教程,欢迎初学和进阶中的小伙伴。

每天晚上20:00我都会开直播给大家分享C/C++编程学习知识和路线方法,群里会不定期更新最新的教程和学习方法,大家都是学习C/C++的,或是转行,或是大学生,还有工作中想提升自己能力的前端党,如果你是正在学习C/C++的小伙伴可以加入学习。最后祝所有程序员都能够走上人生巅峰,让代码将梦想照进现实,非常适合新手学习,有不懂的问题可以随时问我,工作不忙的时候希望可以给大家解惑。

猜你喜欢

转载自blog.csdn.net/chengxuyuanbawei/article/details/89522951