C 判断文件或文件夹是否存在

如题所述

C/C++中判断某一文件或目录是否存在
1.C++很简单的一种办法:#include<iostream#include<fstreamusingnamespacestd;#defineFILENAME"stat.dat"intmain(){fstream_file;
_file.open(FILENAME,ios::in);if(!_file){cout<<FILENAME<<"没有被创建";}else{cout<<FILENAME<<"已经存在";}return0;}
2.利用 c 语言的库的办法:
函数名: access功能: 确定文件的访问权限用法: int access(const char *filename, intamode);
以前一直没用过这个函数,今天调试程序发现了这个函数,感觉挺好用,尤其是判断一个文件或文件夹是否存在的时候,用不着再find了,文件的话还可以检测读写权限,文件夹的话则只能判断是否存在,下面摘自MSDN:int_access(constchar*path,
intmode);Return Value
Each of these functions returns 0 if the file has the given
mode. The function returns –1 if the named file does not exist or
is not accessible in the given mode; in this case,errnois set as follows:EACCESAccess denied: file’s permission setting does not
allow specified access.
ENOENTFilename or path not found.
ParameterspathFile or directory pathmodePermission settingRemarksWhen used with files, the_accessfunctiondetermines whether the specified file exists and can be accessed as
specified by the value ofmode
. When used with
directories,
_accessdetermines only whether the
specified directory exists; in Windows NT, all directories have
read and write access.
modeValue
Checks File For00
Existence only02
Write permission04
Read permission06
Read and write permissionExample#include<io.h#include<stdio.h#include<stdlib.hvoidmain(void){if((_access("ACCESS.C",0))!=-1){printf("FileACCESS.C
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网