I made this directory recursion programme that should output all the file in the whole directory but it does nothing

47 views Asked by At

I made this directory recursion programme that should output all the file in the whole directory but it does nothing, And if i try to cout<<line<<endl; I get a lot of dot's and dot's all over the place, I am sure that i made all the fuctions fine the problem in only in ListAllFiles Probably This is my code, pls help me figure out the problem

#include <iostream>
#include <dirent>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

void writefile(const char* file_path, string data){
    ofstream file(file_path);
    file << data;
    file.close();
}

string readfile(const char* file_path){
    ifstream file(file_path);
    string data;
    string line;
    while(getline(file, line)){
        data.append(line+'\n');
    }
    return data.substr(0, data.length()-1);
}

bool IsDir(const char *path)
{
    struct stat info;
    if(stat(path, &info) != 0)
        return 0;
    else if(info.st_mode & S_IFDIR)
        return 1;
    else
        return 0;
}

bool IsFile(string file_path) {
    if (FILE *file = fopen(file_path.c_str(), "r")) {
        fclose(file);
        return true;
    } 
    else {
        return false;
    }   
}

string ListDir(const char* _path_){
    string data;
    string path = _path_;
    if(path == "."){
        path = "";
    }
    DIR *dir;
    struct dirent *ent;
    if ((dir = opendir (_path_)) != NULL) {
        int i = 0;
        while ((ent = readdir (dir)) != NULL) {
            string temp = (path+ent->d_name).c_str();
            data.append(temp+'\n');
        }
        closedir (dir);
    } else {
        perror ("");
        return "";
    }
    return data.substr(0, data.length()-1);
}

string ListAllFiles(const char* _path_){
    string data1 = ListDir(_path_);
    string data;
    string line;
    istringstream iss(data1);
    while(getline(iss, line)){
        if(IsFile(line.c_str())){
            data.append(line+"\n");
        }
        else if(IsDir(line.c_str())){
            data.append(ListAllFiles(line.c_str())+"\n");
        }
    }
    return data.substr(0, data.length()-1);
}

int main(int argc, char *argv[]){
    cout<<ListAllFiles("C:\\Program Files\\7-Zip")<<endl;
    return 0;
}
0

There are 0 answers