Read data from file and sum over specific columns after hitting specific line (string) using C++ -


i decided open new thread if problem solved partially problem 1 right (read data file 2d array , sum on specific arrays using c++). nevertheless, here want read in:


calculation number of points: 200 # atoms: 4

point 1 : 0.00000000 0.00000000 0.00000000 weighting = 0.00500000

energy 1 # weighting 1.00000000

atom b c d 1 0.476 0.000 0.000 0.100 2 0.476 0.000 0.000 0.100 1 0.000 -0.000 -0.000 0.200 2 -0.000 -0.000 0.000 0.200

energy 2 # weighting 1.00000000

atom b c d 1 0.476 0.000 0.000 0.300 2 0.476 0.000 0.000 0.300 1 0.000 -0.000 -0.000 0.400 2 -0.000 -0.000 0.000 0.400

energy 2 # weighting 1.00000000

atom b c d 1 0.476 0.000 0.000 0.500 2 0.476 0.000 0.000 0.500 1 0.000 -0.000 -0.000 0.600 2 -0.000 -0.000 0.000 0.600

....

....

#include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> using namespace std;  int main() {     int rows = 0;     int columns = 0;     string line;     int firstnumber = 0;     vector<vector<double> > values;     vector<vector<double> > results;     vector<double> rowstotal;     ofstream file;     ifstream in("data.txt");     file.open("output.txt",ios::app);     file.setf(ios::fixed);     file.setf(ios::showpoint);     file.precision(3);      if(in.fail())     {         cerr << "file can not opened" << endl;         return -1;     }      file << "\n" << endl;      // save every double     while(in.good())     {          bool begin_tag = false;         while (getline(in,line))         {             if(line.find("energy   2 #") != std::string::npos ) {                 begin_tag = true;                 continue;             }             else if (line == "energy   1 #")             {                 begin_tag = false;              }              istringstream stream(line);             vector<double> tmp;             double x;              while (stream >> x)                 tmp.push_back(x);              if (tmp.size() > 0)                 values.push_back(tmp);          }     }       columns = values[0].size();     (unsigned = 1; < values.size(); ++i)     {         if (values[i].size() != columns)         {             cerr << "row different column number" << endl;             return -1;         }     }      (unsigned = 0; < values.size(); ++i)     {         // if number 1.0 encountered, add row         if (values[i][0] == 1.0)             results.push_back(values[i]);          // if number 2.0 encountered, add row         if (values[i][0] == 2.0)         {             (unsigned j = 0; j < values[i].size(); ++j)                 results.back()[j] += values[i][j];         }     }        rows = results.size();      file << "number of rows # " << rows << endl;     file << "number of columns # " << columns << endl;     file << " " << endl;      for(int i=0; < rows; i++)     {         for(int j=4; j < columns; j++)          {             file << results[i][j]  <<  "     " << "  " << endl;         }     }       for(int i=0; < rows; i++)     {           rowstotal.push_back(0.0);         (int j=1; j < columns; j++)          {             rowstotal[i] += results[i][j];         }     }      file.close();     in.close();     return 0; } 

the output is:

number of rows # 6 number of columns # 5

0.200
0.400
0.600
0.800
1.000
1.200

as stated above, achieve sum on blocks "energy 2 #" , ignore block beginning "energy 1#". code should give values:

0.600
0.800
1.000
1.200

i tried implement boolean done somehow missing something. thankful if able give me hint or tell me how solve it.

thanks , fruitful hints!

best wishes, daves

i found solution problem. realized setting "begin_tag = false" never asking it. again read post , thought it!


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -