论坛
门户
内部优惠
喜欢
话题
VIP会员
搜索
新浪微博
登录
注册
100%
100%
首页
>
软件开发
>
C/C++语言
>
C++基础:怎样判断某一文件是否存在
回复
« 返回列表
灯火互联
管理员
注册日期
2011-07-27
发帖数
41778
QQ
火币
41290枚
粉丝
1086
关注
100
加关注
写私信
打招呼
阅读:
3578
回复:
0
[C++技术]
C++基础:怎样判断某一文件是否存在
楼主
#
更多
只看楼主
倒序阅读
发布于:2011-12-27 11:56
保存
100%
100%
[]
1
很简单的一种办法:
#include <
ios
tream>
#include <fstream>
using namespace std;
#define FILENAME "stat.dat"
int main()
{
fstream _file;
_file.open(FILENAME,ios::in);
if(!_file)
{
cout<<FILENAME<<"没有被创建";
}
else
{
cout<<FILENAME<<"已经存在";
}
return 0;
}
另外一种利用 c 语言的库的办法:
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
程序例:
#include <stdio.h>
#include <io.h>
int file_exists(char *filename);
int main(void)
{
printf("Does NOTEXIST.FIL exist: %s\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-检查文件是否可读/写访问
喜欢
0
评分
0
最新喜欢:
淘宝天猫隐藏优惠券地址
回复
100%
发帖
回复
« 返回列表
普通帖
您需要登录后才可以回帖,
登录
或者
注册
100%
返回顶部
关闭
最新喜欢