C++ Editing Text File -


the problem data in text file editing. text file contains 5 columns.

1 | 2 | 3 | 4 | 5 |  1 2 4 4 1 2 3 4 4 3 3 4 5 0 0 

the goal move in columns 4 , 5 (values​​> 0) in columns 1 , 2 above or follow up:

1 | 2 | 3 | 4 | 5 |  1 2 4 0 0 2 3 4 0 0 3 4 5 0 0 4 1 0 0 0 4 3 0 0 0 

how achieve this? can show me example how c++ std::vector?

that appreciated.

i agree joachim. additionally, use back_inserter, istream_iterator , stringstream make life easier when reading in file:

vector<vector<double> > contents;  /* read file */ {     ifstream infile( "data.txt" );     ( string line; infile; getline( infile, line ) ) {         stringstream line_stream( line );         vector<double> row;         copy( istream_iterator<double>( line_stream ), istream_iterator<double>(),             back_inserter(row) );         contents.push_back( row );     } } 

that read in whole file contents. you'll need include sstream, algorithm, iterator, iosrteam, fstream, string , vector.

now can process file for loop , accessing numbers contents[i][j]. if understand correctly think want do:

/* process file */ unsigned int n = contents.size(); ( unsigned int i=0; < n; ++i ) {     vector<double> row( 5, 0. );     bool add_row = false;     if ( contents[i].size() >= 5 ) {         ( unsigned int j=3; j<4; ++j ) {             double value = contents[i][j];             contents[i][j] = 0.;             if ( value > 0 ) {                 add_row = true;                 row[j-3] = value;             }         }         if ( add_row == true ) {             contents.push_back( row );         }     } } 

now write file stdout, simply:

/* write file */ ( unsigned int i=0; < contents.size(); ++i ) {     copy( contents[i].begin(), contents[i].end(), ostream_iterator<double>( cout, " " ) );     cout << endl; } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -