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;

}

Edited 4 Years Ago by nahmartin: n/a

You can't open and close a file between each write and expect to keep the data being overwritten.
Move the write outside the IF.

Edited 4 Years Ago by WaltP: n/a

You can't open and close a file between each write and expect to keep the data being overwritten.
Move the write outside the IF.

The file should not be opening and closing between each write

Every 100 iterations it should close the current file, Generate the name for the new file then open a new file

between each 100 iterations the file should stay open not entering the IF statement

if(itr % 100 == 0)
{
fout.close();
sprintf( dump_file, "%d", itr);
ofstream fout(dump_file);
cout << "TIMESTEP " << itr << endl;
}

Sorry, misread...

Have you tried testing the return statii of the I/O to see if there were errors? That's usually a good first step. You're just blindly assuming everything is working correctly -- and clearly it's not.

You also might want to shorten your test. Make a new data file with 88 records and change the IF to 10. When you get that working, go back to the full file.

if(itr % 100 == 0)
    {
      fout.close();

      sprintf( dump_file, "%d", itr);
      fout.open(dump_file);
      cout << "TIMESTEP " << itr << endl;
    }

Found the problem changed from line 33 (post 1) to line 6 here

Still a little unsure why it works for every step (without the IF statement) and not for every 100 steps.

The file was not open before the change I made, fout.is_open = false for all iterations

Edited 4 Years Ago by nahmartin: n/a

This question has already been answered. Start a new discussion instead.