전에 dirent.h
을 사용한 적이 없습니다. 텍스트 파일 (단수)을 읽는 데 istringstream을 사용했지만 디렉토리의 여러 텍스트 파일을 읽도록 프로그램을 수정해야했습니다. 이것은 dirent를 구현하려고 시도했지만 작동하지 않습니다.<dirent.h>을 처음 사용하여 디렉토리의 데이터에 액세스하려고 시도했습니다.
아마도 나는 stringstream과 함께 사용할 수 없습니까? 제발 조언.
가독성을 높이기 위해 내가하고있는 솜털 같은 것을 꺼 냈습니다. 이 은 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 = "-,.;:?\"'[email protected]#$%^&*[]{}|";
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에서는 비표준 임) 유의하십시오. –
안녕하세요, 매우 유감입니다. 나는 upvoting에 대해 몰랐습니다! 돌아가서 이것을 치료했습니다. 머리를 가져 주셔서 감사합니다 :) –