Your code is unnecessarily complicated.
If you just want to add a \*.txt
suffix to the input path string, you can simply use std::string
with its overloaded operator+
.
Then, if you want to pass a std::string
to a Win32 API that has a const char*
(i.e. LPCSTR
) parameter, you can use std::string::c_str()
method:
void GetFiles(LPCSTR path)
{
WIN32_FIND_DATA fileData;
std::string searchPath(path);
searchPath += "\\*.txt";
FindFirstFile(searchPath.c_str(), &fileData);
wcout << fileData.cFileName;
}
Note also that in modern world you should use Unicode (UTF-16) for Win32 programming; so const wchar_t*
and std::wstring
are better options than const char*
and std::string
.
Moreover, I'd just use a std::wstring
class as parameter, instead of a raw wchar_t
pointer.
void GetFiles(const std::wstring& path)
{
std::wstring searchPath = path + L"\\*.txt";
WIN32_FIND_DATA fileData;
FindFirstFile(searchPath.c_str(), &fileData);
std::wcout << fileData.cFileName;
}