Program closing error

I am trying to prepare a simple converter that reads data from a file and then rearranges the data followed by its writing work in a different file.

I have written the code in CODE::BLOCKS. It is compiled without any error. However, while running it fails and gives message that the program has stopped working.

The code written is as below:
/*dump2VTKconverter*/
#include <iostream>
#include <cmath>
#include "stdio.h"

/*************************For reading data from files*************************************/
#include "assert.h"
#include "stdio.h"
#include <fstream>
#include <limits>
/*****************************************************************************************/

using namespace std;

/* ---------------------Function to jump to a specific line of file------------------------- */
std::ifstream& GotoLine(std::ifstream& myfile, unsigned int num)
{
myfile.seekg(std::ios::beg);
for(int i=0; i < num - 1; ++i)
{
myfile.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
return myfile;
}

/* --------------------------Function to write an array to a file---------------------------- */
std::fstream& writefile(std::fstream& file, int nparticles, int ntimestep, double *array, int array_length, int array_type)
{
if(file.is_open())
{
file<<"\n";
if(array_type == 1)
{
file<<ntimestep;
file<<"\t \t";
file<<nparticles;
file<<"\t \t";
}
for(int i = 0; i < array_length; i++)
{
file<<array[i];
file<<"\t";
}
}
}
/*---------------------------------------------------------------------------------------------*/

int main()
{
int NoOfPropertiesInDumpFiles=0; int TimeStepLineNo=0; int NoOfAtomsLineNo=0; int FirstDataLineNo=0;

cout << "Enter NoOfPropertiesInDumpFiles "; cin >> NoOfPropertiesInDumpFiles;
cout << "Enter TimeStepLineNo "; cin >> TimeStepLineNo;
cout << "Enter NoOfAtomsLineNo "; cin >> NoOfAtomsLineNo;
cout << "Enter FirstDataLineNo "; cin >> FirstDataLineNo;

int TimeStep=0; int NoOfAtoms=0; int LineNo=0;

int *ATOM_ID; int *ATOM_TYPE01; int *ATOM_TYPE02; double *X; double *Y; double *Z; double *IX; double *IY; double *IZ;
double *VX; double *VY; double *VZ; double *FX; double *FY; double *FZ; double *OMEGAX; double *OMEGAY; double *OMEGAZ;
double *Radius;

for(int i=1;10;i++)
{
ifstream dumpFile("dumpfile");
//dumpFile.open("dumpfile", std::ios::in::out);

if(ATOM_ID) delete[] ATOM_ID; if(ATOM_TYPE01) delete[] ATOM_TYPE01; if(ATOM_TYPE02) delete[] ATOM_TYPE02; if(X) delete[] X; if(Y) delete[] Y; if(Z) delete[] Z;
if(IX) delete[] IX; if(IY) delete[] IY; if(IZ) delete[] IZ; if(VX) delete[] VX; if(VY) delete[] VY; if(VZ) delete[] VZ;
if(FX) delete[] FX; if(FY) delete[] FY; if(FZ) delete[] FZ; if(OMEGAX) delete[] OMEGAX; if(OMEGAY) delete[] OMEGAY; if(OMEGAZ) delete[] OMEGAZ; if(Radius) delete[] Radius;

ATOM_ID=new int[NoOfAtoms]; ATOM_TYPE01=new int[NoOfAtoms]; ATOM_TYPE02=new int[NoOfAtoms]; X=new double[NoOfAtoms]; Y=new double[NoOfAtoms]; Z=new double[NoOfAtoms];
IX=new double[NoOfAtoms]; IY=new double[NoOfAtoms]; IZ=new double[NoOfAtoms]; VX=new double[NoOfAtoms]; VY=new double[NoOfAtoms]; VZ=new double[NoOfAtoms];
FX=new double[NoOfAtoms]; FY=new double[NoOfAtoms]; FZ=new double[NoOfAtoms]; OMEGAX=new double[NoOfAtoms]; OMEGAY=new double[NoOfAtoms]; OMEGAZ=new double[NoOfAtoms]; Radius=new double[NoOfAtoms];

GotoLine(dumpFile,TimeStepLineNo);
dumpFile>>TimeStep;
GotoLine(dumpFile,NoOfAtomsLineNo);
dumpFile>>NoOfAtoms;

LineNo = FirstDataLineNo; int Current_ATOM_No=0;
while(LineNo < (FirstDataLineNo+NoOfAtoms))
{
GotoLine(dumpFile,LineNo);
dumpFile>>ATOM_ID[Current_ATOM_No]; dumpFile>>ATOM_TYPE01[Current_ATOM_No]; dumpFile>>ATOM_TYPE02[Current_ATOM_No]; dumpFile>>X[Current_ATOM_No]; dumpFile>>Y[Current_ATOM_No]; dumpFile>>Z[Current_ATOM_No];
dumpFile>>IX[Current_ATOM_No]; dumpFile>>IY[Current_ATOM_No]; dumpFile>>IZ[Current_ATOM_No]; dumpFile>>VX[Current_ATOM_No]; dumpFile>>VY[Current_ATOM_No]; dumpFile>>VZ[Current_ATOM_No];
dumpFile>>FX[Current_ATOM_No]; dumpFile>>FY[Current_ATOM_No]; dumpFile>>FZ[Current_ATOM_No]; dumpFile>>OMEGAX[Current_ATOM_No]; dumpFile>>OMEGAY[Current_ATOM_No]; dumpFile>>OMEGAZ[Current_ATOM_No]; dumpFile>>Radius[Current_ATOM_No];
LineNo++; Current_ATOM_No++;
}
// dumpFile.close();
ofstream vtkFile("vtkfile");
vtkFile << "# vtk DataFile Version 2.0" << endl;
vtkFile << "Generated by Rahul K Soni" << endl;
vtkFile << "ASCII" << endl;
vtkFile << "DATASET POLYDATA" << endl;
vtkFile << "POINTS " << NoOfAtoms << " float" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << X[i] << " " << Y[i] << " " << Z[i] << endl;
}
vtkFile << "VERTICES " << NoOfAtoms << " " << NoOfAtoms*2.0 << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << 1 << " " << i << endl;
}
vtkFile << "POINT_DATA " << NoOfAtoms << endl;
vtkFile << "VECTORS i float" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << IX[i] << " " << IY[i] << " " << IZ[i] << endl;
}
vtkFile << "VECTORS f float" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << FX[i] << " " << FY[i] << " " << FZ[i] << endl;
}
vtkFile << "VECTORS v float" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << VX[i] << " " << VY[i] << " " << VZ[i] << endl;
}
vtkFile << "SCALARS omegax float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << OMEGAX[i] << endl;
}
vtkFile << "SCALARS omegay float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << OMEGAY[i] << endl;
}
vtkFile << "SCALARS omegaz float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << OMEGAZ[i] << endl;
}
vtkFile << "SCALARS radius float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << Radius[i] << endl;
}
vtkFile << "SCALARS type float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << ATOM_TYPE01[i] << endl;
}
vtkFile << "SCALARS id float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for(int i=0; NoOfAtoms; i++)
{
vtkFile << ATOM_ID[i] << endl;
}
}

return 0;
}

Normally I would just glance right over a post like this. But I HAVE to know; what in gods name is your main function doing? It's not just the lack of formatting either, I accounted for that and I still don't understand what you are even trying to do. You are first declaring pointers then ... I don't know what ... you appear to be trying to delete something but the pointers aren't initialized so at best you don't even know what you're deleting. Then you're assigning them to zero length arrays because you're declaring and even initializing 'NoOfAtoms' but you're not telling anything to replace it with a number that would result in an array actually being allocated.

Step 1: This is C++, not bash or batch. All of this ATOM stuff needs to be wrapped into a class\struct container. We can move from there after you've done that.

EDIT: Even your for loop. The second parameter in a for loop is the break condition to test against for at the start of each iteration. Simply putting a static '10' means that it will never evaluate to false.
Last edited on
Use code tags.

Aceix.
I am sorry for posting things in that manner. I am not an expert in C++. Actually, there is file named dumpfile.dat with some written data. The program has to read the data store them in different variables such as ATOM_ID, ATOM_TYPE01 etc. Then, the data in those variables has to be printed in a file called vtkfile but in a different fashion. For example, in dumpfile all properties (coordinates, velocities, forces, angular velocities, diameters) of one atom is written together, followed by for second atom and so on. However, in vtkfile the format will be coordinates of all atoms then velocities of all atoms then forces then diameters and so on.

Based on previous comments, I have made some modifications in the cpp file. After modifications it runs successfully and writes the vtk file properly. However, at the end it stops working. In MS visual studio it says that "Dump2VTKConverter.exe has triggered a breakpoint." and asks to either break or continue. I am unable to get why it is happening.

The text of modified cpp file, dumpfile and vtkfile are given below. Sorry for posting such a big message. Thanks in advance.

main.cpp

/*Dump2VTKConverter*/
#include <fstream>
#include <iostream>
#include <cmath>
#include "stdio.h"
#include "assert.h"
#include "stdio.h"
#include <limits>

using namespace std;
std::ifstream& GotoLine(std::ifstream& myfile, unsigned int num)
{
myfile.seekg(std::ios::beg);
for (unsigned int i = 0; i < num - 1; ++i)
{
myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return myfile;
}

int main()
{
int NoOfPropertiesInDumpFiles = 0, TimeStepLineNo = 0, NoOfAtomsLineNo = 0, FirstDataLineNo = 0;

cout << "Enter NoOfPropertiesInDumpFiles = "; cin >> NoOfPropertiesInDumpFiles;
cout << "Enter TimeStepLineNo = "; cin >> TimeStepLineNo;
cout << "Enter NoOfAtomsLineNo = "; cin >> NoOfAtomsLineNo;
cout << "Enter FirstDataLineNo = "; cin >> FirstDataLineNo;

int TimeStep = 0; int NoOfAtoms = 0; int LineNo = 0;

int *ATOM_ID, *ATOM_TYPE01, *ATOM_TYPE02; double *X, *Y, *Z, *IX, *IY, *IZ, *VX, *VY, *VZ, *FX, *FY, *FZ, *OMEGAX, *OMEGAY, *OMEGAZ, *Radius;

ifstream dumpFile("dumpfile.dat");

ATOM_ID = new int[NoOfAtoms]; ATOM_TYPE01 = new int[NoOfAtoms]; ATOM_TYPE02 = new int[NoOfAtoms];
X = new double[NoOfAtoms]; Y = new double[NoOfAtoms]; Z = new double[NoOfAtoms];
IX = new double[NoOfAtoms]; IY = new double[NoOfAtoms]; IZ = new double[NoOfAtoms];
VX = new double[NoOfAtoms]; VY = new double[NoOfAtoms]; VZ = new double[NoOfAtoms];
FX = new double[NoOfAtoms]; FY = new double[NoOfAtoms]; FZ = new double[NoOfAtoms];
OMEGAX = new double[NoOfAtoms]; OMEGAY = new double[NoOfAtoms]; OMEGAZ = new double[NoOfAtoms];
Radius = new double[NoOfAtoms];

if (!dumpFile)
{
cout << "Couldn't open dumpFile" << endl;
}

GotoLine(dumpFile, TimeStepLineNo);
dumpFile >> TimeStep;
GotoLine(dumpFile, NoOfAtomsLineNo);
dumpFile >> NoOfAtoms;

cout << "TimeStep = " << TimeStep << endl;
cout << "NoOfAtoms = " << NoOfAtoms << endl;

LineNo = FirstDataLineNo; int Current_ATOM_No = 0;

while (LineNo < (FirstDataLineNo + NoOfAtoms))
{
GotoLine(dumpFile, LineNo);
dumpFile >> ATOM_ID[Current_ATOM_No]; dumpFile >> ATOM_TYPE01[Current_ATOM_No];
dumpFile >> ATOM_TYPE02[Current_ATOM_No]; dumpFile >>
X[Current_ATOM_No]; dumpFile >> Y[Current_ATOM_No]; dumpFile >> Z[Current_ATOM_No];
dumpFile >> IX[Current_ATOM_No]; dumpFile >> IY[Current_ATOM_No]; dumpFile >> IZ[Current_ATOM_No];
dumpFile >> VX[Current_ATOM_No]; dumpFile >> VY[Current_ATOM_No]; dumpFile >> VZ[Current_ATOM_No];
dumpFile >> FX[Current_ATOM_No]; dumpFile >> FY[Current_ATOM_No]; dumpFile >> FZ[Current_ATOM_No];
dumpFile >> OMEGAX[Current_ATOM_No]; dumpFile >> OMEGAY[Current_ATOM_No]; dumpFile >> OMEGAZ[Current_ATOM_No];
dumpFile >> Radius[Current_ATOM_No];
LineNo++; Current_ATOM_No++;
}
dumpFile.close();

ofstream vtkFile("vtkfile.vtk");
if (!vtkFile)
{
cout << "Can't open vtkFile" << endl;
}

vtkFile << "# vtk DataFile Version 2.0" << endl;
vtkFile << "Generated by Rahul K Soni" << endl;
vtkFile << "ASCII" << endl;
vtkFile << "DATASET POLYDATA" << endl;
vtkFile << "POINTS " << NoOfAtoms << " float" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << X[i] << " " << Y[i] << " " << Z[i] << endl;
}

vtkFile << "VERTICES " << NoOfAtoms << " " << NoOfAtoms*2.0 << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << 1 << " " << i << endl;
}

vtkFile << "POINT_DATA " << NoOfAtoms << endl;
vtkFile << "VECTORS i float" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << IX[i] << " " << IY[i] << " " << IZ[i] << endl;
}

vtkFile << "VECTORS f float" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << FX[i] << " " << FY[i] << " " << FZ[i] << endl;
}

vtkFile << "VECTORS v float" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << VX[i] << " " << VY[i] << " " << VZ[i] << endl;
}

vtkFile << "SCALARS omegax float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << OMEGAX[i] << endl;
}

vtkFile << "SCALARS omegay float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << OMEGAY[i] << endl;
}

vtkFile << "SCALARS omegaz float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << OMEGAZ[i] << endl;
}

vtkFile << "SCALARS radius float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << Radius[i] << endl;
}

vtkFile << "SCALARS type float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << ATOM_TYPE01[i] << endl;
}

vtkFile << "SCALARS id float 1" << endl;
vtkFile << "LOOKUP_TABLE default" << endl;
for (int i = 0; i < NoOfAtoms; i++)
{
vtkFile << ATOM_ID[i] << endl;
}

vtkFile.close();

return 0;
}





dumpfile

ITEM: TIMESTEP
5500000
ITEM: NUMBER OF ATOMS
4
ITEM: BOX BOUNDS ff ff ff
-0.08 0.08
-0.15 0.15
-0.15 0.15
ITEM: ATOMS id type type x y z ix iy iz vx vy vz fx fy fz omegax omegay omegaz radius
30012 1 1 -0.0582374 -0.0195104 -0.135083 0 0 0 4.68321e-06 0.0606032 -0.00877069 5.52272e-05 -6.18736e-06 -5.33603e-05 0 0.000484166 -0.0140728 0.0025
32079 1 1 -0.0372178 -0.0188816 -0.135032 0 0 0 -9.23203e-06 0.0606018 -0.00848398 0.00011131 4.30535e-05 0.000108306 0 -0.00877039 -0.00135728 0.0025
28627 1 1 0.014887 -0.0188805 -0.135053 0 0 0 -1.06437e-05 0.0606273 -0.00848534 8.64585e-05 -9.10874e-06 -5.12592e-05 0 -0.00586016 -0.0183048 0.0025
30579 1 1 -0.0675001 -0.0179654 -0.13518 0 0 0 -2.27102e-05 0.0606637 -0.00809197 7.46358e-05 7.53267e-06 3.05454e-05 0 0.0113528 -0.00226138 0.0025




vtkfile

# vtk DataFile Version 2.0
Generated by Rahul K Soni
ASCII
DATASET POLYDATA
POINTS 4 float
-0.0582374 -0.0195104 -0.135083
-0.0372178 -0.0188816 -0.135032
0.014887 -0.0188805 -0.135053
-0.0675001 -0.0179654 -0.13518
VERTICES 4 8
1 0
1 1
1 2
1 3
POINT_DATA 4
VECTORS i float
0 0 0
0 0 0
0 0 0
0 0 0
VECTORS f float
5.52272e-005 -6.18736e-006 -5.33603e-005
0.00011131 4.30535e-005 0.000108306
8.64585e-005 -9.10874e-006 -5.12592e-005
7.46358e-005 7.53267e-006 3.05454e-005
VECTORS v float
4.68321e-006 0.0606032 -0.00877069
-9.23203e-006 0.0606018 -0.00848398
-1.06437e-005 0.0606273 -0.00848534
-2.27102e-005 0.0606637 -0.00809197
SCALARS omegax float 1
LOOKUP_TABLE default
0
0
0
0
SCALARS omegay float 1
LOOKUP_TABLE default
0.000484166
-0.00877039
-0.00586016
0.0113528
SCALARS omegaz float 1
LOOKUP_TABLE default
-0.0140728
-0.00135728
-0.0183048
-0.00226138
SCALARS radius float 1
LOOKUP_TABLE default
0.0025
0.0025
0.0025
0.0025
SCALARS type float 1
LOOKUP_TABLE default
1
1
1
1
SCALARS id float 1
LOOKUP_TABLE default
30012
32079
28627
30579
Last edited on
One more thing. The above code is run to test for conversion of one file from one type format to another. However, it has to be done recursively. There are multiple files such as dump1000.dat, dump2000.dat, dump3000.dat etc. I want to further modify the code with the file reading in the manner dump*.dat and then write the vtk files such as vtkfile*.vtk.
Please suggest appropriate modification for this.
Have you considered using a structure to hold this data? That should make things much easier. I'd also suggest more functions.

It also looks like your GotoLine() function needs work.

1
2
3
4
5
6
7
8
9
std::ifstream& GotoLine(std::ifstream& myfile, unsigned int num)
{
   myfile.seekg(std::ios::beg);
   for (unsigned int i = 0; i < num - 1; ++i)
   {
      myfile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   }
   return myfile;
}


First std::ios::beg is an implementation defined value used to determine the seek direction, so using this value as the distance parameter is incorrect. You need to look at some documentation for this function so you use it correctly.

Also the use of the unsigned int for the "distance" to seek is also incorrect. The seekg() function expects a parameter type of streampos. This implementation defined type is not necessarily an unsigned int, and in some cases a streampos can be larger than what can safely be held in a unsigned int.

Lastly Please use code tags when posting code.
Topic archived. No new replies allowed.