c++如何才能实现查询功能?
发布网友
发布时间:2023-02-05 16:33
我来回答
共1个回答
热心网友
时间:2023-12-02 17:22
//下面是C++查找文件的源代码,我自己写的,已经通过编译和测试,如果楼主有不懂的地方,可以和我交流
//使用方法:用命令行输入。例如,程序名为Find.exe,则命令行为 Find *yourfile*
//查找支持通配符*,?
#define _WIN32_WINNT 0x0400
#include <iostream>
#include <stdlib.h>
using namespace std;
#ifdef UNICODE
#undef UNICODE
#endif
#include <windows.h>
BOOL MyFindFile(LPCSTR sFindPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
char sPath[MAX_PATH];
char sFormatFileName[MAX_PATH+2] = "*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;
lstrcpy(sFormatFileName, sFindPath);
lstrcat(sFormatFileName, "\\*");
lstrcat(sFormatFileName, sFindFileName);
lstrcat(sFormatFileName, "*");
hFind = FindFirstFile(sFormatFileName, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
while (!fFinished)
{
lstrcpy(sPath, sFindPath);
lstrcat(sPath, "\\");
lstrcat(sPath, FindFileData.cFileName);
if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
{
if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
cout << " Folder " << ++uCountFolder << ". - " << sPath <<""<< endl;
}
else
cout << " File " << ++uCountFile << ". - " << sPath <<""<< endl;
if (!FindNextFile(hFind, &FindFileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
else
{
break;
}
}
}
FindClose(hFind);
}
return TRUE;
}
BOOL MyFindFolder(LPCSTR sPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
char sTemp[MAX_PATH];
char sFormatFileName[MAX_PATH];
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;
MyFindFile(sPath, sFindFileName, uCountFolder, uCountFile);
lstrcpy(sFormatFileName, sPath);
lstrcat(sFormatFileName, "\\*");
hFind = FindFirstFile(sFormatFileName, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
while (!fFinished)
{
if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
{
if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
{
lstrcpy(sTemp, sPath);
lstrcat(sTemp, "\\");
lstrcat(sTemp, FindFileData.cFileName);
MyFindFolder(sTemp, sFindFileName, uCountFolder, uCountFile);
}
}
if (!FindNextFile(hFind, &FindFileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
FindClose(hFind);
}
return TRUE;
}
void GetFileName(LPCSTR sFullPath, LPSTR sFilePath, LPSTR sFileName)
{
LPSTR p = (LPSTR)(sFullPath + lstrlen(sFullPath) - 1);
bool Flag(false);
while (p != sFullPath)
{
if ('\\' == *p || '/' == *p)
{
Flag = true;
break;
}
p--;
}
if (Flag)
{
lstrcpy(sFileName, p + 1);
lstrcpy(sFilePath, sFullPath);
sFilePath[p-sFullPath] = '\0';
}
else
{
lstrcpy(sFileName, sFullPath);
GetFullPathName(".", MAX_PATH, sFilePath, NULL);
}
}
ULONGLONG MyStartFind(LPCSTR sFindFileName)
{
char sPath[MAX_PATH];
char sFileName[MAX_PATH];
ULONGLONG uCountFolder(0);
ULONGLONG uCountFile(0);
GetFileName(sFindFileName, sPath, sFileName);
MyFindFolder(sPath, sFileName, uCountFolder, uCountFile);
if (uCountFolder + uCountFile)
{
cout << "------------------------------------------------------------"<< endl;
cout << "Total Folders:"<< uCountFolder << "" << endl;
cout << "Total Files: "<< uCountFile << "" << endl;
}
else
cout << "Couldn't Find File." << endl;
return uCountFolder + uCountFile;
}
int main(int argc, char *argv[])
{
char sFindFileName[MAX_PATH];
if (argc < 2)
{
cout << "Enter Find File Name:";
cin >> sFindFileName;
}
else
{
lstrcpy(sFindFileName, argv[1]);
}
MyStartFind(sFindFileName);
#ifdef _DEBUG
system("pause");
#else
if (argc < 2)
system("pause");
#endif
return (0);
}
/*Output:
C:\a>find a
File 1. - C:\a\a.mdb
File 2. - C:\a\a.txt
Folder 1. - C:\a\a1
File 3. - C:\a\a1\a1.mdb
File 4. - C:\a\a1\a1.txt
Folder 2. - C:\a\a1\a2
File 5. - C:\a\a1\a2\a2.mdb
File 6. - C:\a\a1\a2\a2.txt
--------------------------------
Total Folders:2
Total Files: 6
*/追问您用的是C吧