io - Treat a space(or tab) separated file "like" and array - C++ -


i have short tab/space separated file (i can create accordingly) structure

[data00] <space> [data01] <space> [data02] <space> [data03] <newline> [data10] <space> [data11] <space> [data12] <space> [data13] <newline> ... 

the first column representing numerical id. create file feed executable, format fixed. after feeding it, executable outputs file similar structure:

[data00] <space> [data01]<newline> [data10] <space> [data11]<newline> ... 

given id, need read corresponding [datax1], perform operations on [datax3] in first file, feed executable, , iterate.

i think of 2 way of doing this:

  • operate on 2 textfile "as if" array, given structure fixed, lost on function/syntax use. should small function allow me read interesting bit passing relevant numeric id hiding pesky i/o code, need repeat operation lot in different context
  • keep first file in arrays , trick executable feeding stream (is possible? executable expects file argument).

i read files arrays , write files anew each time, want avoid useless read , write operation, when need read/write 1 cell each time. don't how how stop/identify interest bit when read whole line text file using,say, getline.

first write function split inputted string based upon given separator. (in case use space.)

int split(const std::string& line, const std::string& seperator, std::vector<std::string> * values){     std::string tstring = "";     unsigned counter = 0;     for(unsigned l = 0; l < line.size(); ++l){         for(unsigned = 0; < seperator.size(); ++i){             if(line[l+i]==seperator[i]){                 if(i==seperator.size()-1){                     values->push_back(tstring);                     tstring = "";                     ++counter;                 }else continue;             }else{                 tstring.push_back(line[l]);                 break;             }         }     }     return counter; } 

now write ourselves simple main read file, use split break up, , output data based upon location within file.

int main(){     std::vector<std::vector<std::string> > lines;     std::string tstring = "";     std::vector<std::string> tvector;     std::ifstream filetoload;      filetoload.open(file_name);      if(filetoload.is_open()){         while(std::getline(filetoload,tstring)){             split(tstring, " ", &tvector);             lines.push_back(tvector);             tvector.clear();         }          //now print our output.         for(unsigned i1 = 0; i1 < lines.size(); ++i1){             for(unsigned i2 = 0; i2 < lines[i1].size(); ++i2){                 std::cout<<"["<<i1<<","<<i2<<"] = "<<lines[i1][i2]<<std::endl;             }         }     }else{         std::cerr<<"failed open file: "<<file_name<<std::endl;         return 1;     }     return 0; } 

the input file used has data:

450 105 10 10.5 -10.56001 23 10 478 1290 384 1289 3489234 1 2 3 4 5 1 2 3 4 5 6.1 19 -1.5 

and output gives:

[0,0] = 450 [0,1] = 105 [0,2] = 10 [0,3] = 10.5 [0,4] = -10.56001 [1,0] = 10 [1,1] = 478 [1,2] = 1290 [1,3] = 384 [1,4] = 1289 [1,5] = 3489234 [1,6] = 1 [1,7] = 2 [1,8] = 3 [1,9] = 4 [2,0] = 1 [2,1] = 2 [2,2] = 3 [2,3] = 4 [2,4] = 5 [2,5] = 6.1 [2,6] = 19 

now need use favorite parsing algorithm change each string double. (strtod, atof, etc) depending how important optimization may want modify container vector, depending upon use cases.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -