2011-02-28 335 views
1

我试图从一个文件中读取,并从文件中创建一个所有单词的向量。我在下面尝试做的是让用户输入文件名,然后让代码打开文件,如果它们不是字母数字,则跳过字符,然后将其输入到文件中。试图读取文件并跳过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[]; 
} 
+2

此代码不被编译:'words'是一个'的std ::矢量'所以'字[]'丢失的参数。 (根据[此链接](http://www.cplusplus.com/reference/stl/vector/operator [] /),没有不带参数的过载) – ereOn 2011-02-28 23:08:23

+0

+1 to ereOn。你会想要遍历矢量'单词'中的每个项目并输出到'cout'。 – arviman 2011-02-28 23:11:45

+0

当这行'getline(bookread,line);'失败时会发生什么?你不检查失败。 – 2011-02-28 23:22:34

回答

2

我想我会做一点不同的工作。既然你要忽略所有,但字母数字字符,我想通过定义将其它所有字符空格一个语言环境中启动:

struct digits_only: std::ctype<char> { 
    digits_only(): std::ctype<char>(get_table()) {} 

    static std::ctype_base::mask const* get_table() { 
     static std::vector<std::ctype_base::mask> 
      rc(std::ctype<char>::table_size,std::ctype_base::space); 

     std::fill(&rc['0'], &rc['9'], std::ctype_base::digit); 
     std::fill(&rc['a'], &rc['z'], std::ctype_base::lower); 
     std::fill(&rc['A'], &rc['Z'], std::ctype_base::upper); 
     return &rc[0]; 
    } 
}; 

,使得文字阅读/从数据流的号码相当琐碎。例如:

int main() { 
    char const test[] = "This is a bunch=of-words and [email protected]#4(with)stuff to\tseparate,them, I think."; 
    std::istringstream infile(test); 
    infile.imbue(std::locale(std::locale(), new digits_only)); 

    std::copy(std::istream_iterator<std::string>(infile), 
       std::istream_iterator<std::string>(), 
       std::ostream_iterator<std::string>(std::cout, "\n")); 

    return 0; 
} 

就目前而言,我已经复制的话/数字到标准输出,但复制的载体只是意味着给不同的迭代器std::copy。为了实际使用,我们无疑也希望从std::ifstream获得数据,但是(再次)它只是提供正确的迭代器的问题。只需打开文件,用语言环境灌注它,然后阅读您的文字/数字。所有的标点符号等都会被自动忽略。

0

以下内容会读取每行,跳过非字母数字字符并将每行添加为输出向量的项目。您可以调整它,以便输出单词而不是线条。我不想提供整个解决方案,因为这看起来有点像家庭作业问题。

#include <vector> 
#include <sstream> 
#include <string> 
#include <iostream> 
#include <iomanip> 
#include <fstream> 
using namespace std; 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    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.eof())){ 
      line = ""; 
      getline(bookread, line); 


      string lineToAdd = ""; 

      for(int i = 0 ; i < line.size(); ++i) 
      { 
       if(isalnum(line[i]) || line[i] == ' ') 
       { 
        if(line[i] == ' ') 
         lineToAdd.append(" "); 
        else 
        { // just add the newly read character to the string 'lineToAdd' 
         stringstream ss; 
         string s; 
         ss << line[i]; 
         ss >> s;    
         lineToAdd.append(s); 
        } 
       } 
      } 

      words.push_back(lineToAdd); 

     } 
    } 
    for(int i = 0 ; i < words.size(); ++i) 
    cout << words[i] + " "; 


    return 0; 
} 
相关问题