Hi,
I am having trouble outputting to many files
ifstream sim_dump;
sim_dump.open("c_grain.sim_trj");
char dump_file[20];
int itr = 4000;
sprintf( dump_file, "%d", itr);
ofstream fout(dump_file);
while(lammpsdump.good())
{
//if(itr % 100 == 0)
//{
fout.close();
sprintf( dump_file, "%d", itr);
ofstream fout(dump_file);
cout << "TIMESTEP " << itr << endl;
//}
...
}
When I run the code with my if loop commented I get the correct output but with 100 times more files than I would like
file names 4000 4001 4002 4003 4004 etc all with correct output in file
If I include the if loop I get an output of files 4000 4100 4200 4300 as expected but the files are empty
Edit: In both cases cout << "TIMESTEP " << itr << endl; outputs as expected
I have searched for the answer but was not getting anywhere thanks for any help you can offer
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
int timestep, atom, type, No_Atoms;
double x, y, z, xs, ys, zs, vms;
char line1[250], line2[250], line3[250], line4[250], line5[250], line6[250], line7[250], line8[250], line9[250];
int main(){
ifstream sim_dump;
sim_dump.open("c_grain.sim_trj2");
char dump_file[20];
int itr = 4000;
sprintf( dump_file, "%d", itr);
ofstream fout(dump_file);
while(sim_dump.good())
{
if(itr % 100 == 0)
{
fout.close();
sprintf( dump_file, "%d", itr);
ofstream fout(dump_file);
cout << "TIMESTEP " << itr << endl;
}
sim_dump.getline(line1,250);
sim_dump.getline(line2,250);
sim_dump.getline(line3,250);
sim_dump.getline(line4,250);
sim_dump.getline(line5,250);
sim_dump.getline(line6,250);
sim_dump.getline(line7,250);
sim_dump.getline(line8,250);
sim_dump.getline(line9,250);
timestep = atoi(line2);
No_Atoms = atoi(line4);
fout << timestep << " 0 0 0 0" << endl;
for(int i=1;i<=No_Atoms;i++)
{
sim_dump >> atom;
sim_dump >> type;
sim_dump >> x;
sim_dump >> y;
sim_dump >> z;
sim_dump >> xs;
sim_dump >> ys;
sim_dump >> zs;
vms = sqrt(0.5*( (xs - ys)*(xs - ys) + (ys - zs)*(ys - zs) + (zs - xs)*(zs - xs) ));
fout << atom << " " << x << " " << y << " " << z << " " << vms << " " << endl;
}
sim_dump.ignore(256,'\n');
itr++;
}
fout.close();
sim_dump.close();
return 1;
}