读书人

用C+API对目录下所有文件进行遍历出错

发布时间: 2012-03-07 09:13:51 作者: rapoo

用C+API对目录下所有文件进行遍历出错,请大虾帮忙
我在VC里新建了个控制台应用程序,代码如下:

#include <stdio.h>
#include <string.h>
#include <windows.h>

#define TYPE_FILE 0x0001
#define TYPE_DIRECTORY 0x0002
#define TYPE_DOT 0x0004

//判断类型
int FileType(LPWIN32_FIND_DATA lpfd)
{
BOOL bDir;
int ft=TYPE_FILE;
bDir=(lpfd-> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if(bDir)
{
if((lstrcmp(lpfd-> cFileName, ". ")==0)
|| (lstrcmp(lpfd-> cFileName, ".. ")==0))
ft=TYPE_DOT;
else
ft=TYPE_DIRECTORY;
}
return ft;
}

void ShowDir(char* dir)
{
HANDLE hFile;
WIN32_FIND_DATA fd;
BOOL ret;
char parent[_MAX_PATH];
char fullpath[_MAX_PATH];
int i;
int filetype;

for(i=0; dir[i]; i++);
if(dir[i-1] != '\\ ')
strcat(dir, "\\ ");
strcpy(parent,dir);
strcpy(fullpath,dir);
strcat(dir, "*.* ");

ZeroMemory(&fd,sizeof(WIN32_FIND_DATA));
hFile=FindFirstFile(dir,&fd);
while(ret=FindNextFile(hFile,&fd))
{
filetype=FileType(&fd);
if(filetype==TYPE_FILE)
{
strcat(fullpath,fd.cFileName);
printf( "%s\n ",fullpath);
strncpy(fullpath,parent,strlen(parent));
fullpath[strlen(parent)]= '\0 ';
}
else if(filetype==TYPE_DOT)
continue;
else
{
strcat(fullpath,fd.cFileName);
ShowDir(fullpath);
}
}
FindClose(hFile);
}

int main(void)
{
char path[_MAX_PATH];
printf( "请输入一个目录名: ");
gets(path);
printf( "该目录下的所有文件:\n ");
ShowDir(path);

return 0;
}

例如我的硬盘上有如下文件结构:

D---001----001.c
|
-wsp001.dsp
|
-wsp001.dsw
|
-wsp001.ncb
|
-wsp001.opt
|
-wsp001.plg


|
-debug -------001.obj
|
-vc60.idb
|
-vc60.pdb
|
-wsp001.exe
|
-wsp001.ilk
|
-wsp001.pch
|
-wsp001.pdb
|
-a.txt
|
-b.txt

执行如下:

请输入一个目录名:D:\001
该目录下的所有文件:
D:\001\001.c
D:\001\Debug\001.obj
D:\001\Debug\a.txt
D:\001\Debug\b.txt
D:\001\Debug\vc60.idb
D:\001\Debug\vc60.pdb
D:\001\Debug\wsp001.exe
D:\001\Debug\wsp001.ilk
D:\001\Debug\wsp001.pch
D:\001\Debug\wsp001.pdb
D:\001\Debug\*.*wsp001.dsp //why ?????????
D:\001\wsp001.dsw
D:\001\wsp001.ncb
D:\001\wsp001.opt
D:\001\wsp001.plg
Press any key to continue


请大峡们指点下 本程序不涉及MFC

------解决方案--------------------


这样试试

#include <stdio.h>
#include <string.h>
#include <windows.h>

#define TYPE_FILE 0x0001
#define TYPE_DIRECTORY 0x0002
#define TYPE_DOT 0x0004

//判断类型
int FileType(LPWIN32_FIND_DATA lpfd)
{
BOOL bDir;
int ft=TYPE_FILE;
bDir=(lpfd-> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if(bDir)
{
if((lstrcmp(lpfd-> cFileName, ". ")==0)
|| (lstrcmp(lpfd-> cFileName, ".. ")==0))
ft=TYPE_DOT;
else
ft=TYPE_DIRECTORY;
}
return ft;
}

void ShowDir(char* dir)
{
HANDLE hFile;
WIN32_FIND_DATA fd;
BOOL ret;
char parent[_MAX_PATH];
char fullpath[_MAX_PATH];
int i;
int filetype;

for(i=0; dir[i]; i++);
if(dir[i-1] != '\\ ')
strcat(dir, "\\ ");
strcpy(parent,dir);
strcpy(fullpath,dir);
strcat(dir, "*.* ");

ZeroMemory(&fd,sizeof(WIN32_FIND_DATA));
hFile=FindFirstFile(dir,&fd);
while(ret=FindNextFile(hFile,&fd))
{
filetype=FileType(&fd);
if(filetype==TYPE_FILE)
{
printf( "%s\%s\n ",parent,fd.cFileName);
strncpy(fullpath,parent,strlen(parent));
fullpath[strlen(parent)]= '\0 ';
}
else if(filetype==TYPE_DOT)
continue;
else
{
strcat(fullpath,fd.cFileName);
ShowDir(fullpath);
}
}
FindClose(hFile);
}

int main(void)
{
char path[_MAX_PATH];
printf( "请输入一个目录名: ");
gets(path);
printf( "该目录下的所有文件:\n ");
ShowDir(path);

return 0;
}

[解决办法]
Dev C++ 测试通过:

利用链表实现目录内所有文件列表显示

#include <stdio.h>
#include <dirent.h>
/*#include <alloc.h> */
#include <string.h>

void main(int argc,char *argv[])
{
DIR *directory_pointer;
struct dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
if (argc!=2)
{
printf( "Must specify a directory\n ");
exit(1);
}
if ((directory_pointer=opendir(argv[1]))==NULL)
printf( "Error opening %s\n ",argv[1]);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node-> next=(struct FileList *)malloc(sizeof(struct FileList));
node=node-> next;
strcpy(node-> filename,entry-> d_name);
node-> next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf( "%s\n ",node-> filename);
node=node-> next;
}
}
}
[解决办法]
或者这样:

long handle;
struct _finddata_t filestruct;  
char path_search[_MAX_PATH];
handle = _findfirst( "目录 ",&filestruct);
if((handle == -1)) return;
if( ::GetFileAttributes(filestruct.name)& FILE_ATTRIBUTE_DIRECTORY )
{
if( filestruct.name[0] != '. ' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
}
else
{


if( !stricmp(filestruct.name, szFilename) )
{
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
while(!(_findnext(handle,&filestruct)))
{
  if( ::GetFileAttributes(filestruct.name) &FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '. ')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
}

读书人网 >C语言

热点推荐