# Trouble with FindFirstFile (Win32)

This topic is 3081 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, the following code uses to work fine (it finds all the files I'm looking for) until I try to edit any of those files (using fopen and then fclose, I'm pretty sure I close them correctly before I try to list them again). After that, the FindFirstFile stops finding my files returning INVALID_HANDLE_VALUE, as if they wouldn't exist. Here is the code:
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;

hFind = FindFirstFile("entities\*.ent", &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
std::cerr << "cFileListing::GetNumFiles - directory/files not found.\n";
return -1;
}
int nNumFiles = 1;

while (FindNextFile(hFind, &FindFileData) != 0)
{
nNumFiles++;
}
FindClose(hFind);


What's going on? Any clue?

##### Share on other sites
Perhaps you change your current directory?
So that the path is no longer valid. Try using an absolute path (as in 'C:\...etc).

##### Share on other sites
I tried using the absolute path and it finally worked fine. Thanks. Why does it happen only if my program reads one of the files in the path?

##### Share on other sites
Most likely something is changing the working directory. There are a few ways the working can change (e.g. calling GetOpenFileName).

In general, it's not a good idea to rely on the working directory remaining consistent throughout the life of your program. What I usually do is when the program starts up, save off the working directory and manually combine that saved working directory with any relative paths I want to use.