reading multiple file names & displaying them
hi,
wondering if someone could explain why the following doesn't work as intended ?
The 2nd for loop should display the file names, but instead it displays the same one, the last one read in.
Now I've tested reading in strings into char array and displaying them again in another for loop, and it works as intended, and I've looked/compared the addresses too but this, has me stumped.
Something I'm missing?
Code:
#include <dirent.h>
#include <iostream>
using namespace std;
int main()
{
char *afilelist[100] = { "0" };
DIR *d;
struct dirent *dir[100];
d = opendir(".");
if (d)
{
for( int x=0; (dir[x] = readdir(d)) != NULL;x++)
{
afilelist[x] = dir[x]->d_name;
cout<<afilelist[x]<<endl; //displays file names properly.
}
closedir(d);
}
cout<<endl;
for( int x=0;x<10 ; x++)
{
cout<<afilelist[x]<<endl; //doesn't display file names as per the array ?
}
cin.get();
}