Question

I've never used dirent.h before. I was using istringstream to read through text files (singular), but have needed to try to revise the program to read in multiple text files in a directory. This is where I tried implementing dirent, but it's not working.

Maybe I can't use it with the stringstream? Please advise.

I've taken out the fluffy stuff that I'm doing with the words for readability. This was working perfectly for one file, until I added the dirent.h stuff.

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>  // for istringstream
#include <fstream>
#include <stdio.h>
#include <dirent.h>

void main(){

    string fileName;
    istringstream strLine;
    const string Punctuation = "-,.;:?\"'!@#$%^&*[]{}|";
    const char *commonWords[] = {"AND","IS","OR","ARE","THE","A","AN",""};
    string line, word;
    int currentLine = 0;
    int hashValue = 0;

    //// these variables were added to new code //////

    struct dirent *pent = NULL;
    DIR *pdir = NULL; // pointer to the directory
    pdir = opendir("documents");

    //////////////////////////////////////////////////


    while(pent = readdir(pdir)){

        // read in values line by line, then word by word
        while(getline(cin,line)){
            ++currentLine;

            strLine.clear();
            strLine.str(line);

            while(strLine >> word){

                        // insert the words into a table

            }

        } // end getline

        //print the words in the table

    closedir(pdir);

    }
Était-ce utile?

La solution

You should be using int main() and not void main().

You should be error checking the call to opendir().

You will need to open a file instead of using cin to read the contents of the file. And, of course, you will need to ensure that it is closed appropriately (which might be by doing nothing and letting a destructor do its stuff).

Note that the file name will be a combination of the directory name ("documents") and the file name returned by readdir().

Note too that you should probably check for directories (or, at least, for "." and "..", the current and parent directories).

The book "Ruminations on C++" by Andrew Koenig and Barbara Moo has a chapter that discusses how to wrap the opendir() family of functions in C++ to make them behave better for a C++ program.


Heather asks:

What do I put in getline() instead of cin?

The code at the moment reads from standard input, aka cin at the moment. That means that if you launch your program with ./a.out < program.cpp, it will read your program.cpp file, regardless of what it finds in the directory. So, you need to create a new input file stream based on the file you've found with readdir():

while (pent = readdir(pdir))
{
    ...create name from "documents" and pent->d_name
    ...check that name is not a directory
    ...open the file for reading (only) and check that it succeeded
    ...use a variable such as fin for the file stream
    // read in values line by line, then word by word
    while (getline(fin, line))
    {
         ...processing of lines as before...
    }
}

You probably can get away with just opening the directories since the first read operation (via getline()) will fail (but you should probably arrange to skip the . and .. directory entries based on their name). If fin is a local variable in the loop, then when the outer loop cycles around, fin will be destroyed, which should close the file.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top