FindFirstFile Not working properly

Mar 22, 2011 at 5:05am
Hi Guys

I am trying to read contents of directory... using "FindNextFile(hFind, &FindFileData)"
Findfirstfile returned me then first file in the directory.. even though I have somany files in the directory (FindNextFile(hFind, &FindFileData) returning me zero.....

can anyone help me... thanks in advance...


Thanks
Prakash
Mar 22, 2011 at 5:13am
We can't point out errors without seeing your code.
Mar 22, 2011 at 5:26am
Hi Disch this is my code...

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(refFileName.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
return false;

do
{

refFileName = SeedName;
string temp;

temp.append(FindFileData.cFileName);
cout<<endl<<"Filename....: "<<temp.c_str()<<endl;

_timeb timebuffer;
_ftime( &timebuffer );
CurrentTime = U64(timebuffer.time) * 10000 + timebuffer.millitm;
if(temp.c_str() == refFileName.c_str())
{
countSeed++;
}


}while((countSeed <= noOfInstances) && ( (FindNextFile(hFind, &FindFileData) != 0) || (CurrentTime < StopTime)));
FindClose(hFind);
Mar 22, 2011 at 6:18am
Are you sure FindNextFile is the problem here? Because it doesn't look like it.

I have no idea what your _ftime and related stuff is doing. Maybe that's contributing to the problem.

Other things I noticed:

1
2
//if(temp.c_str() == refFileName.c_str())  //BAD
if(temp == refFileName)  // GOOD 


You can't use the == operator to compare two c-style strings (char arrays). Use it on std strings instead.

Also, was countSeed ever initialized? I don't see it anywhere in that code. If the loop is only running once, my money is that it's because countSeed > noOfInstances.

The only way I can see FindNextFile being the problem is if refFileName is something weird. What is that when FindFirstFile is called?
Last edited on Mar 22, 2011 at 6:19am
Mar 22, 2011 at 10:14am
Thank you disch _time and _timeb are time buffers.. reFileName is some reference filename I have taken.
Mar 22, 2011 at 2:17pm
We know. But they're not initialised.

The file times are in the buffer, FindFileData, in fields ftCreationTime, ftLastAccessTime and ftLastWriteTime. You need to convert the one you're interested in with FileTimeToLocalTime.
Mar 24, 2011 at 6:30am
Also refFileName will contain the compleate path including the name of the file while temp will only contain the name of the file . so
1
2
3
4
if(temp.c_str() == refFileName.c_str()) 
{
countSeed++;
}


so this condition will always be false . and the countSeed will not be increament .
Mar 25, 2011 at 9:04am
@bluecoder, thanks alot for your kind info. and
@Kbw Iam using the buffers just to hold the current time and stop time. before the while loop it is initialized...

Thanks alot for your kind support....
Prakash
Mar 25, 2011 at 9:06am
Guys problem solved....
It needs some settings to be changed in the properties..
In project properties just change the character set from unicode to multibyte character set....:)


Thanks
Prakash
Topic archived. No new replies allowed.