2016-07-24 10 views
0

ファイルから読み込まれた文字列をカンマで区切ってC++でトークン化しようとしましたが、すべての行の最初の3つのデータが必要です。C++文字列の一部をトークン化

例えば

: 行は次のようになります。
140,152,2240,1,0,3:0:0:0:
156,72,2691,1,0,1:0:0: 0:
356,72,3593,1,0,1:0:0:0:

しかし、私はこれらの行の最初の3つのデータが必要です。この場合:
140、152、2240
156、72、2691
356、72、3593

私はちょうどからのラインを読み飛ばしする方法がわからないベクターにこれらのデータを追加しようとしています最初の3つのデータの後のファイル。

これが私の現在のコードです:(canPrintはデフォルトでtrueである)最初のベクトルの大きさを確認する程度

ifstream ifs; 
     ifs.open("E:\\sample.txt"); 
     if (!ifs) 
      cout << "Error reading file\n"; 
     else 
      cout << "File loaded\n"; 



     int numlines = 0; 
     int counter = 0; 
     string tmp; 

     while (getline(ifs, tmp)) 
     { 
      //getline(ifs, tmp); // Saves the line in tmp. 
      if (canPrint) 
      { 
       //getline(ifs, tmp); 
       numlines++; 

       // cout << tmp << endl; // Prints our tmp. 
       vector<string> strings; 
       vector<customdata> datalist; 
       istringstream f(tmp); 
       string s; 
       while (getline(f, s, ',')) { 
        cout << s << " "; 
        strings.push_back(s); 
       } 
       cout << "\n"; 


      } 

答えて

1

どのように?おそらく何か

while (strings.size() < 3 && getline(f, s, ',')) { ... } 
関連する問題