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;
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.
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.
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;
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;
}
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, unsignedint num)
{
myfile.seekg(std::ios::beg);
for (unsignedint 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.