2011-02-28 9 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 :: vector 'です。したがって' words [] 'にはパラメータがありません。 ([このリンク](http://www.cplusplus.com/reference/stl/vector/operator [] /)によれば、パラメータをとらない過負荷はありません) – ereOn

+0

+1 to ereOn。ベクトル 'words'の各項目をループして' cout'に出力したいでしょう。 – arviman

+0

この行 'getline(bookread、line);'が失敗するとどうなりますか?あなたは失敗を確認しません。 –

答えて

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; 
} 
関連する問題