Ⅰ 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盘下的所有文件,并逐个打印它们的文件名。