Solved

FindNextFile() in C++

Posted on 2006-11-21
2
1,141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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