Spera Spera - 3 months ago 33
C++ Question

Directory lister program is faulty , C++

I am making a program to collect directory and user folder names , keep in mind that I am doing this as a hobby although I might take Computer Science next year if I am lucky.

Language : C++

IDE : Code Blocks

Problem : It is suppose to create a text file with a list of files in the directories below , but when I run it , it does not do that , what it does is nothing.

Code :

void directory_lister() // List folders in respective directories.
{
string dir_hos = " ";
int cycle = 0;
string dir_program = "C:\\Program Files\\";
string dir_program32 = "C:\\Program Files (x86)\\";
string dir_users = "C:\\Users\\";

while (cycle < 3){

if(dir_hos != dir_program){
dir_hos = dir_program;
cycle++;

DIR *dir = NULL;
struct dirent *drnt = NULL;
opendir(dir_hos.c_str());

while((drnt = readdir(dir))) // Faulty code #1.
{
w_file("directorylist.txt",drnt->d_name);
}
closedir(dir);

cout << cycle;
}else if(dir_hos != dir_program32){
dir_hos = dir_program32;
cycle++;

DIR *dir = NULL;
struct dirent *drnt = NULL;
opendir(dir_hos.c_str());

while((drnt = readdir(dir))) // Faulty code #2.
{
w_file("directorylist.txt",drnt->d_name);
}
closedir(dir);

cout << cycle;
}else if(dir_hos == dir_users){
dir_hos = dir_users;
cycle++;

DIR *dir = NULL;
struct dirent *drnt = NULL;
opendir(dir_hos.c_str());

while((drnt = readdir(dir))) // Faulty code #3.
{
w_file("directorylist.txt",drnt->d_name);
}
closedir(dir);

cout << cycle;
}

}

return;
}

int main(){
directory_lister();
return 0;
}


Also w_file function is this :

void w_file(string filename_s, char * content)
{
// Change string to const char *
const char * filename_cc = filename_s.c_str();

// Writing File Code
ofstream file;
file.open(filename_cc,ios::app);
file << content << endl;
file.close();
return;
}





Compiler output :

Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))




Faulty code #1,#2,#3 :

Before this I do not have extra brackets , e.g while(drnt = readdir(dir)) , but the compiler gives me 3 warning about not enough brackets (compiled successfully but does not work) , so I add them but it still does not work.

I also tried using "==" instead of "=" but it hanged the program and it crashed. Before it crash it managed to make "directories.txt" .




Others :

I think there are other codes that I have not properly written but I can't figure it out since I am bad at programming. Can you guys help me fix it? I am sure I will learn something out of this . Thank you!




On another note :

I am not particularly good at using dirent.h , every time I use it I am so blur , I usually look up examples and copy the simplest ones to use (I am ashamed but I am very confused when reading the documentation).

Answer

The argument supplied to readdir() is supposed to be a value previously returned by opendir() - supplying a null pointer as you are causes an error.

The value returned by opendir() is never stored in a variable - which means it is immediately discarded, and certainly never passed to readdir().

It is usually a good idea to check the return values of functions. For example, opendir() returns a null pointer on error.

I haven't looked closely enough to determine if there are other problems .... but fixing the problems I have identified will be educational for you anyway.

Comments