我试图从一个文件中读取,并从文件中创建一个所有单词的向量。我在下面尝试做的是让用户输入文件名,然后让代码打开文件,如果它们不是字母数字,则跳过字符,然后将其输入到文件中。试图读取文件并跳过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'是一个'的std ::矢量'所以'字[]'丢失的参数。 (根据[此链接](http://www.cplusplus.com/reference/stl/vector/operator [] /),没有不带参数的过载) –
ereOn
2011-02-28 23:08:23
+1 to ereOn。你会想要遍历矢量'单词'中的每个项目并输出到'cout'。 – arviman 2011-02-28 23:11:45
当这行'getline(bookread,line);'失败时会发生什么?你不检查失败。 – 2011-02-28 23:22:34