2017-06-18 22 views
0

私は、ファイルから2次元ベクトルへ一連の数値を読み取ろうとしてきましたが、大きな成功はありませんでした。ファイルから数列を読み取る方法は?

私は未知の長さ(フォーマット)の数値のシーケンスを読んで、いくつかの問題に遭遇したsequences.txt

1,2,3,4,5 
6,3,1 
7,4,10,4,1,9 

のデータ。 例えばそれは3の数値形式を固定した場合、入力は次のようになります。それは、

input_stream >> integer >> char >> integer >> char >> integer 

しかし、各シーケンスの番号の数は不明である...

もう一つの問題は、','文字ですどういうわけか、シーケンスの最後の番号に行を移動してから、行を終了した後にそれを選択することが可能であることを意味する、各シーケンスの最後にはありませんか?コードの

私の試み:

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

void PrintMatrix(std::vector<std::vector<int>> matrix) 
{ 
    for(auto v : matrix) 
    { 
     for(auto x : v) 
     { 
      std::cout << x << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

int main() 
{ 
    std::fstream input_stream("sequences.txt", std::ios::in); 
    if(!input_stream) 
     std::cout << "Problem with opening a file"; 
    else 
    { 
     std::vector<std::vector<int>> matrix; 
     while(input_stream.good()) 
     { 
      std::vector<int> v; 
      int number; 
      char chr; 
      while(input_stream >> number >> chr) 
      { 
       v.push_back(number); 
      } 
      matrix.push_back(v); 
     } 
     PrintMatrix(matrix); 
    } 
    return 0; 
} 

は、私は非常に最近のファイルを学び始めたが、私は今、練習しようとし文学のほとんどとを読むので、この問題を解決する方法上の任意のヒントやでの作業上の任意のヒント一般的なファイル、私は非常に感謝する:)

ありがとう

PS私は(間違っている)、このコードから取得 出力:私は取得したい1 2 3 4 5

出力:ここ

1 2 3 4 5 
6 3 1 
7 4 10 4 1 9 
+0

読むためにいくつかの時間がかかるしてください[なぜのiostream :: eofの間違った考えられてループ条件の内側?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a- loop-condition-considered-wrong)例を使用して'input_stream.good()'はちょうど同じで悪いです。 –

+0

また、 'const'と' references'を使って渡すことについても覚えています。 - 印刷機能のコピーをたくさん節約できます。 –

+0

あなたの問題を解決する方法としては、 '' std :: getline'](http://en.cppreference.com/w/cpp/string/basic_string/getline)を参照してください。次に、その文字列を '' std :: istringstream'(http://en.cppreference.com/w/cpp/io/basic_istringstream)に入れることができます。ここで、 '* std :: getline'を' '、' 'を各要素を得るための区切り記号として使用します。 –

答えて

0

はあなたかどうかを判断するために、整数を読むループの内側を覗き見、一つの可能​​な解決策であります次の行に移動する必要があり

#include <iostream> 
#include <vector> 
#include <cassert> 

using std::cin; 
using std::cout; 
using std::endl; 

int main() { 
    auto data = std::vector<std::vector<int>>(1); 
    auto integer = int{}; 
    auto current_line = 0; 
    while (cin >> integer) { 
     data[current_line].push_back(integer); 

     // read the next character and if it is a newline add a new entry in 
     // the data vector 
     auto next_character = char{}; 
     if (cin.get(next_character)) { 
      if (next_character == '\n') { 
       data.emplace_back(); 
       ++current_line; 
      } 
     } else { 
      break; 
     } 
    } 

    for (const auto& vec : data) { 
     for (auto integer : vec) { 
      cout << integer << " "; 
     } 
     cout << endl; 
    } 

    return 0; 
} 

Also as noted in the comments、心に留めておくべき一つのことは、あなたがトンを使用しなければならないということです彼は.good().eof()などと呼ぶのではなく、ループの終了と終了をチェックするために自分自身を呼びます。

0

これは、行のそれぞれの数を分割するためにboost :: splitメソッドを使用する別の方法です。

#include <fstream> 
#include <string> 
#include <iostream> 
#include <boost/algorithm/string.hpp> 

void printMatrix(std::vector<std::vector<int> >& matrix) 
{ 
    for(auto line : matrix) 
    { 
     for(auto num : line) 
     { 
      std::cout << num << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

int main() 
{ 
    std::ifstream in; 
    std::string line; 
    in.open("sequences.txt"); 
    std::vector<std::vector<int> > mat; 

    while(std::getline(in, line)) 
    { 
     std::vector<std::string> text; 
     std::vector<int> nums; 
     boost::split(text, line, boost::is_any_of(",")); 
     for(auto num : text) 
      nums.push_back(std::stoi(num)); 

     mat.push_back(nums); 
    } 
    printMatrix(mat); 
} 
関連する問題