Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FindNextFile() in C++

Posted on 2006-11-21
2
Medium Priority
?
1,187 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 1500 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

877 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