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

c判斷文件是文件夾還是文件夾

發布時間: 2023-05-11 04:24:08

㈠ 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

linuxc語言如何實現判斷一個路徑是文件還是文件夾

#include
<stdio.h>
#include
<sys/stat.h>
#include
<unistd.h>
int
main(int
argc,char
*argv[])
{
struct
stat
st;
printf("%s",argv[1]);
stat(argv[1],&st);
if
(S_ISDIR(st.st_mode))
printf("is
a
dir\n");
else
printf("is
not
a
dir\n");
return
0;
}
虛擬機上測過了.
是驗證輸入的第一個參數是不是目錄.

㈢ c#判斷路徑 是文件還是文件夾

如果文件名同目錄名完全一樣,你沒法判斷到底是個文件還是目錄,因為物理磁碟上可能兩者都有(完全同名)。
比如說c:\windows,既可能是windows目錄,也可能是個主名為"windows"且沒有擴展名的文件,所以但判斷路徑字元串本身是無法判斷的。

㈣ c++ 判斷一個路徑是文件夾還是文件

WIN32_FIND_DATAAFindFileData;
FindFirstFileA("c:\1.txt",&FindFileData);
if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
//是文件夾
}
else
{
//是文件
}

可能需要#include<windows.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語言判斷文件夾內是否有文件夾或文件

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

㈦ C#,判斷是文件還是文件夾。

樓上的胡說,文件也可能沒有擴展名,目錄也可以有小數點
判斷是文件還是文件夾
if(File.Exists(path)){
// 是文件
}else if(Directory.Exists(path)){
// 是文件夾
}else{
// 都不是
}

㈧ VC判斷是否是文件夾

因為每個文件夾底下都有兩個隱藏文件夾,一個是.,一個是..。這是每個文件夾創建時都會有的。.是代表本身這個文件夾,..是指上一級目錄即父文件夾。

熱點內容
劍與家園新伺服器什麼時候轉國 發布:2025-02-12 18:38:05 瀏覽:433
php發送email 發布:2025-02-12 18:38:02 瀏覽:296
掃描二維碼密碼多少 發布:2025-02-12 18:23:35 瀏覽:51
北京時間ftp 發布:2025-02-12 18:23:31 瀏覽:777
開源分布式文件存儲 發布:2025-02-12 18:22:54 瀏覽:632
安卓七騎士亞服哪裡下載 發布:2025-02-12 18:22:49 瀏覽:532
資料庫腦裂 發布:2025-02-12 18:17:31 瀏覽:497
parsephp 發布:2025-02-12 18:17:28 瀏覽:19
stl源碼剖析高清pdf 發布:2025-02-12 18:11:48 瀏覽:980
ftp匿名帳號 發布:2025-02-12 18:04:32 瀏覽:765