C language searches the instance code of the specified file
- 2020-04-01 21:37:55
- OfStack
#include<stdio.h>
#include<windows.h>
void FindFile(char* ,char* );
int count=0;//Number of documents
char fname[32];
#define BUFSIZE 256
int main(int argc,char* argv[])
{
char szLogicalDriveStrings[BUFSIZE];
DWORD iLength;
int iSub;
printf(" Please enter the file name to search: ");
scanf("%s",fname);
ZeroMemory(szLogicalDriveStrings, BUFSIZE);
iLength = GetLogicalDriveStringsA(BUFSIZE-1, szLogicalDriveStrings);
for(iSub=0; iSub<iLength; iSub+=4)
{
//If not fixed disk drive: local hard disk or removable hard disk, ignore
if(GetDriveType(szLogicalDriveStrings+iSub)!=3)
continue;
FindFile(szLogicalDriveStrings+iSub,"*.*");
}
printf(" Find the total %d A file ...n",count);
scanf("%*d");
return 0;
}
void FindFile(char* pfilename,char* pfilter)
{
WIN32_FIND_DATA findfiledate;
HANDLE hfind;
char filename[512];
char lpFileName[512];
char _lpFileName[512];
int i;
int result;
for(i=0;*(pfilename+i)!='0';i++)
filename[i]=*(pfilename+i);
filename[i]='0';
//If the last character is not ''
if(filename[strlen(filename)-1]!='\')
strcat(filename,"\"); //Add ''
strcpy(lpFileName,filename);
strcat(lpFileName,pfilter);
hfind=FindFirstFile(lpFileName,&findfiledate);
if(hfind==INVALID_HANDLE_VALUE)
return;
do
{
//If it's not a directory
if(!(findfiledate.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
//If the specified file is found
if(0==strcmp(fname,findfiledate.cFileName))
{
printf("%s%sn",filename,findfiledate.cFileName);
count++;
}
}
//If it's a directory
else
{
//. And... No output
if(findfiledate.cFileName[0]!='.')
{
strcpy(_lpFileName,filename);
strcat(_lpFileName,findfiledate.cFileName);
FindFile(_lpFileName,pfilter); //recursive
}
}
}while(FindNextFile(hfind,&findfiledate));//FindNextFile returns true and continues the search
FindClose(hfind);
return;
}
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201303/2013313231933065.jpg" >
< img SRC = "/ / files.jb51.net/file_images/article/201303/2013313232124790.jpg" border = 0 >
Use recursive search file, low efficiency, use multithreading effect better.