Solved

FindNextFile() in C++

Posted on 2006-11-21
2
1,113 Views
Last Modified: 2012-08-14
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);      
0
Comment
Question by:ucom-net
2 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 17993537
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!
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 17993733
>>>> You need to perform a custom sort

You might use the following as a compare function.

Regards, Alex

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool compareFileNamesLess(const std::string& f1, const std::string& f2)
{
    int l1 = f1.length();
    int l2 = f2.length();
    int l  = (l1 < l2)? l1 : l2;

    for (int i = 0; i < l; ++i)
    {
        char c1 = toupper(f1[i]);
        char c2 = toupper(f2[i]);
        if (c1 == c2)                              
            continue;
        if (isdigit(c1) && isdigit(c2))
        {
            int n1 = atoi(f1.substr(i).c_str());
            int n2 = atoi(f2.substr(i).c_str());
            if (n1 != n2)
                return (n1 < n2);
        }
        return c1 < c2;   // note, "file01" < "file1"
    }
    return l1 < l2;
}

int main()
{
    vector<string > files;
    files.push_back("file1.dat");
    files.push_back("file10.dat");
    files.push_back("file101.dat");
    files.push_back("file2.dat");
    files.push_back("file3.dat");
    files.push_back("file20.dat");

    sort(files.begin(), files.end(), compareFileNamesLess);

    for (int i = 0; i < files.size(); ++i)
        cout << files[i] << ' ';
    return 0;
}


0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question