c判斷是否是文件夾
❶ c++ 判斷一個路徑是文件夾還是文件
WIN32_FIND_DATAAFindFileData;
FindFirstFileA("c:\1.txt",&FindFileData);
if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
//是文件夾
}
else
{
//是文件
}
可能需要#include<windows.h>
❷ c語言判斷文件夾是否存在
使用c語言庫中的_access()函數判斷文件夾是否存在。該函數的參數中文件夾路徑中不允許由空格。因此下面的代碼運行錯誤。 其實檢查的是e盤的my文件夾。
代碼:#include <io.h
#include <stdio.h
#include <stdlib.h
void main( void ){/* Check for existence */
❸ 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++判斷文件/文件夾是否存在
一、判斷文件夾是否存在: 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語言判斷一個字元串是文件還是文件夾
一般來說在C語言中讀取txt文件的信息有兩種方法,一種是使用C語言標准文件I/O中的fopen()、fread()等等函數,一種是調用操作系統中的API函數,比如Windows上的ReadFile()、OpenFile()等等,現在操作系統一般都具備內存文件映射功能,對於大的txt文件,一般都使用這種方式操作。下面是一個使用C語言標准文件I/O操作文件的例子。
#include<stdio.h>
FILE*stream;
void main(void)
{
long l;
float fp;
char s[81];
char c;
stream=fopen("fscanf.out","w+");
if(stream==NULL)
printf("Thefilefscanf.outwasnotopened\n");
else
{
fprintf(stream,"%s%ld%f%c","hello world",
65000,3.14159,'x');
/*Setpointertobeginningoffile:*/
fseek(stream,0L,SEEK_SET);
/*Readdatabackfromfile:*/
fscanf(stream,"%s",s);
fscanf(stream,"%ld",&l);
fscanf(stream,"%f",&fp);
fscanf(stream,"%c",&c);
/*Outputdataread:*/
printf("%s\n",s);
printf("%ld\n",l);
printf("%f\n",fp);
printf("%c\n",c);
fclose(stream);
}
}
❻ 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
❼ VC判斷是否是文件夾
因為每個文件夾底下都有兩個隱藏文件夾,一個是.,一個是..。這是每個文件夾創建時都會有的。.是代表本身這個文件夾,..是指上一級目錄即父文件夾。
❽ C#,判斷是文件還是文件夾。
樓上的胡說,文件也可能沒有擴展名,目錄也可以有小數點
判斷是文件還是文件夾
if(File.Exists(path)){
// 是文件
}else if(Directory.Exists(path)){
// 是文件夾
}else{
// 都不是
}
❾ c#中如何判斷一個路徑是目錄還是文件
1、在visual studio當中創建一個C#控制台應用程序,選擇新建項目,然後選擇visual C#,再選中控制台應用程序,輸入項目名稱,選擇位置,確定即可。
❿ c#判斷路徑 是文件還是文件夾
如果文件名同目錄名完全一樣,你沒法判斷到底是個文件還是目錄,因為物理磁碟上可能兩者都有(完全同名)。
比如說c:\windows,既可能是windows目錄,也可能是個主名為"windows"且沒有擴展名的文件,所以但判斷路徑字元串本身是無法判斷的。