Solved

FindNextFile() in C++

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
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 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…

772 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

12 Experts available now in Live!

Get 1:1 Help Now