用C+API对目录下所有文件进行遍历出错,请大虾帮忙

Aaron_Jerry 2007-03-24 11:35:16
我在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
...全文
394 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gscool 2007-03-25
  • 打赏
  • 举报
回复
这样试试

#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;
}
Aaron_Jerry 2007-03-25
  • 打赏
  • 举报
回复
谢谢两位的热心答复。
还有一问不解:执行的输出列表中的文件顺序好象并不和硬盘上的文件顺序一致啊,why?

jixingzhong 2007-03-25
  • 打赏
  • 举报
回复
或者这样:

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);
}
jixingzhong 2007-03-25
  • 打赏
  • 举报
回复
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;
}
}
}
在Windows 10或Windows 11操作系统中,用户经常会遇到共享打印机时出现的一系列错误代码,这些错误代码可能会阻碍打印机共享功能的正常使用。常见的错误代码包括0x00000057、0x00000709和0x0000011b,这些代码通常指出了不同的问题,比如权限不足、服务未运行或配置错误等。除此之外,还有一些故障提示如“连接失败”或“内存不足”,这些都可能影响到打印机共享的稳定性。 要解决这些故障,首先要确保打印机已经正确地连接到网络,并且在需要共享的电脑上进行了设置。确保打印机驱动程序是最新的,并且在共享设置中没有错误配置。对于权限问题,需要检查网络上的用户账户是否具有足够的权限来访问共享打印机。同时,也要确保打印机服务正在运行,特别是“Print Spooler”服务,因为这是打印机共享服务的核心组件。 在某些情况下,问题可能与操作系统的更新有关,如升级到最新版的Windows 10或Windows 11后可能出现的兼容性问题。这时,可能需要查看微软的官方支持文档来获取特定的解决方案或更新。 对于错误代码0x00000057,这通常是由于没有足够的权限来访问网络打印机或其共享资源,解决方法是确保网络打印机的权限设置正确,包括在组策略中设置相应的访问权限。而0x00000709错误可能是由于打印机驱动问题或打印机端口配置错误,可以尝试重新安装或更新打印机驱动来解决。至于0x0000011b错误,这往往是因为打印机队列服务的问题,检查并重启“Print Spooler”服务通常是解决这类问题的常见手段。 至于“连接失败”或“内存不足”这类故障,通常与客户端和打印机之间的网络连接以及打印机本地资源的使用情况有关。检查网络连接,确保打印机所在的网络段没有故障或中断。同时,如果打印机的打印队列长时间得不到处理,可能会导致内存不足的情况,这时可能需要清理打印队列或增加打印机的内存配置。 为了帮助用户更快速地解决这些问题,市面上出现了各种打印机共享错误修复工具。这些工具往往通过预设的修复程序来自动检测和修正打印机共享中常见的问题。它们可以快速检查打印机驱动、网络连接以及共享设置,并且能够提供一键修复功能,大幅减少了用户自行排查和解决问题的难度。 然而,在使用这些修复工具之前,用户应确保这些工具的来源是安全可靠的,避免因使用不当的修复工具而引发其他系统安全或隐私问题。用户可以到官方平台或者信誉良好的软件提供商处下载这些工具。通过细心检查打印机的共享设置,及时更新驱动程序和服务,以及合理使用修复工具,大多数共享打印机的问题都可以得到有效的解决。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧