Solved

FindNextFile() in C++

Posted on 2006-11-21
2
1,098 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now