Link to home
Start Free TrialLog in
Avatar of ucom-net
ucom-net

asked on

FindNextFile() in C++

I have some .eps files in folder and I would like to read their names using C++. I am using FindFirstFile() and FindNextFile() Win32 API to read file names. My file names are as Sample1.eps, Sample2.eps,....,Sample12.eps. I would like to read all the file names sequentially i.e. from Sample1 to Sample12. My code is reading all file names but they are not in sequence. It reads Sample1.eps first then it reads Sample10.eps instead of Sample2.eps. If I change file names to Sample01.eps, Sample02.eps,..., Sample12.eps. Then my code reads all the file names sequentially.

But I would like to keep my file names as Sample1.eps, Sample2.eps etc. Does anybody help me to solve this problem?

I am attaching my file read code below:
    WIN32_FIND_DATA fd;
    HANDLE handle = FindFirstFile("C:\\eps\\*.eps", &fd);
    if(handle == INVALID_HANDLE_VALUE)
      {
        // error
        cout << "Invalid file handle." << endl;
         return -1;
      }
    do
      {
        eps_vector.push_back(fd.cFileName);
      } while(FindNextFile(handle, &fd));
                FindClose(handle);      
Avatar of Axter
Axter
Flag of United States of America image

Hi ucom-net,
> But I would like to keep my file names as Sample1.eps, Sample2.eps etc

You need to perform a custom sort, in which you put your desired sort pattern logic.

David Maisonave (Axter)
Cheers!
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial