Hi there, I'm currently writing a program that calculates the energy of atoms inside a simulation space (I'll spare the other details). Basically I want to create an output file (.txt) that shows the value of 'r' and 'total_energy' for each iteration of the loop. The relevant part of the code is:
And then assigning a value to the output file inside the loop, but all I got out was the final values of both 'r' and 'total_energy', not the values after each iteration of the loop. Any suggestions of a better way to do this?
ofstream outputFile;
outputFile.open("LJ_plot.txt", ios::app); //ios::app opens the file for appending
// data and not truncating or deleting previous data
int entry = 0; //to keep track of # of total iterations
for (int i = 0; i < n_atoms-1; i++)
{
entry += 1;
for (int j = i+1; j < n_atoms; j++)
{
entry += 1;
//...your code and then...
total_energy = total_energy + e_lj;
outputFile << "This is the " << entry << "th entry that is output to file" << endl;
}
}
outputFile.close();
Just to add to this, if I wanted to create two output files at the same time, say instead of "LJ_plot.txt", I had instead "LJ_radius" and "LJ_total_energy", how would I go about doing that?
1 - You can open and close file 1 and then open and close file 2 for each iteration. Sound good ? Haha I hope not !
2 - Since each file would have data output at essentially the same time it'll be much better to create 2 ofstream objects dedicated to handling each file separately. Declare the 2nd one with the same format as the first. It'll just be a matter of copy pasting mostly with just a change in name of the object and the file to open.