c语言判断文件是否存在
㈠ c语言里面如何判断一个文件不存在
//用fopen()函数以读的方式打开,如果文件句柄为NULL,则该文件不存在咯!
//希望对您有所帮助!!
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
if ((fp=fopen("test.txt", "r")) == NULL)
printf("File test.txt not exists.\n");
else
printf("File test.txt exists.\n");
fclose(fp);
return 0;
}
㈡ c语言怎么判定一个文件夹是否有文件谢谢
#include<携察dos.h>
#include<dir.h>
void main()
{
struct ffblk ffblk;
int success=0; //假设没有文件
int done;
done = findfirst("c:\\test\\空坦*.*"辩亏茄,&ffblk,0); //假设文件夹名称c:\\test\\
if(done==0)
success=1; //表示有文件
}
㈢ 在C语言中,我们如何判断一个文件是否已经被创建了呢
FILE *fp;
fp=fopen("file.txt" , "r");
if ( fp == NULL )
printf("file not exist!");
else
fcolse(fp);
另外:
int access(char *path, int amode);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
以上函数都可以判断文件是否存在,可查阅相关文档,看其详细用法
㈣ 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语言,判断一个文件是否存在
你贴的这个函数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>
有问题请继续追问啊