Ⅰ MFC CFileFind和CFile遍歷一個指定文件夾並刪除裡面的所有文件(裡面沒有下層文件夾目錄)問題
首先,不要用TCHAR,其次,你的for循環有問題,再者Remove函數只可刪除文件,不可刪除目錄,而且你還沒加異常判斷。
void RemoveFileInDir(const char* dir) //比如 E:\\Test
{
char buff[256];
sprintf_s(buff,"%s\\*.*",dir);
CString name,path;
CFileFind find; BOOL bFind = find.FindFile(buff);
try{
while(bFind)
{
bFind = find.FindNextFile();
name = find.GetFileName();
if(find.IsDirectory() || name.Compare(".") == 0 || name.Compare("..") == 0)
continue;
path = find.GetFilePath();
CFile::Remove(path);
cout<<path.LPCTSTR();<<endl;
}
}catch(CFileException &e){
e.GetErrorMessage(buff,256);
cout<<buff<<endl;
}}
Ⅱ C璇璦linux綆鍗曠紪紼嬶紝閬嶅巻鏂囦歡澶硅幏寰楁枃浠跺悕錛岃皟璇曚笉瀵癸紝奼傚姪錛侊紒璋㈣阿錛侊紒
opendir() 鐨勫弬鏁頒笉瀵廣備笉搴旇ユ槸鏈夐氶厤絎︾殑*.jp2褰㈠紡錛岃屽簲璇ユ槸涓涓鎸囧畾鐨勭洰褰曘
鐪嬩綘鐨勯渶奼傛弿榪幫紝寤鴻浣犵敤scandir鍑芥暟錛屽彲鑳芥洿鏂逛究涓浜涖
scandir鍙浠ユ寚瀹氫竴涓猣ilter銆傚彧榪斿洖婊¤凍鏉′歡鐨勯」鐩銆
涓嬮潰鏄鍙傝僱inux涓璼candir man page 閲岀殑渚嬪瓙鍐欑殑涓孌靛弬鑰冧唬鐮併
#include <dirent.h>
int myfilter(const struct dirent *)
{
// 濡傛灉鏂囦歡浠0寮澶翠笖鏈.jp2鍚庣紑錛岃繑鍥1
// 鍚﹀垯 榪斿洖0
}
main(){
struct dirent **namelist;
int n;
n = scandir(DIRNAME, &namelist, myfilter, alphasort);
if (n < 0)
perror("scandir");
else {
while(n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
Ⅲ 怎樣使用C語言列出某個目錄下的文件
C語言本身沒有提供象dir_list()這樣的函數來列出某個目錄下所有的文件。不過,利用C語言的幾個目錄函數,你可以自己編寫一個dir_list()函數。 首先,頭文件dos.h定義了一個find_t結構,它可以描述DOS下的文件信息,包括文件名、時間、日期、大小和屬性。其次,C編譯程序庫中有_dos_findfirst()和_dos_findnext()這樣兩個函數,利用它們可以找到某個目錄下符合查找要求的第一個或下一個文件。 dos_findfirst()函數有三個參數,第一個參數指明要查找的文件名,例如你可以用「*.*」指明要查找某個目錄下的所有文件。第二個參數指明要查找的文件屬性,例如你可以指明只查找隱含文件或子目錄。第三個參數是指向一個find_t變數的指針,查找到的文件的有關信息將存放到該變數中。 dos_findnext()函數在相應的目錄中繼續查找由_dos_findfirst()函數的第一個參數指明的文件。_dos_findnext()函數只有一個參數,它同樣是指向一個find_t變數的指針,查找到剛文件的有關信息同樣將存放到該變數中。 利用上述兩個函數和find_t結構,你就可以遍歷磁碟上的某個目錄,並列出該目錄下所有的文件,請看下例: #include <stdio.h> #include <direct.h> #include <dos.h> #include <malloc.h> #include <memory.h> #include <string.h> typedef struct find_t FILE_BLOCK void main(void); void main(void){FILE_BLOCK f-block; /* Define the find_t structure variable * / int ret_code; / * Define a variable to store the return codes * / / * Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. * / ret_code = _dos_findfirst(" *. * ", 0xFF, &f_block); /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. * / while (ret_code == 0){/* Print the file's name * / printf(" %-12s\n, f_block, name); / * Use the -dos_findnext() function to look
Ⅳ C語言如何讀取指定路徑下的所有指定格式的文件
C語言讀取目錄中的文件名的方法多種多樣,以下是幾種常用的方法:
在Windows環境下,可以使用`system()`函數調用系統命令`dir`。具體做法是,編寫如下代碼:
_CRTIMP int __cdecl system (const char*);
system("dir c:\\ /a:h /b > c:\\dir.txt");
這段代碼會執行`dir`命令,列出c盤目錄下的文件名,並將結果輸出到`c:\\dir.txt`文件中。
另一種方法是使用`dirent.h`頭文件中的`opendir()`和`readdir()`函數。示例代碼如下:
int main(int argc, char* argv[]) {
DIR* directory_pointer;
struct dirent* entry;
if((directory_pointer=opendir("d:\\XL"))==NULL) {
printf("Error opening\n");
}
else {
while((entry=readdir(directory_pointer))!=NULL) {
printf("%s\n",entry->d_name);
}
closedir(directory_pointer);
}
system("PAUSE");
return 0;
}
這段代碼會打開d盤下的`XL`目錄,遍歷其中的文件名,並逐行列印出來。
如果缺少`dirent.h`頭文件,則可以使用`io.h`頭文件中的`_findfirst()`和`_findnext()`函數。示例代碼如下:
int main(int argc, char* argv[]) {
long file;
struct _finddata_t find;
_chdir("d:\\");
if((file=_findfirst("*.*",&find))==-1L) {
printf("空白!\n");
exit(0);
}
printf("%s\n",find.name);
while(_findnext(file,&find)==0) {
printf("%s\n",find.name);
}
_findclose(file);
system("PAUSE");
return 0;
}
這段代碼會遍歷d盤下的所有文件,並逐個列印它們的文件名。