파일에서 읽으려고하고 파일에서 모든 단어의 벡터를 만들려고합니다. 내가 아래에서 시도한 것은 사용자가 파일 이름을 입력하게 한 다음 코드가 파일을 열고 영숫자가 아닌 문자를 건너 뛰고 파일에 입력하는 것입니다.파일에서 읽으려고하고 C++에서 구두점을 건너 뜁니다.
바로 지금 파일 이름을 입력하면 바로 닫힙니다. 내가 뭘 잘못했는지 알기나 해?
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string line; //for storing words
vector<string> words; //unspecified size vector
string whichbook;
cout << "Welcome to the book analysis program. Please input the filename of the book you would like to analyze: ";
cin >> whichbook;
cout << endl;
ifstream bookread;
//could be issue
//ofstream bookoutput("results.txt");
bookread.open(whichbook.c_str());
//assert(!bookread.fail());
if(bookread.is_open()){
while(bookread.good()){
getline(bookread, line);
cout << line;
while(isalnum(bookread)){
words.push_back(bookread);
}
}
}
cout << words[];
}
이 코드는 컴파일되지해야'words' 그래서'말은 [] '매개 변수 누락되는'표준 : : 벡터'이다. ([이 링크] (http://www.cplusplus.com/reference/stl/vector/operator [] /)에 따르면 매개 변수를 사용하지 않는 과부하가 없음) –
ereOn
+1 to ereOn. 벡터'words'의 각 항목을 순환하여'cout'으로 출력하고 싶을 것입니다. – arviman
이 줄'getline (bookread, line);이 실패하면 어떻게됩니까? 실패 여부는 확인하지 않습니다. –