I'm making a program which recursively lists all files in a certain directory and uploads each file separately to an FTP server using WinINet. The problem I'm encountering is using filesystem::path::filename in the FtpPutFile() function because a LPCWSTR is needed. Whats the best and easiest way to convert it (or somehow use it as is)?
std::string path = "C:\\Programs";
for (const auto & entry : std::experimental::filesystem::recursive_directory_iterator(path))
FtpPutFile(hIConnect, entry.path().filename(), entry.path().filename(), FTP_TRANSFER_TYPE_BINARY, 0);
The error I get is: no suitable conversion function from "const std::experimental::filesystem::v1::path" to "LPCWSTR" exists
EDIT: Here is a solution that worked for me, by following Lightness solution:
std::string path = "C:\\Programs";
for (const auto & entry : std::experimental::filesystem::recursive_directory_iterator(path))
FtpPutFile(hIConnect, entry.path().wstring().c_str(), entry.path().filename().wstring().c_str(), FTP_TRANSFER_TYPE_BINARY, 0);
LPCWSTR
is Microsoft's obfuscation of theconst wchar_t*
type, andfilesystem
paths conveniently have awstring()
member function. As you may recall, C++ strings give you access to their character buffer, too, viac_str()
.So,
entry.path().filename().wstring().c_str()
is aLPCWSTR
you can use (ugh!). Be careful to use that immediately, or store the result ofwstring()
somewhere for as long as you need theLPCWSTR
to survive, becausewstring()
returns by value and you don't want a dangling pointer.