我以前从未使用dirent.h
过。我正在使用 istringstream 读取文本文件(单数),但需要尝试修改程序以读取目录中的多个文本文件。这是我尝试实现 dirent 的地方,但它不起作用。
也许我不能将它与字符串流一起使用?请指教。
为了便于阅读,我已经去掉了我用单词做的蓬松的东西。这对一个文件非常有效,直到我添加了 dirent.h 的东西。
#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);
}