【发布时间】:2023-03-04 19:10:01
【问题描述】:
我以前从未使用过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);
}
【问题讨论】:
-
请注意,
void main()不是 C++ 中主程序的有效原型之一(并且在 C 中是非标准的)。 -
嗨,我很抱歉,我不知道投票!已经回去并解决了这个问题。谢谢你的提醒:)
标签: c++ directory opendir istringstream dirent.h