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);
}
You should be using
int main()and notvoid main().You should be error checking the call to
opendir().You will need to open a file instead of using
cinto 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 byreaddir().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:
The code at the moment reads from standard input, aka
cinat the moment. That means that if you launch your program with./a.out < program.cpp, it will read yourprogram.cppfile, 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 withreaddir():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). Iffinis a local variable in the loop, then when the outer loop cycles around,finwill be destroyed, which should close the file.