導航:首頁 > 文件類型 > c文件夾是否存在文件

c文件夾是否存在文件

發布時間:2023-09-05 01:39:01

Ⅰ 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語言判斷文件夾內是否有文件夾或文件

舉例來說:FILE*fp=fopen("dict.txt","r");charbuf[1024];if(fp!=(FILE*)NULL){while(fgets(buf,sizeof(buf),fp))//從文件中讀入一行字元串,保存在buf中,直到讀完所有字元串{//處理讀入的字元串buf}fclose(fp);}

Ⅲ 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++中,<io.h>中的_access可以判斷文件是否存在,<direct.h>中的_mkdir可以岩鬧尺創建文件。

---------------------------------------------

建單級目錄:

#include <io.h>

#include <direct.h>

#include <string>

int main()

{

std::string prefix = "彎蘆G:/test/";

if (_access(prefix.c_str(), 0) == -1) //如果文件夾不存在

_mkdir(prefix.c_str()); //則創建

}

----------------------------------------------------

建多級目錄:

最後一個如果是文件夾的話,需要加上 '\\' 或者 '/粗高'

#include <io.h>

#include <direct.h>

#include <string>

int createDirectory(std::string path)

{

int len = path.length();

char tmpDirPath[256] = { 0 };

for (int i = 0; i < len; i++)

{

tmpDirPath[i] = path[i];

if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')

{

if (_access(tmpDirPath, 0) == -1)

{

int ret = _mkdir(tmpDirPath);

if (ret == -1) return ret;

}

}

}

return 0;

}

Ⅳ 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語言判斷指定文件是否存在

頭文件:.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判斷文件夾是否存在

用 _access() 可以判斷文件夾或文件是否存在。注意路徑用雙斜杠。例如:
#include <io.h>#include <stdio.h>#include <stdlib.h>main( ){ /* 檢查存在否 */ if( (_access( "D:\\user\\C\\P1", 0 )) != -1 ) printf( "Yes !" );
else printf("No !");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

閱讀全文

與c文件夾是否存在文件相關的資料

熱點內容
擴展名為rm的文件如何打開 瀏覽:707
導入android項目沒有rjava 瀏覽:899
帶英文和數字的文件名 瀏覽:843
黃山數控編程培訓在哪裡 瀏覽:111
win10俠盜5無限讀取 瀏覽:557
js工廠模式 瀏覽:927
iphone6s清理緩存 瀏覽:465
ug編程怎麼選不上字體 瀏覽:435
pc文件加密哪個好 瀏覽:393
蘋果軟體更新4位密碼 瀏覽:96
手機如何將文件弄成文件夾 瀏覽:275
swordigo第四關 瀏覽:996
t420win10網卡驅動 瀏覽:805
ps怎麼打開dxf文件怎麼打開 瀏覽:698
ps格式文件打開軟體 瀏覽:371
下載網路資源遇到哪些問題 瀏覽:887
抖音交友app廣告怎麼投 瀏覽:870
esxiha配置文件 瀏覽:573
pubg交易平台有哪些APP 瀏覽:144
excel插入文件夾地址 瀏覽:948

友情鏈接