C++ Converting a text file list with numbers and words into an integer 2D array -


i have basic text file has 1 entry per line, entries are numerical, there few lines word , (evenly spaced) in them. here example of 1 such spacing between , :

<event> 4 0 0.1005960e+03 0.2722592e+03 0.7546771e-02 0.1099994e+00 21 -1 0 0 501 502 0.00000000000e+00 0.00000000000e+00 0.17700026409e+03 0.17700026409e+03 0.00000000000e+00 0. -1. 21 -1 0 0 502 503 0.00000000000e+00 0.00000000000e+00 -0.45779372796e+03 0.45779372796e+03 0.00000000000e+00 0. 1. 6 1 1 2 501 0 -0.13244216743e+03 -0.16326397666e+03 -0.47746002227e+02 0.27641406353e+03 0.17300000000e+03 0. -1. -6 1 1 2 0 503 0.13244216743e+03 0.16326397666e+03 -0.23304746164e+03 0.35837992852e+03 0.17300000000e+03 0. 1. </event> 

what need create numerical matrix (using numerical values) each column holds data values between each separate instance of , .

this have far:

using namespace std;  int main() {     vector <string> data;      string str;    ifstream fin("final_small.txt");    while (fin >> str)    {                       data.push_back(str);   }   fin.close(); // close file.  int n = data.size();    int matrix[13][19];    (int = 0; < 13; i++) {       (int j = 0; j < 19; j++) {     matrix[i][j] = data[i];  }   } } 

obviously huge work in progress. first of all, read text file in vector, can not of type int because of words. causes problems later on when try input matrix.

does have suggestions?

thanks in advance!

in c++11, use std::stoi convert string int. note std::stoi throw exception of type std::invalid_argument if conversion cannot performed.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -