0

I want to read all the files of a directory and print the words in it, all I've done is to store the names of the files to a string array but now how would I do that...

my code for storing file names:

struct dirent *contents;
DIR *dir;
dir= opendir((char*)"text files");
DIR *op;
op= opendir((char*)"text files");
if(!dir){
    cout<<"The given directory is not found";
}
else {
    string *arr;
    int count;
    int i=0;
    while ((readdir(op)) != NULL){
        count++;
    }//for the size array equivalent to number of txt files in directory
    count=count-2;
    arr=new string[count];
    while ((contents = readdir(dir)) != NULL) {
        string name = contents->d_name;
        if (name != "."&&name!="..") {
            arr[i]=name;
            i++;
        }
    }
    cout<<"\t*The list of files are*"<<endl;
    for(int j=0;j<count;j++){
        cout<<arr[j]<<endl;
    }
}}

Any guidance

genpfault
  • 51,148
  • 11
  • 85
  • 139
James
  • 1
  • 1
  • 1
  • What do you mean by print words? Like print words of each file in a directory? – kiner_shah Oct 31 '21 at 07:34
  • yes print the words of each files in a directory – James Oct 31 '21 at 07:38
  • 1
    Think of a simple algorithm: open a dir, get the list of all files, sub-directories in it, for each file, read the contents and store it, for each sub-directory repeat the same process (recursion). – kiner_shah Oct 31 '21 at 07:39
  • I dont know how to that thats why I asked – James Oct 31 '21 at 07:42
  • 1
    To read a file, you can use `std::ifstream` ([See this](https://stackoverflow.com/a/18816228)) – kiner_shah Oct 31 '21 at 07:45
  • i know that we use fstream but the thing I'm confused is that once the names are stored in an array , how would I read the files respective of that names – James Oct 31 '21 at 07:51
  • 1
    @James You should do yourself a big favor and use `std::vector` instead of pointers, raw memory, and `new string[]`. – PaulMcKenzie Oct 31 '21 at 07:51
  • Don't store names of the files, rather store the absolute path to the files. Then you can easily access them. – kiner_shah Oct 31 '21 at 07:52
  • @kiner_shah I'll look forward to it – James Oct 31 '21 at 07:55
  • 2
    @James Also, `opendir()` and `readdir()` are old, consider using the standard [``](https://en.cppreference.com/w/cpp/filesystem) library instead, in particular [`std::filesystem::path`](https://en.cppreference.com/w/cpp/filesystem/path) and [`std::filesystem::directory_iterator`](https://en.cppreference.com/w/cpp/filesystem/directory_iterator). – Remy Lebeau Oct 31 '21 at 08:09

1 Answers1

2

The simplest solution to your problem, as I can understand it, is to use one of the loops from main below:

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    // print all files from the current directory and below
    for(auto& p: fs::recursive_directory_iterator(fs::current_path()))
        std::cout << p.path() << '\n';
    // print all files from the current directory
    for(auto& p: fs::directory_iterator(fs::current_path()))
        std::cout << p.path() << '\n';
}

This is C++17 standard code. This is modern code. If you went as far as to tackle the problem with the C interface, you'll know what to do with these loops.

The (technical) documentation of the filesystem standard library is, for example, here: https://en.cppreference.com/w/cpp/filesystem This library has all you need, including utilities to tell ordinary files from symbolic links, directory names etc. Paths can be easily converted to std::strings via their string family member functions, see: https://en.cppreference.com/w/cpp/filesystem/path

zkoza
  • 2,644
  • 3
  • 16
  • 24