I am trying to find files with specific name and deleting them in c++ as this code works fine if i give direct desktop path to it L"path//" but as path of desktop is different due to different user and system so i what i am doing at the top is to get desktop path in string variable and assigning it rather than direct path.
string desk=getenv("DESKTOP");
WIN32_FIND_DATAW fd;
HANDLE hFind = FindFirstFileW(desk, &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
DeleteFileW((wstring(desk) + fd.cFileName).c_str());
} while (FindNextFileW(hFind, &fd));
FindClose(hFind);
}
I am getting the following error
Error 4 error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'std::string' to 'LPCWSTR'
I have already tried using wstring and wchar but it gives error again. Can anyone please help me to sortout this issue. looking for correction in code
Windows will usually have two versions of a function, an A suffix will generally be it accepts chars, a W suffix accepts a wchar_t, and without the suffix usually ends up as a macro for whatever character set is selected. Generally the string types they will accept is LPCWSTR (long pointer to wide constant string) or LPCSTR (long pointer to constant string).
First argument of FindFirstFileW() takes a LPCWSTR.
LPCWSTR is a typedef for const wchar_t*. You are passing an std::string, so it's the wrong type.
Just be consistent with the string type, either do:
or:
Notice that in neither case you are passing the string class, but the character buffer held by the string.
The suffixes W and A I think stand for wide and ANSI.