im using WIN32_FIND_DATA to store the data findfirstfile outputs. i want the file location (C:\file) as a string but i don’t know how to get it or any other data from it.
Edit: here is my code
PTSTR pszFileName;
PTSTR pszFileName2[100];
if (search_handle)
{
do
{
pszFileName = file.cFileName;
pszFileName2[loop] = pszFileName;
Sleep(100);
loop++;
std::wcout << file.cFileName << std::endl;
}
while(FindNextFile(search_handle,&file));
CloseHandle(search_handle);
}
WIN32_FIND_DATAis a struct. Check out thecFileNamemember.For example:
Update in response to comments
In this example the storage for the string is on the stack, and the same buffer is used for every call. This means that every
FindNextFile()overwrites the previous string. You will have to make a copy of the string.Since you’re using C++ and classes in
stdI suggest you store it instd::string(or better yet, make sure you defineUNICODEand_UNICODEand usewstring.) Initializing a newstringclass will do the allocation and copying on your behalf.Alternatively you can copy the string using the typical C techniques (for example: using
malloc+memcpy,strdup, or similar), but it sounds like you might want a refresher in strings, pointers, and memory allocation in C before you get to that.By the way — to check for error, your code compares the find handle against
NULL; this is incorrect.FindFirstFile()returnsINVALID_HANDLE_VALUE(which works out to(HANDLE)-1) on failure. Additionally, to close the handle you will want to useFindClose(), and notCloseHandle(). (A “find handle” isn’t really a handle to a kernel object in the same sense that a file handle, handle to a module, or a thread or process handle is. They’ve just overloaded the type.)