Storing filenames in an array
Hi,
I'm making a program, which reads the filenames from a directory, and stores them in an array of strings, but when I'm running the program, it stores only 1 filename 20 times in the whole array.
Here's my code:
Code:
#include <stdio.h>
#include <dirent.h>
int main(void)
{
char *NameArr[20]; // the array which will store the filenames
DIR *dir;
int i=0,k;
struct dirent *ent;
dir = opendir ("c:\\");
if (dir != NULL)
{
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
NameArr[i]=ent->d_name;
/* save filenames in the array */
i++;
}
closedir (dir);
}
else
{
/* could not open directory */
perror ("");
}
for (k=0;(k<(i+1)) && (k<20);k++) printf("%s\n",NameArr[k]);
//print the filenames on the screen
return 0;
}
Thank you for any help!