I have a directory with pictures, where three different kind of pictures are stored. They all look like this:
abc_1_1_300_0.5.png
dfg_1_1_300_0.5.png
jkl_1_1_300.0.5.png
The numbers can change (placeholders here), however there are always three files belonging together, which have the same number. However, using the answer provided here: Get all files from dir will mix up the entries, so that the list of files is completely unsorted like:
dfg_1_1_237_0.5.png
abc_1_1_1_0.5.png
abc_1_3_17_0.5.png
...
...and so on.
I know that the files are crated chronologically, so all files belonging together have been created around the same time. I found this answer: Solution with Boost. But this only works with Boost, and I do not want to add another dependency to my project.
So my question is, how can I this without Boost?
Here is what I have come up with, which is basically just a little bit of modified code from the first answer:
int main(int argc, char* argv[])
{
char file1[100];
char file2[100];
char file3[100];
//init to 1 to skip dots
int jump = 1;
if(argc)
jump = jump + 3*atoi(argv[1]);
DIR *dir;
struct dirent *ent;
int filecnt = 0;
if ((dir = opendir ("/home/khoefle/Downloads/renderer/data/Earrings/depth")) != NULL) {
while ((ent = readdir (dir)) != NULL && filecnt != jump) {
printf ("%s\n", ent->d_name);
if(filecnt % 3 == 0)
sprintf(file1,"%s",ent->d_name);
if((filecnt+1) % 3 == 0)
sprintf(file2,"%s",ent->d_name);
if((filecnt+2) % 3 == 0)
sprintf(file3,"%s",ent->d_name);
printf("%d\n",filecnt);
filecnt = filecnt + 1;
}
closedir (dir);
} else {
perror ("");
return EXIT_FAILURE;
}
printf("first file %s\n",file1);
printf("second file %s\n",file2);
printf("third file %s\n",file3);
}
The program gets a "jump" variable, so the loop iterating over the files jumps to a specific point of three files inside the directory.