Listing the file in the directory and save all relevent information to array or vector or something else
Originally, i am using MFC. now changing to use dirent.h
now i doing this thing
=================================================
#include <dirent.h> // directory header
#include <iostream> // input/output stream
//#include <afx.h>
// use a few things from the std namespace for console I/O
using namespace std;
int main () // entry point of the program
{
// first off, we need to create a pointer to a directory
DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
pdir = opendir ("C:\\"); // "." will refer to the current directory
struct dirent *pent = NULL;
// I used the current directory, since this is one which will apply to anyone reading
// this tutorial~ If I said "C:\\" and you're on Linux, it may get a little confusing!
if (pdir == NULL) // if pdir wasn't initialised correctly
{ // print an error message and exit the program
cout << "\nERROR! pdir could not be initialised correctly";
exit (3);
} // end if
char ** data = new char*[50];
int i = 0;
while (pent = readdir (pdir)) // while there is still something in the directory to list
{
if (pent == NULL) // if pent has not been initialised correctly
{ // print an error message, and exit the program
cout << "\nERROR! pent could not be initialised correctly";
exit (3);
}
// otherwise, it was initialised correctly. Let's print it on the console:
cout << pent->d_name << endl;
cout << "number : " << i << endl;
data[i] = pent->d_name;
cout << data[0] << endl;
i++;
}
// finally, let's close the directory
closedir (pdir);
cout << i << endl;
cout << data[1] << endl;
// cin.get(); // pause for input
return EXIT_SUCCESS; // everything went OK
}
=====================================================================
However, the char ** data cannot work .
It only show one item.
For example
the directory have
a.txt
b.txt
windows
now
the data[0] = windows
data[1] = windows
data[2] = windows
when you use the opendir function, the system allocates a dir sized structure on the heap and returns you a pointer to this structure. lets call this pointer pdir
[Note that dirent structure is part of this DIR structure].
Now when you call readdir, you pass the pdir that you obtained earlier, and if readdir succeeds, then it will fill in the d_name member of the dirent structure member of the DIR structure.
iMPORTANT:
BECAUSE IT IS THE SAME DIR STRUCTURE THAT IS CONTINUALLY BEING REUSED THIS MEANS THAT YOUR POINTER TO DIRENT IS ALWAYS THE SAME.
So becasue you are copying the address of the d_name to the char* array, you are copying the the same address each time.
So when the readdir finds no more names to read form the directory, it returns 0 to indicate finished, so but the last name read still remains in d_name.
Note also - the Opendir function allocated the DIR structure, and when you call CloseDir it deallocates it.
But because your array pointers are still pointing into the (now deallocated) DIR structure - then you may possibly have problems if you try to print out your char pointers.
while (pent = readdir (pdir)) // while there is still something in the directory to list
{
//this is redundant code - as the loop will only run if pent is true
//then pent can't be false at this point.
if (pent == NULL)
{
cout << "\nERROR! pent could not be initialised correctly";
exit (3);
}
//more code
} //end loop