0

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.

Dev Null
  • 4,731
  • 1
  • 30
  • 46
Kev1n91
  • 3,553
  • 8
  • 46
  • 96
  • Roughly like this : store the filenames in an array and sort it using [`qsort`](http://www.cplusplus.com/reference/cstdlib/qsort/). – Jabberwocky Aug 08 '17 at 10:40
  • @MichaelWalz or in a `std::vector`. Or directly in a `std::set` since all filenames should be unique. – muXXmit2X Aug 08 '17 at 10:41
  • @muXXmit2X Yes, `std::vector` is definitely better. I overlooked the C++ tag. – Jabberwocky Aug 08 '17 at 10:42
  • 1
    You don't have Boost, but do you have C++17? Then you can use `std::filesystem`. – ypnos Aug 08 '17 at 11:04
  • Thank you for your answers, I am using c++11 (it is nvcc with g++ mixed) – Kev1n91 Aug 08 '17 at 11:18
  • @MichaelWalz thank you, would you recommend using only one vector, or three vetor (each one for a filename) – Kev1n91 Aug 08 '17 at 11:19
  • @MichaelWalz No, that's not what I meant. In consideration of the later sorting process: Will it be easier to sort one array that holds all specific filenames from the first type(starting with abc), one vector with all the files for the second type (starting with dfg) and so on... or to store them all into one vector, so that there is only one vector, with all the filenames – Kev1n91 Aug 08 '17 at 11:25
  • @Kev1n91 forget my comment, it was stupid, I just deleted it. For your question concernijg using 1 or 3 arrays: it dépends on your exact requirements. – Jabberwocky Aug 08 '17 at 11:27
  • Thank you really much, np, I will open a new question considering the sorting then – Kev1n91 Aug 08 '17 at 11:28
  • std::strings and std::vector are a vast improvement over what you've got so far. But strings typically sort lexicographically and I doubt lexicographic sort (of the full file name) will do what you want. You should consider adding a compare function (for any of the std sorts) on the std::string file name-without-the-leading-4-chars (i.e. substr). – 2785528 Aug 08 '17 at 12:32

0 Answers0