Mingw x86_64 v11.2.0
Windows 10 21H2
My current problem is that this function can normally read files and folders in the directory, but if it is saved in the pointer and printed on the screen, a single file will appear repeatedly.
These are the files in the directory.
.
..
main.c
main.exe
README.md
test.txt
The following is the source code I wrote:
#include "../DeleteCompletely.h"
#include <dirent.h>
#define TotalNumber      4096
#define TotalFileNameLen 4096
typedef struct dirent DIRENT;
typedef struct {
    DIR    *dir_ptr;
    DIRENT *dirent_ptr;
    char  **pathSet;
    size_t  number;
} snDIR;
static snDIR *getAllFile(const char *path)
{
    snDIR *dirSet = (snDIR *)malloc(sizeof(snDIR));
    dirSet->dir_ptr = opendir(path);
    size_t loopIndex;
    dirSet->pathSet = (char **)malloc(sizeof(char **) * TotalNumber);
    if(dirSet->dir_ptr) {
        dirSet->number = 0;
        loopIndex = 0;
        while ((dirSet->dirent_ptr = readdir(dirSet->dir_ptr)) != NULL) {
            dirSet->pathSet[loopIndex] = (char *)malloc(TotalFileNameLen);
            dirSet->pathSet[loopIndex] = dirSet->dirent_ptr->d_name;
            dirSet->number++;
            loopIndex++;
        }
        closedir(dirSet->dir_ptr);
    }
    return dirSet;
}
int main(int argc, char **argv)
{
    snDIR *set = getAllFile(".");
    for(size_t x = 0; x < set->number; ++x) {
        printf("%s\n", set->pathSet[x]);
    }
    free(set);
    return 0;
}
 
    