I have a lot of data in separate files generated from a simulation program. All data is clear and formatted via a single space to read in 4 columns. I can open and store data from each file one at a time, but that will get so impossible once I hit more than, say, 10 files of data with the same simulation data columns.
I am just trying to find a better way to open files and store the data using an iterative method instead of typing, for example, "open file, read file data, append to file" over 1000 times.
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
int main ()
{
// INITIALIZE
int tmax = 80000;
float time_step[tmax];
float LEx[tmax];
float LEy[tmax];
float LEz[tmax];
float LE[tmax];
float AvgLEx;
float AvgLEy;
float AvgLEz;
float AvgLE;
float Function[tmax];
// FILES
ifstream infile;
infile.open("File_1.prn");
// THIS INFILE is where I want to put a loop to change File_1 to File_2 to File_3 and so on and so on to append data from all those files into ONE single file
if(infile.fail())
{
cout << "error reading file" << endl;
return 1;
}
for (int i = 0; i < tmax+1; ++i)
{
infile >> time_step[i] >> LEx[i] >> LEy[i] >> LEz[i] >> LE[i];
AvgLEx = AvgLEx + LEx[i];
AvgLEy = AvgLEy + LEy[i];
AvgLEz = AvgLEz + LEz[i];
AvgLE = AvgLE + LE[i];
}
AvgLEx = AvgLEx/tmax;
AvgLEy = AvgLEy/tmax;
AvgLEz = AvgLEz/tmax;
AvgLE = AvgLE/tmax;
cout << "AvgLE: " << AvgLE << "\n";
cout << time_step[tmax] << " LEx " << LEx[tmax] << " LEy " << LEy[tmax] << " LEz: " << LEz[tmax] << " LE: " << LE[tmax] << endl;
cout << "\n";
infile.close();
cout << "\nFile read! \n";
// FUNCTIONS
for (int i = 0; i < tmax+1; ++i)
{
Function[i] = LE[i] - AvgLE;
cout << LE[i] << " " << AvgLE << " " << Function[i] << "\n";
}
cout << "\n Function set up \n";
// // CONVOLUTION
float A_t;
float A_dt;
int dt, t;
float max=0;
for (int dt = 0; dt < 1 + tmax/10; ++dt)
{
//dt = 1;
cout << "dt: " << dt << "\n";
//A_dt = 0;
int window = tmax-dt;
for (int t = 0; t < window; ++t)
{
if (t==0)
{
A_t = 0;
cout << "A_t was set to zero \n";
}
A_t+= Function[t] * Function[t + dt];
cout << "A_t is accumulation ...: " << A_t << "\n" ;
}
A_dt = A_t / window;
if (A_dt > max)
{
max = A_dt;
}
// OUTPUT
ofstream AutoCor;
AutoCor.open ("AutoCor_25.prn", ios::app);
AutoCor << dt << " " << A_dt << "\n";
AutoCor.close();
cout << "A_dt: " << A_dt << " " << "window used: " << window << "\n";
}
cout << "max used: " << max << "\n";
// RETURN
return A_dt;
return 0;
}
cat
to do this. \$\endgroup\$cat file* > all.file
orcat file{1..3} > file4
\$\endgroup\$