c判断文件夹存在
① C/C++如何判断一个文件夹是否存在
方法一:access函数判断文件夹或者文件是否存在
函数原型: int access(const char *filename, int mode);
所属头文件:io.h
filename:可以填写文件夹路径或者文件路径
mode:0 (F_OK) 只判断是否存在
2 (R_OK) 判断写入权限
4 (W_OK) 判断读取权限
6 (X_OK) 判断执行权限
用于判断文件夹是否存在的时候,mode取0,判断文件是否存在的时候,mode可以取0、2、4、6。 若存在或者具有权限,返回值为0;不存在或者无权限,返回值为-1。
错误代码
EACCESS 参数pathname 所指定的文件不符合所要求测试的权限。
EROFS 欲测试写入权限的文件存在于只读文件系统内。
EFAULT 参数pathname指针超出可存取内存空间。
EINVAL 参数mode 不正确。
ENAMETOOLONG 参数pathname太长。
ENOTDIR 参数pathname为一目录。
ENOMEM 核心内存不足
ELOOP 参数pathname有过多符号连接问题。
EIO I/O 存取错误。
特别提醒:使用access()作用户认证方面的判断要特别小心,例如在access()后再做open()的空文件可能会造成系统安全上的问题。
实例:
#include <stdio.h>
#include <io.h>
int main(void)
{
if ( !access("C://windows",0) )
puts("C://windows EXISITS!");
else
puts("C://windows DOESN'T EXISIT!");
return 0;
}
方法二:fopen函数判断文件是否存在
函数原型:FILE *fopen (char *filename, char *type);
filename:文件路径
type:打开文件的方式(有r、w、r+、w+、a、rb、wb等等)
用于判断文件是否存在可以使用 r 或者 rb ,因为使用 其它方式的话,可能会自动建立文件。 返回值为NULL(打不开)和正数(能打开)。
特别提醒:用这种方法做出的判断是不完全正确的,因为有的文件存在,但是可能不可读。
② 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
③ c语言怎么查找制定目录下的文件是否存在
C语言中用OPEN函数就可以判断出指定目录下的文件是否存在。
比如:
#include<stdio.h>
main()
{
FILE *fp;
if((fp=fopen("c:\\filechk.txt","r"))==NULL)printf("this file is not exist";//文件不存在
else
printf("Open sucess");
close(fp);
}
④ 在C++中如何判断文件夹是否存在,不存在的话创建文件夹
参考代码如下:
#include <stdio.h>
#include <direct.h>
#include <stdlib.h>
#include <memory>
//检查文件夹是否存在,不存在则创建之
//文件夹存在返回 0
//文件夹创建失败返回-1
//文件夹创建失败返回1
int CheckDir(char* Dir)
{
FILE *fp = NULL;
char TempDir[200];
memset(TempDir,'\0',sizeof(TempDir));
sprintf(TempDir,Dir);
strcat(TempDir,"\\");
strcat(TempDir,".temp.fortest");
fp = fopen(TempDir,"w");
if (!fp)
{
if(_mkdir(Dir)==0)
{
return 1;//文件夹创建成功
}
else
{
return -1;//can not make a dir;
}
}
else
{
fclose(fp);
}
return 0;
}
⑤ 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