导航:首页 > 版本升级 > c判断文件夹中是否存在文件

c判断文件夹中是否存在文件

发布时间:2023-03-09 19:50:52

① 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/C++判断文件/文件夹是否存在

一、判断文件夹是否存在: 1.用CreateDirectory(".//FileManege",NULL);如果文件夹FileManege不存在,则创建。 2.或者if(_access(".//FileManege",0)==-1),表示FileManege不存在。 3.或者BOOL PathIsDirectory(LPCTSTR pszPath);二、判断文件是否存在: 1.用if((file=fopen(".//FileManege//F//F.dat","rb"))==NULL) file=fopen(".//FileManege//F//F.dat","ab+"); // 先判断有无文件,没的话新建一个 2.用if(_access(".//FileManege//F//F.dat",0)==-1),表示文件不存在。 函数int _access( const char *path, int mode );可以判断文件或者文件夹的mode属性 mode=00;//Existence only mode=02;//Write permission mode=04;//Read permission 需要包含头文件<io.h>。

③ 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=fopen("dict.txt","r");charbuf[1024];if(fp!=(FILE*)NULL){while(fgets(buf,sizeof(buf),fp))//从文件中读入一行字符串,保存在buf中,直到读完所有字符串{//处理读入的字符串buf}fclose(fp);}

⑤ 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

⑥ 在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++如何判断一个文件夹是否存在

方法一: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判断文件夹中是否存在文件相关的资料

热点内容
maya粒子表达式教程 浏览:84
抖音小视频如何挂app 浏览:283
cad怎么设置替补文件 浏览:790
win10启动文件是空的 浏览:397
jk网站有哪些 浏览:134
学编程和3d哪个更好 浏览:932
win10移动硬盘文件无法打开 浏览:385
文件名是乱码还删不掉 浏览:643
苹果键盘怎么打开任务管理器 浏览:437
手机桌面文件名字大全 浏览:334
tplink默认无线密码是多少 浏览:33
ipaddgm文件 浏览:99
lua语言编程用哪个平台 浏览:272
政采云如何导出pdf投标文件 浏览:529
php获取postjson数据 浏览:551
javatimetask 浏览:16
编程的话要什么证件 浏览:94
钱脉通微信多开 浏览:878
中学生学编程哪个培训机构好 浏览:852
荣耀路由TV设置文件共享错误 浏览:525

友情链接