2011-02-28 345 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; 
} 
相關問題