2011-12-03 19 views
20

私は、各行に1つの単語が含まれているテキストファイルの各行を読み込み、それらの単語をベクトルに入れようとしています。それをどうやってやるの?テキストファイルから行を読み込み、文字列をベクトルに入れますか?

これは私の新しいコードです:私はまだそれに何か間違っていると思います。

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

int main() 
{ 
    std::string line; 
    vector<string> DataArray; 
    vector<string> QueryArray; 
    ifstream myfile("OHenry.txt"); 
    ifstream qfile("queries.txt"); 

    if(!myfile) //Always test the file open. 
    { 
     cout<<"Error opening output file"<<endl; 
     system("pause"); 
     return -1; 
    } 
    while (std::getline(qfile, line)) 
    { 
     QueryArray.push_back(line); 
    } 
    if(!qfile) //Always test the file open. 
    { 
     cout<<"Error opening output file"<<endl; 
     system("pause"); 
     return -1; 
    } 

    while (std::getline(qfile, line)) 
    { 
     QueryArray.push_back(line); 
    } 

    cout<<QueryArray[0]<<endl; 
    cout<<DataArray[0]<<endl; 

} 
+2

これまでのコードではどういう問題がありますか? – Mahesh

+0

@Mahesh this * if(!myfile)*が最初の問題かもしれません。 (すみません.. STLを学ぶ必要があります) – Beginner

+0

@RomanB:その行には何も問題ありません。 – Puppy

答えて

29

@FailedDevは、もっとも単純な形式を示しました。別の方法として、ここで私は、多くの場合、そのループをコーディングする方法である:

std::vector<std::string> myLines; 
std::copy(std::istream_iterator<std::string>(myfile), 
      std::istream_iterator<std::string>(), 
      std::back_inserter(myLines)); 

全体のプログラムは次のようになります。私は省いてる

std::vector<std::string> lines; 
for (std::string line; std::getline(ifs, line); /**/) 
    lines.push_back(line); 

// Avoid "using namespace std;" at all costs. Prefer typing out "std::" 
// in front of each identifier, but "using std::NAME" isn't (very) dangerous. 
#include <iostream> 
using std::cout; 
using std::cin; 
#include <fstream> 
using std::ifstream; 
#include <string> 
using std::string; 
#include <vector> 
using std::vector; 
#include <iterator> 
using std::istream_iterator; 
#include <algorithm> 
using std::copy; 

int main() 
{ 

    // Store the words from the two files into these two vectors 
    vector<string> DataArray; 
    vector<string> QueryArray; 

    // Create two input streams, opening the named files in the process. 
    // You only need to check for failure if you want to distinguish 
    // between "no file" and "empty file". In this example, the two 
    // situations are equivalent. 
    ifstream myfile("OHenry.txt"); 
    ifstream qfile("queries.txt"); 

    // std::copy(InputIt first, InputIt last, OutputIt out) copies all 
    // of the data in the range [first, last) to the output iterator "out" 
    // istream_iterator() is an input iterator that reads items from the 
    // named file stream 
    // back_inserter() returns an interator that performs "push_back" 
    // on the named vector. 
    copy(istream_iterator<string>(myfile), 
     istream_iterator<string>(), 
     back_inserter(DataArray)); 
    copy(istream_iterator<string>(qfile), 
     istream_iterator<string>(), 
     back_inserter(QueryArray)); 

    try { 
     // use ".at()" and catch the resulting exception if there is any 
     // chance that the index is bogus. Since we are reading external files, 
     // there is every chance that the index is bogus. 
     cout<<QueryArray.at(20)<<"\n"; 
     cout<<DataArray.at(12)<<"\n"; 
    } catch(...) { 
     // deal with error here. Maybe: 
     // the input file doesn't exist 
     // the ifstream creation failed for some other reason 
     // the string reads didn't work 
     cout << "Data Unavailable\n"; 
    } 
} 
+0

私は何が必要なのですか? – user977154

+1

甘いそれは働くようになった。どうもありがとうございます。これは間違いなくもっと簡単で清潔なsoooです。 – user977154

+0

@ user977154上記の完全な例を参照してください –

28

最も単純な形式:

std::string line; 
std::vector<std::string> myLines; 
while (std::getline(myfile, line)) 
{ 
    myLines.push_back(line); 
} 

狂気のC鮫のための必要はありません:)

編集:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

int main() 

{ 
    std::string line; 
    std::vector<std::string> DataArray; 
    std::vector<std::string> QueryArray; 
    std::ifstream myfile("OHenry.txt"); 
    std::ifstream qfile("queries.txt"); 

    if(!myfile) //Always test the file open. 
    { 
     std::cout<<"Error opening output file"<< std::endl; 
     system("pause"); 
     return -1; 
    } 
    while (std::getline(myfile, line)) 
    { 
     DataArray.push_back(line); 
    } 

    if(!qfile) //Always test the file open. 
    { 
     std::cout<<"Error opening output file"<<std::endl; 
     system("pause"); 
     return -1; 
    } 

    while (std::getline(qfile, line)) 
    { 
     QueryArray.push_back(line); 
    } 

    std::cout<<QueryArray[20]<<std::endl; 
    std::cout<<DataArray[12]<<std::endl; 
    return 0; 
} 

キーワード使用は違法C++です!絶対に使用しないでください。 OK?良い。あなたが書いたものと私が書いたことを比較して、違いを見つけようとしましょう。あなたはまだ質問が戻ってくる場合。

+0

私のポストでコードを修正しましたが、今何が間違っていますか?私は2つの異なるテキストファイルで作業する必要があるからです。途中で助けてくれてありがとう。 – user977154

+0

@ user977154外側whileループは必要ありません。それを除く!両方の場合において。また、あなたのベクターには12行と20行が存在するのでしょうか? – FailedDev

+0

はい私はテストファイルに20以上の行があります。そして、エラー出力ファイルを開いているとエラーが表示され続けます – user977154

16

最も簡単なバージョン含まれ、他の塊。私のバージョンはFailedDevのものとほぼ同じですが、 'for'ループを使用することで、ループ内に 'line'という宣言を置きます。これは、行数を減らすための単なるトリックではありません。これにより、行の範囲が減少します。これはforループの後に消えます。すべての変数は可能な限り小さなスコープを持つ必要があります。したがって、これが優れています。 forループは素晴らしいです。

+0

優れています。それは 'using namespace std; 'でよりクリーンで、' std :: 'はすべて削除できます。 'ifs'宣言が欠落しています: 'ifstream ifs(textFilePath、ios :: in);' –

関連する問題