方法
1. access
函数
适用范围:所有
C/C++
项目
头文件:
#include < io.h>
函数原型:
intaccess(const char *filename, int mode);
函数说明:判断是否具有存取文件的权限
函数参数说明:
filename
:可以填写文件夹路径或者文件路径
mode
:
F_OK (
或
0):
判断该文件
/
文件夹是否存在;
R_OK (
或
2):
判断该文件
/
文件夹是否有读权限;
W_OK (
或
4):
判断该文件
/
文件夹是否有写入权限;
X_OK (
或
6):
判断该文件
/
文件夹是否有执行权限;
返回值:
若存在或者具有权限,返回值为
0
;不存在或者无权限,返回值为
-1
,并把错误代码存在
errno
中
(errno.h
中定义
)
。
错误代码:
EACCESS:
参数
pathname
所指定的文件不符合所要求测试的权限
.
EROFS:
欲测试写入权限的文件存在于只读文件系统内
.
EFAULT:
参数
pathname
指针超出可存取内存空间
.
EINVAL:
参数
mode
不正确
.
ENAMETOOLONG:
参数
pathname
太长
.
ENOTDIR:
参数
pathname
为一目录
.
ENOMEM:
核心内存不足
ELOOP:
参数
pathname
有过多符号连接问题
.
EIO I/O:
存取错误
.
|
#include <stdio.h>
#include <io.h>
int main(int argc, const char** argv)
{
char* filePath = "C://Users//Public//Downloads" ;
if (_access(filePath,0) == -1)
{
printf("The file/dir doesn't exisit");
}
else
{
printf("Exisit ");
}
return 0;
}
|
|
方法
2. fopen
函数
适用范围:所有
C/C++
项目
头文件:
#include
<stdio.h>
或
#include<fstream>
函数原型:
FILE *fopen(const char * path, const char * mode);
函数说明:判断是否具有存取文件的权限
函数参数说明:
filename
:可以填写文件夹路径或者文件路径
mode
:
r :
以只读方式打开文件,该文件必须存在。
r+ :
以可读写方式打开文件,该文件必须存在。
w :
打开只写文件,若文件存在则文件长度清为
0
,即该文件内容会消失。若文件不存在则建立该文件。
a:
以附加的方式打开只写文件。
b:
二进制文件。
返回值:
文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回
NULL
,并把错误代码存在
errno
中
(errno.h
中定义
)
。
用于判断文件是否存在可以使用
r
或者
rb
,返回值为
NULL
,说明打不开或不存在。但
用这种方法做出的判断是不完全正确的,因为有的文件存在,但是可能不可读
。
|
#include <stdio.h>
//#include<fstream>
int main(int argc, const char** argv)
{
char* filePath = "C://Users//Public//Downloads" ;
FILE *fp=NULL;
fp = fopen(filePath,"r");//
只供读取
if (fp == NULL)
{
printf("The file/dir doesn't exisit");
}
else
{
printf("Exisit ");
}
fclose(fp);//
关闭文件
fp=NULL;//
需要指向空,否则会指向原打开文件地址
return 0;
}
|
|
扩展
.
建一个文件夹
头文件:
#include
<
direct.h>
函数原型:
int _mkdir(const char *path,mode_t mode);
函数说明:建立一个文件夹
返回值:创建成功则返回
0
,否则返回
-1
方法
3. FindFirstFile
函数
适用范围:
windows
平台
头文件:
#include "windows.h"
函数原型:
__out HANDLEWINAPI FindFirstFileW(
__in LPCWSTR lpFileName,
__outLPWIN32_FIND_DATAW lpFindFileData);
或
__outHANDLE WINAPI FindFirstFileA(
__in LPCSTR lpFileName,
__outLPWIN32_FIND_DATAW lpFindFileData);
函数说明:检查文件或文件夹是否存在
|
#include <stdio.h>
#include "windows.h"
int main(int argc, const char** argv)
{
WIN32_FIND_DATA wfd ;
HANDLE hFind;
WCHAR *filePath = L"C://Users//Public//Downloads";
hFind = FindFirstFile(filePath, & wfd );
//
判断文件
if (hFind == INVALID_HANDLE_VALUE)
{ printf ("Invalid File Handle. Get Last Error reports %d ", GetLastError()); }
else
{
printf ("The first file found is %s ", wfd .cFileName);
FindClose(hFind);
}
//
判断文件夹
if (hFind != INVALID_HANDLE_VALUE &&(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
printf ("The first file found is %s ", wfd.cFileName);
FindClose(hFind);
}
else
{ printf ("Invalid File Handle. Get Last Error reports %d ", GetLastError()); }
return (0);
}
|
|
方法
4. PathFileExists
函数
适用范围:
MFC
项目
头文件:
#include <shlwapi.h>
函数原型:
LWSTDAPI_(BOOL) PathFileExistsA(__in LPCSTR pszPath);
或:
LWSTDAPI_(BOOL) PathFileExistsW(__in LPCWSTR pszPath);
参考
:
http://blog.csdn.net/roger_77/article/details/1538447
http://blog.csdn.net/gneveek/article/details/6848473
http://blog.csdn.net/xhhjin/article/details/6369336