3

I am using the following code to iterate through files in the directory. All the files in this directory are numbered, each with a unique number.

The code below successfully prints out all the files in the directory however not in ascending order. It comes out completely random and one file is duplicated.

I can't find another way of printing out the files or printing them in ascending order. I really need to print them in ascending order and then later open them in ascending order. Any help is appreciated.

DIR *dpdf; struct dirent *epdf;

dpdf = opendir("/data/files");
if (dpdf != NULL){
   while (epdf = readdir(dpdf)){
      printf("%s\n",epdf->d_name);
         // std::out << epdf->d_name << std::endl;
   }
}
Ree
  • 863
  • 2
  • 18
  • 38

1 Answers1

7

Put the file names into a vector as you read them.

When you've read all of them, use std::sort to sort the array.

Then process the sorted file names as you see fit.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111