I want to learn how to read a text file (contain 3 columns 300000+ rows) and assign each column of the text file to different arrays in c++. I searched this on the internet but i could not find any suitable solution for this. I found a code to do this but the code read only 547 rows. The code is below.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
vector<double> vecX, vecY, vecZ;
double x, y, z;
ifstream inputFile("filetext.txt");
while (inputFile >> x >> y >> z)
{
vecX.push_back(x);
vecY.push_back(y);
vecZ.push_back(z);
}
for(int i = 0; i < vecX.size(); i++) {
cout << vecX[i] << ", " << vecY[i] << ", " << vecZ[i] << endl;
cout << i << endl;
}
}
A sample input text file data is below:
3.862015625000000e+03 5.611499505259664e-01 1.183793839633211e-02
3.862031250000000e+03 5.587474540972663e-01 1.186382272148924e-02
3.862046875000000e+03 7.376678568236076e-01 1.032568525995413e-02
3.862062500000000e+03 8.921759412061890e-01 9.389467084403112e-03
3.862078125000000e+03 8.003829513850249e-01 9.913663338280957e-03
. . .
. . .
. . .
I have one more question. The above code give an output such this: 3862.02, 0.56115, 0.0118379. But i want full digit as in the text file. How can i get.
Thanks in advance.
std::setprecision?x,y,andzvalues. Create a vector of these structures. This is generally more efficient than parallel vectors (the processor's cache can hold at least one of these structures, there is no guarantee that the processor's cache can hold all 3 arrays). The processor may have to reload it's cache in order to get values in the arrays.std::setprecisionbeacuse i am new in c++, but i try to learn. I will consider your suggestions, thank you.