您的代码存在一些问题。
您将"\\*" 连接到argv[1] 指向的内存,这很糟糕。您需要将path 从wchar_t* 指针更改为wchar_t[] 数组,然后将wcscpy argv[1] 数据放入其中。
您没有为filePath 分配任何内存,因此wcscpy() 和wcscat() 正在写入无效内存。您还需要将filePath 更改为wchar_t[] 数组,然后将wcscpy/wcscat 路径数据放入其中。
将path 和cFileNames 值组合在一起时,您也不会忽略串联的*。
你根本不需要memset()(尤其是因为你给了它错误的字节数)。
尝试类似的方法:
#define UNICODE 1
#include <stdio.h>
#include <windows.h>
#include <wchar.h>
int wmain(int argc, wchar_t **argv)
{
if (argc < 2)
{
wprintf(L"Usage: \"%s\" path\n", argv[0]);
return -1;
}
int len = wcslen(argv[1]);
if (len >= MAX_PATH)
{
wprintf(L"Path is too long\n");
return -1;
}
wchar_t path[MAX_PATH+1] = {};
wcscpy(path, argv[1]);
if ((len > 0) && (path[len-1] != L'\\'))
wcscat(path, L"\\");
wchar_t searchMask[MAX_PATH+2] = {};
wcscpy(searchMask, path);
wcscat(searchMask, L"*");
WIN32_FIND_DATA FindData;
HANDLE hSearch = FindFirstFileW(searchMask, &FindData);
if (hSearch == INVALID_HANDLE_VALUE)
{
if (GetLastError() != ERROR_FILE_NOT_FOUND)
{
wprintf(L"Error looking for first file\n");
return -1;
}
wprintf(L"No files found\n");
}
else
{
wchar_t filePath[MAX_PATH*2];
do
{
wcscpy(filePath, path);
wcscat(filePath, FindData.cFileName);
wprintf(L"Path %s\n", filePath);
}
while (FindNextFileW(hSearch, &FindData));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
FindClose(hSearch);
wprintf(L"Error looking for next file\n");
return -1;
}
FindClose(hSearch);
}
return 0;
}
尽管如此,您确实应该使用std::unique_ptr 和std::wstring 类并让它们为您管理内存/资源。使用 C 库函数并不能帮助您学习 C++:
#define UNICODE 1
#include <windows.h>
#include <wchar.h>
#include <iostream>
#include <string>
#include <memory>
struct FindDeleter
{
typedef HANDLE pointer;
void operator()(HANDLE h)
{
if(h != INVALID_HANDLE_VALUE)
FindClose(h);
}
};
int wmain(int argc, wchar_t **argv)
{
if (argc < 2)
{
std::wcerr << L"Usage: \"" << argv[0] << L"\" path" << std::endl;
return -1;
}
std::wstring path = argv[1];
if ((!path.empty()) && (path[path.length()-1] != L'\\'))
path += L'\\';
WIN32_FIND_DATA FindData;
std::unique_ptr<HANDLE, FindDeleter> hSearch(FindFirstFileW((path + L"*").c_str(), &FindData));
if (hSearch.get() == INVALID_HANDLE_VALUE)
{
if (GetLastError() != ERROR_FILE_NOT_FOUND)
{
std::wcerr << L"Error looking for first file" << std::endl;
return -1;
}
std::wcout << L"No files found" << std::endl;
}
else
{
do
{
std::wstring filePath = path + FindData.cFileName;
std::wcout << L"Path " << filePath << std::endl;
}
while (FindNextFileW(hSearch.get(), &FindData));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
std::wcerr << L"Error looking for next file" << std::endl;
return -1;
}
}
return 0;
}
或者,如果您没有使用 C++11 编译器:
#define UNICODE 1
#include <windows.h>
#include <wchar.h>
#include <iostream>
#include <string>
class FindHandle
{
private:
HANDLE m_hFind;
public:
FindHandle(HANDLE hFind) : m_hFind(hFind) {}
~FindHandle()
{
if (m_hFind != INVALID_HANDLE_VALUE)
FindClose(m_hFind);
}
HANDLE get() { return m_hFind; }
};
int wmain(int argc, wchar_t **argv)
{
if (argc < 2)
{
std::wcerr << L"Usage: \"" << argv[0] << L"\" path" << std::endl;
return -1;
}
std::wstring path = argv[1];
if ((!path.empty()) && (path[path.length()-1] != L'\\'))
path += L'\\';
WIN32_FIND_DATA FindData;
FindHandle hSearch(FindFirstFileW((path + L"*").c_str(), &FindData));
if (hSearch.get() == INVALID_HANDLE_VALUE)
{
if (GetLastError() != ERROR_FILE_NOT_FOUND)
{
std::wcerr << L"Error looking for first file" << std::endl;
return -1;
}
std::wcout << L"No files found" << std::endl;
}
else
{
do
{
std::wstring filePath = path + FindData.cFileName;
std::wcout << L"Path " << filePath << std::endl;
}
while (FindNextFileW(hSearch.get(), &FindData));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
std::wcerr << L"Error looking for next file" << std::endl;
return -1;
}
}
return 0;
}