I have a data file coordMe.dat with different time snapshots of data like below:
Snapshot 0
Hello World 1
Hello World 2
ID x y z
1 0.23 0.369 0.1435
2 0.364 786 0.0032
.
.
.
2197 0.39 0.957 0.136
Snapshot 1
Hello World 1
Hello World 2
ID x y z
1 -0.741 0.963 0.0101
2 0.354 0.002 0.879
.
.
.
2197 0.1239 0.92357 0.01036
Snapshot 2
Hello World 1
Hello World 2
ID x y z
1 -0.001 0.9103 0.5401
2 0.00354 0.23302 0.8475
.
.
.
2197 0.7895 0.2537 0.00016
I would like to only read the real numbers after ID x y z in every snapshot into arrays. I wrote a code to loop over all snapshots and read the data in every snapshot.Then I tested it by outputting what has been read. The output data are different from the input data. The output data are columns of zeros.
How to make the loop access all the real numbers after ID x y z in all the snapshots such that these real number are read into arrays.
Here is my code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std ;
int main(){
int N=2197;
double position_x[N]{}, position_y[N]{}, position_z[N]{} ;
int ID[N]{}; string rubbish;
ifstream inputfile ("coordMe.dat");
ofstream outputfile("outputfile.dat");
if (inputfile.is_open() && outputfile.is_open()){
cout<<"The files are open \n" ;}
for(int snapshot=0 ; snapshot <= 2 ; ++snapshot){
fill_n(position_x,N,0); fill_n(position_y,N,0);fill_n(position_z,N,0); fill_n(ID,N,0);
getline(inputfile,rubbish);
getline(inputfile,rubbish);
getline(inputfile,rubbish);
getline(inputfile,rubbish);
for(int iparticle=1 ; iparticle <= N ; ++iparticle){
inputfile>>ID[iparticle]>>position_x[iparticle]>>position_y[iparticle]>>position_z[iparticle];
outputfile<<ID[iparticle]<<"\t"<<position_x[iparticle]<<"\t<<position_y[iparticle]<<"\t"
<<position_z[iparticle]<<"\n"; }
outputfile<<"*****************************\n*****************************\n";
}
return 0;
}