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

c判斷文件夾

發布時間: 2023-03-30 05:41:10

❶ 如何用c語言判斷文件夾內是否有文件夾或文件

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

❷ c#中如何判斷一個路徑是目錄還是文件

1、在visual studio當中創建一個C#控制台應用程序,選擇新建項目,然後選擇visual C#,再選中控制台應用程序,輸入項目名稱,選擇位置,確定即可。

❸ 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/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判斷文件夾是否存在

用 _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++中,<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語言怎麼判定一個文件夾是否有文件謝謝

#include<攜察dos.h>
#include<dir.h>
void main()
{
struct ffblk ffblk;
int success=0; //假設沒有文件
int done;
done = findfirst("c:\\test\\空坦*.*"辯虧茄,&ffblk,0); //假設文件夾名稱c:\\test\\
if(done==0)
success=1; //表示有文件
}

❾ 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++判斷文本文件為空,必須要先讀一下文件(讀一個字元),才能判斷是否為空。
剛打開文件時,文件狀態標識都是初始化狀態,eof()函數是根據文件狀態標識來判斷當前是否到了文件尾,而不是用來判斷文件是否為空。當讀取文件數據時,遇到文件尾時,系統會更改文件狀態標識為文件尾,同時,返回EOF標志。
參考代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream file;
int ch;
file.open("a.txt",ios::in);
ch=file.get(); //試圖去讀一個字元
if(file.eof()) //或 if ( ch==EOF ) //一個字元都未讀到,表示文件為空
{
cout<<"文件為空"<<endl;
}
file.close();

熱點內容
編程找點 發布:2025-05-15 20:43:10 瀏覽:586
php上傳臨時文件夾 發布:2025-05-15 20:43:00 瀏覽:656
impala資料庫 發布:2025-05-15 20:42:12 瀏覽:648
android安裝插件 發布:2025-05-15 20:41:31 瀏覽:240
神秘顧客訪問 發布:2025-05-15 20:33:39 瀏覽:296
安卓市場手機版從哪裡下載 發布:2025-05-15 20:17:28 瀏覽:815
幼兒速演算法 發布:2025-05-15 20:15:08 瀏覽:87
best把槍密碼多少 發布:2025-05-15 20:13:42 瀏覽:548
android安裝程序 發布:2025-05-15 20:13:20 瀏覽:560
c語言跳出死循環 發布:2025-05-15 20:06:04 瀏覽:825