I am using dirent to read filenames from a specific folder, and I want to save the names in a char* vector. It seems that it's copying some weird symbols instead of copying the filenames. This is what I tried so far:
std::vector<char*> filenames;
int filenamesAndNumberOfFiles(char *dir)
{
struct dirent *dp;
DIR *fd;
int count = 0;
if ((fd = opendir(dir)) == NULL)
{
fprintf(stderr, "listdir: can't open %s\n", dir);
return 0;
}
while ((dp = readdir(fd)) != NULL)
{
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue; /* skip self and parent */
printf("Filename: %s\n", dp->d_name);
filenames.push_back(dp->d_name);
count++;
}
closedir(fd);
return count;
}
Can anyone tell me why it doesn't copy the filenames and how could I do to copy them?
Edit: d_name is a char variable declared as:
char d_name[PATH_MAX];
and it seems like in my program PATH_MAX is equal to 260.
PS: It is my first time when I use dirent, so I am not really familiar with it.