c如何判断一个文件|C语言判断一个文件是否存在

『壹』 C/C++如何判断一个文件夹是否存在

方法一:access函数判断文件夹或者文件是否存在函数原型: int access(const char *filename, int mode);所属头文件:io.hfilename:可以填写文件夹路径或者文件路径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 里,怎么判断是否存在某个文件

如果你知道文件名和路径的话; 直接用 fopen 是C的标准库函数。 参数为FILE * fopen(const char * path,const char * mode); 路径为完整路径, mode 为打开方式, 是只读方式打开,还是读写方式打开这些。如果你只知道文件名, 不知道路径, 我猜你想要个类似于windows 里面的 搜索的。。。 这个说来话长了。。再或者, 你知道路径, 不知道文件名?那个, 要用系统的API 来干这个事情了。不用系统级的高级函数, 搞不定了。因为这个是整个文件系统的事情了。。

『叁』 C语言,判断一个文件是否存在

可以使用库函抄数 access,该函数袭声明于头文件 io.h,函数原型为:int access(const char *filename, int amode);amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。这个函数还可以检查其它文件属性:06 检查读写权限 04 检查读权限 02 检查写权限 01 检查执行权限 00 检查文件的存在性例如 if(access("D:\\123.txt", 0) == -1) printf("文件不存在");

『肆』 C语言判断指定文件是否存在

头文件:.h 功 能: 确定文件或文件夹的访问权限。即,检查某个文件的存取方式,比如说是只读方式、只写方式等。如果指定的存取方式有效,则函数返回0,否则函数返回-1。 用 法: int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode ); 参数说明: filenpath 文件或文件夹的路径,当前目录直接使用文件或文件夹名 备注:当该参数为文件的时候,access函数能使用mode参数所有的值,当该参数为文件夹的时候,access函数值能判断文件夹是否存在。在WIN NT 中,所有的文件夹都有读和写权限 mode 要判断的模式 在头文件unistd.h中的预定义如下: #define R_OK 4 /* Test for read permission. */#define W_OK 2 /* Test for write permission. */#define X_OK 1 /* Test for execute permission. */#define F_OK 0 /* Test for existence. */具体含义如下:00 只判断是否存在02 只判断是否有写权限04 只判断是否有读权限06 判断是否有读并且有写权限 程序例#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);}头文件:io.h 功 能: 确定文件或文件夹的访问权限。即,检查某个文件的存取方式,比如说是只读方式、只写方式等。如果指定的存取方式有效,则函数返回0,否则函数返回-1。 用 法: int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode ); 参数说明: filenpath 文件或文件夹的路径,当前目录直接使用文件或文件夹名 备注:当该参数为文件的时候,access函数能使用mode参数所有的值,当该参数为文件夹的时候,access函数值能判断文件夹是否存在。在WIN NT 中,所有的文件夹都有读和写权限 mode 要判断的模式 在头文件unistd.h中的预定义如下: #define R_OK 4 /* Test for read permission. */#define W_OK 2 /* Test for write permission. */#define X_OK 1 /* Test for execute permission. */#define F_OK 0 /* Test for existence. */具体含义如下:00 只判断是否存在02 只判断是否有写权限04 只判断是否有读权限06 判断是否有读并且有写权限 程序例#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);}

『伍』 怎样让c语言判断文件是否存在

FILE * fp; fp=fopen("h:\\abc\\x.txt","r"); if(fp==NULL){ //不存在}else{ fclose(fp);} 可以抄把上述代码封袭装成一个函数。 其实也可以考虑用Windows api 来进行判断

『陆』 c语言如何判断一个文件是否被修改

可以用 文件状态 ,例如 文件建立时间,文件最后一次修改时间,文件最后一次被访问的时间,做判断。获取文件状态用:#include <io.h>int get_namein_time(char *namein, char * ftime){struct _finddata_t fileinfo;int res,DEBUG=0,flag=0;if ( (res = _findfirst(namein, &fileinfo)) == -1){if (DEBUG==1) printf("get file info error !\n");return 0;};if ( strcmp(namein,fileinfo.name)==0 ) {flag=1; goto Lab;}do {if ( strcmp(namein,fileinfo.name)==0 ) {flag=1;goto Lab;}} while ( _findnext(res, &fileinfo) ==0); Lab: strcpy(ftime,ctime(&fileinfo.time_write)); _findclose(res);return flag;}最可靠的是用文件的哈希码判断,就是区块链中用的方法。例如视窗系统,调用系统 Certutil 计算出 文件的 哈希码,与文件原来的码对比。若变了,就是被修改了。Certutil -hashfile abc.txt MD5 这个检查 文件 abc.txt Certutil -hashfile XYZ.txt SHA512 这个检查 文件 XYZ.txt

『柒』 C语言怎么判断一个文件为空

我这里有一种方法,不知能满足楼主的要求不?FILE *pfile;int filepos = 0;pfile = fopen("test.txt", "r");fseek(pfile, 0, SEEK_END);filepos = ftell(pfile);if(0 == pos){ puts("The file is null!");}else{ puts("The file has some content!");}

『捌』 C语言如何判断文件类型

windows下,可以根据文件名的扩展名去判断,如.txt是文本文件,.exe是可执行文件把文件名存到一个变量s中,用strrchr(s,'.')函数去得到扩展名,进行判断就可以了!

『玖』 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 ValueEach of these functions returns 0 if the file has the givenmode. The function returns –1 if the named file does not exist oris not accessible in the given mode; in this case,errnois set as follows:EACCESAccess denied: file’s permission setting does notallow 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 asspecified by the value ofmode. When used withdirectories,_accessdetermines only whether thespecified directory exists; in Windows NT, all directories haveread and write access.modeValueChecks File For00Existence only02Write permission04Read permission06Read and write permissionExample#include<io.h#include<stdio.h#include<stdlib.hvoidmain(void){if((_access("ACCESS.C",0))!=-1){printf("FileACCESS.C

『拾』 C语言,判断一个文件是否存在

你贴的这个函数PathFileExists并不是C语言提供的库函数,而是windows系统提供的系统调用,如果你是初学者,尽量用C语言提供的库函数来实现功能,你可以这样:int exist(char *file) //传入想要判断的路径字符串指针{ FILE *fp; fp=fopen(file,"r"); //fopen是一个C库函数,用于打开文件,"r"是只读模式,在这种模式下,如果文件存在,则能成功以只读模式打开,fopen返回一个非0的文件描述符,如果文件不存在,则fopen返回NULL(NULL意思是空)。正好可以利用这一点来判断文件是否存在 if(fp=NULL) return 0; //不存在返回0 else { fclose(fp); //存在的话,要先把之前打开的文件关掉 return 1; //然后返回1 }}这样,你就可用这里定义的exist函数判断文件是否存在了。比如if(exist("a.txt")==0)printf("不存在!");else printf("存在!");如果你真想用PathFileExists这个函数,那么也很简单,LPCTSTR你可以简单理解为就相当于char*,这是windows封装的一个数据类型。_in是一个修饰符,表示参数是传入给PathFileExists用的而不是由PathFileExists传出来的。这个函数可以这样用:if(PathFileExists("a.txt")==FALSE)printf("不存在!");else printf("存在!");用这个函数时注意加头文件<windows.h>有问题请继续追问啊


赞 (0)