Hello i got a problem with my code here.
LPCSTR mergeString(LPCSTR firstString, std::string secondString)
{
string convertedString = ConvertString(firstString);
LPCSTR mergedString;
int i = convertedString.size();
convertedString.insert(i, secondString);
mergedString = (convertedString.c_str());
return mergedString;
}
void GetFiles(LPCSTR path)
{
WIN32_FIND_DATA File_Data;
LPCSTR lPath = mergeString(path,"\\*.txt");
FindFirstFile(lPath, &File_Data);
wcout << File_Data.cFileName;
}
You pass in the path you want to use in the GetFiles(LPCSTR path) Then i use the mergestring function to merge together the path with a extension (\*.txt) everything works except when it returns the LPCSTR then its just a lot of wierd characters and i dont know why or is it a better way to do this?
Your code is unnecessarily complicated.
If you just want to add a
\*.txtsuffix to the input path string, you can simply usestd::stringwith its overloadedoperator+.Then, if you want to pass a
std::stringto a Win32 API that has aconst char*(i.e.LPCSTR) parameter, you can usestd::string::c_str()method:Note also that in modern world you should use Unicode (UTF-16) for Win32 programming; so
const wchar_t*andstd::wstringare better options thanconst char*andstd::string.Moreover, I’d just use a
std::wstringclass as parameter, instead of a rawwchar_tpointer.