【发布时间】:2020-09-21 02:46:22
【问题描述】:
C++ 新手。我有一个代码通过目录旋转以查找特定文件(123.txt)并在文本框中列出结果。我需要做的是将这些结果存储在内存中,以便我以后可以访问它。也许是一个数组?我不确定这是怎么做到的。
这是执行它的代码:
outfile1.open("pxutil1.log");
DWORD dwSize = MAX_PATH;
char szLogicalDrives[MAX_PATH];
DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
if (dwResult == 0)
{
// error handling...
}
else if (dwResult > MAX_PATH)
{
// not enough buffer space...
}
else
{
for (char* szSingleDrive = szLogicalDrives; *szSingleDrive != 0; szSingleDrive += (lstrlenA(szSingleDrive) + 1))
{
if (GetDriveTypeA(szSingleDrive) == DRIVE_FIXED)
FindFile(szSingleDrive);
}
}
这是搜索 123.txt 并在日志文件中列出结果的代码。
std::string dir = directory;
if ((!dir.empty()) && (dir.back() != '\\') && (dir.back() != '/'))
dir += '\\';
WIN32_FIND_DATAA file;
HANDLE search_handle = FindFirstFileA((dir + "*").c_str(), &file);
if (search_handle == INVALID_HANDLE_VALUE)
{
if (GetLastError() != ERROR_FILE_NOT_FOUND)
{
// error handling...
//::MessageBox(NULL, "File not found", "", MB_OK);
}
}
else
{
do
{
if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((lstrcmpA(file.cFileName, ".") != 0) && (lstrcmpA(file.cFileName, "..") != 0))
{
//FindFile(dir + file.cFileName);
if (!ExcludeDir(dir + file.cFileName))
{
FindFile(dir + file.cFileName);
}
}
}
else
{
if (lstrcmpA(file.cFileName, "123.txt") == 0)
{
outfile1 << dir.c_str() << endl; //write to log file
}
}
} while (FindNextFileA(search_handle, &file));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
::MessageBox(NULL, "No more files", "", MB_OK);
}
FindClose(search_handle);
}
我想也许我可以将它添加到代码中,但它不起作用
string listOfDir[20]
std::string(dir) >> listOfDir;
【问题讨论】:
-
@JaziriRami 英文版??
-
尝试使用
std::vector<string> listOfDir;。然后在互联网上搜索c++ output iterator或使用循环打印向量的每个槽。 -
只要您使用std::filesystem::recursive_directory_iterator,这归结为几行代码。