Having troubles with understanding the assignment to the pointers (using <dirent.h>)

Let's say I have 11 files with extension ".exe" files in the folder c:/.
And I want to create 11 pointers of type char*, each of this pointer contains the file name of the .exe file. So, we have 11 pointers - 11 file names.
But the output is what really confuses me (see below).

My implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    #include <dirent.h>

    // some code here

    DIR *dir;
    struct dirent *ent;
    char** FileName = new char* [11]; // Creating 11 pointers of type char*
    count = 0; //counting the events when .exe file detected
    dir = opendir("c:/");
    while ((ent = readdir (dir)) != NULL) // read directory file by file until there is nothing
          {
                string matchName = string(ent->d_name);
                if (matchName.find(".exe") != std::string::npos) // Finding files with 
                                                                 //.exe extension only
                {
                   FileName[count] = ent->d_name;
                   cout << "count = " << count << ": " << FileName[count] << endl;
                   count++; // There are 11 .exe files in the folder, so the maximum
                            // of the count=11 (from FileName[0] to FileName[10])
                }
          }
    closedir (dir);
    
    // Here I'm just checking the output
    cout << "count = 0: " << FileName[0] << endl;
    cout << "count = 1: " << FileName[1] << endl;
    cout << "count = 2: " << FileName[2] << endl;
    cout << "count = 3: " << FileName[3] << endl;
    cout << "count = 4: " << FileName[4] << endl;
    cout << "count = 5: " << FileName[5] << endl;
    cout << "count = 6: " << FileName[6] << endl;
    cout << "count = 7: " << FileName[7] << endl;
    cout << "count = 8: " << FileName[8] << endl;
    cout << "count = 9: " << FileName[9] << endl;
    cout << "count = 10: " << FileName[10] << endl;


My output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
count = 0: file0.exe
count = 1: file1.exe
count = 2: file2.exe
count = 3: file3.exe
count = 4: file4.exe
count = 5: file5.exe
count = 6: file6.exe
count = 7: file7.exe
count = 8: file8.exe
count = 9: file9.exe
count = 10: file10.exe

count = 0:  // just empty for all 11 files
count = 1: 
count = 2: 
count = 3: 
count = 4: 
count = 5: 
count = 6: 
count = 7: 
count = 8: 
count = 9: 
count = 10:


Why do I have correct result while I'm inside the while-loop, but then when I'm checking the same values of the pointers outside the loop - it's just empty?
I have no idea how to fix that...
Any help will be appreciated.
Thank you!
Last edited on
readir() might reuse the same dirent object each time it's called so if you want to hang on to the filenames you will have to copy them elsewhere.

Instead of using an array of pointers why don't you use a vector of strings (std::vector<std::string>)? I think that would make life much easier for you.
That is THE solution to that!
Thank you very much, Peter87!
Topic archived. No new replies allowed.