當前位置:首頁 » 文件管理 » c判斷文件夾存在

c判斷文件夾存在

發布時間: 2023-02-24 23:24:02

① 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

熱點內容
ps3連ftp 發布:2025-05-20 04:19:11 瀏覽:816
計算機編譯干什麼的 發布:2025-05-20 04:05:18 瀏覽:46
安卓如何調手機時間 發布:2025-05-20 04:01:31 瀏覽:916
風扇轉壓縮機不轉 發布:2025-05-20 03:57:47 瀏覽:284
安卓手機如何測網速慢 發布:2025-05-20 03:55:49 瀏覽:495
用電腦做機房的伺服器 發布:2025-05-20 03:55:48 瀏覽:13
如何修改文件夾修改日期 發布:2025-05-20 03:44:08 瀏覽:831
安卓如何登陸tiktok 發布:2025-05-20 03:30:53 瀏覽:75
linux下執行python 發布:2025-05-20 03:23:30 瀏覽:431
sql查看器 發布:2025-05-20 03:22:53 瀏覽:134