2012-04-04 12 views
-1

誰もがC++で、次の問題解決私を助けることができる場合、私は思ったんだけど:ファイル処理

を私はいくつかの時間は、データが不足しているファイルを持つ2つの連続したタブがあります。すなわち、私は変換する必要があります第2のTABを例えば「-999999」または「0」にする。ここ ファイルが

 i_1 i_2 i_3 i_4 i_5 
j_1 12   14   16 
j_2  11 17 25 
j_3 44    51 65 

どのように見えるかである私は、最初の行として、すなわち(12,14および16)の要素の平均を計算したい:

sum+=tab[i][j]; 
mean = sum/5; (considering empty spaces =0) 

+0

i_1、i_2、j_1などはファイルの一部ですか? – jrok

+0

値間のスペースまたはタブ?値は常に2桁ですか? – k06a

+0

カラムの数が固定されている場合は、何も変換する必要はありません...そこにあるものをまとめてください。 – jrok

答えて

1
ありがとう
#include <boost/algorithm/string/split.hpp> 
#include <iostream> 
#include <fstream> 
#include <vector> 
#include <list> 

bool const compress_tokens = false; 
bool const table_width = ...; 

std::ifstream inp("filename"); 

// parsed grid 
std::list<std::vector<std::string> > table; 

std::string strbuf; 
std::vector<std::string> vecbuf; 
while(inp.getline(strbuf)) 
{ 
    vecbuf.clear(); 
    boost::split(vecbuf, strbuf, boost::is_any_of("\t"), compress_tokens); 
    assert(vecbuf.size() == table_width); 
    table.push_back(vecbuf); 
} 
+0

あなたのコードの構築を説明できますか? – NadCo

+0

ファイルを開き、行ごとに読み込んで各行を分割し、トークンをベクトルに入れ、2D表を表すベクトルのリストにベクトルを追加する – bobah