查找指定目录下所有TXT文件的代码
//quanju mulu;
#include <string.h>
#include <stdio.h>
#define _ALPHA_
#include <windef.h>
#include <winbase.h>
//kernel32.lib
int res=0;
FILE* FP;
void func(char* dir)
{
WIN32_FIND_DATA wfd;
char dirWork[MAX_PATH];
strcpy(dirWork,dir);
strcat(dirWork,"*");
/* if(strlen(dirWork)>MAX_PATH-1)
{
puts(dirWork);
return;
}*/
HANDLE hFirFile=::FindFirstFile(dirWork,&wfd);
/* if(hFirFile==INVALID_HANDLE_VALUE)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf,0,NULL
);
printf("%s",lpMsgBuf);
}
*/
do
{
if( !strcmp(wfd.cFileName,".")||!strcmp(wfd.cFileName,"..") )
continue;
else if( wfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
char subdir[MAX_PATH];
strcpy(subdir,dir);
strcat(subdir,wfd.cFileName);
strcat(subdir,"\\");
func(subdir);
}
else
{
if( strstr(wfd.cFileName,".txt") )
{
res++;
SaveFileName(wfd.cFileName);
}
}
}while( ::FindNextFile(hFirFile,&wfd) );
}
void SaveFileName(char* s)
{
fprintf(FP,"%s\n",s);
}
void main()
{
FP=fopen("C:\\Documents and Settings\\xu\\桌面\\filenames.txt","w");
char* dir="C:\\Program Files\\Common Files\\";
func(dir);
printf("res=%d\n",res);
fclose(FP);
}
这段代码初看起来是正确的,您知道错在哪里吗?(用本程序不能搜索到所有txt文件,比如有windows的搜索工具能搜到9个,而此程序只能搜到5个)