I wanted to read out the absolute file-path (filename) as sorted in a folder (on Linux). The reading the file-paths is ok but I have problems in sorting.
selectedDirectory = fl_dir_chooser ("Select Imagedirectory:",NULL,0); //This is just a widget to show the folder.
DIR *d;
dirent *de;
// char pfad[256];
numberOfFilesOnSelectedFolder= 0;
d=opendir(selectedDirectory); // Here you can enter the folder path instead selectedDirectory
while((de=readdir(d))){
numberOfFilesOnSelectedFolder++;
}
closedir(d);
d= opendir(selectedDirectory);
std::vector <std::string> result;
char buffer[500];
int n;
if (d) {
while (true) {
de= readdir(d);
if (de == NULL) break;
n= sprintf (buffer, "%s/%s", selectedDirectory, de->d_name);
result.push_back( std::string(buffer));
}
closedir(d);
std::sort( result.begin(), result.end() );
}
cout << "result 3:" << result.at(0) << endl << result.at(1) << endl << result.at(2) << endl << result.at(3) << endl << result.at(4) << endl << result.at(5) << endl << result.at(6) << endl;
If I run this code and want determine what the 0th, 1st, 2nd, 3rd etc. files in the folder are, then I get the following answer:
result 3:/home/csad6517/Desktop/ViSeCut/Neck2/.
/home/csad6517/Desktop/ViSeCut/Neck2/..
/home/csad6517/Desktop/ViSeCut/Neck2/Unbenannt-105.dcm
/home/csad6517/Desktop/ViSeCut/Neck2/Unbenannt-106.dcm
/home/csad6517/Desktop/ViSeCut/Neck2/Unbenannt-107.dcm
/home/csad6517/Desktop/ViSeCut/Neck2/Unbenannt-36.dcm
/home/csad6517/Desktop/ViSeCut/Neck2/Unbenannt-37.dcm
The files -105.dcm, -106.dcm, -107.dcm lie in the folder at the bottom and -36.dcm, -37.dcm- at the top. The program compares 1 and 3 of 105 and 37, 1 is lesser than 3, then prints out first, but does not know that 105 is three digits and 37 is two digits. How can I solve this problem?