Error when I tried to read from a Binary file

Dear all,
I am trying to read a binary file input.ndf which is supplied externally. The problem statement is as follows:

There are neutron rays passing through the sample and gets detected by the detector. I am creating a grid from the input.ndf file. In the grid, in case there is a part of the sample, it is filled by 1 or 0. I am dynamically allocating the 3-d grid and 2-d detector. I am having problems with reading the file. I have attached the code below. Kindly help me. I am quite new to programming. So, kindly give your suggestions.

[code]
#include<iostream>
#include<fstream>
using namespace std;

typedef struct read
{

int xn,yn,zn,detyn,detzn;
double xd,yd,zd,det_yd,det_zd,qq;
//int i,j;
//double temp;
}r1;



int read1(int xn,int yn,int zn,int detyn,int detzn,double xd,double yd,double det_yd,double det_zd,double qq)
{
fstream reader("input.ndf",std::ios::binary|std::ios::in);

if(!reader)
{
return 0;
}

r1 read;
reader.read((char *)&read,sizeof(r1));
int i,j;
double temp;

cin>>temp;
/*read.xn=int(temp);
read.yn=int(temp);
read.zn=int(temp);
read.detyn=int(temp);
read.detzn=int(temp);
*/

cout<<read.xn<<endl;
cout<<read.yn<<endl;
cout<<read.zn<<endl;
cout<<read.detyn<<endl;
cout<<read.detzn<<endl;
cout<<read.xd<<endl;
cout<<read.yd<<endl;
cout<<read.zd<<endl;
cout<<read.det_yd<<endl;
cout<<read.det_zd<<endl;
cout<<read.qq;
}


int read2(int xn,int yn,int zn,double detyn,double detzn,int grid,double detector)
{
for(int i=1;i<=xn;i++)
{
for(int j=1;j<=yn;j++)
{
for(int k=1;k<=zn;k++)
{
grid=11+(i-1)*yn*zn + (j-1)*zn +k;;
}
}
}
}


//--------------------
//-------MAIN---------
//--------------------
int main()

{
int xn, yn,zn;
double xd,yd,det_yd,det_zd,Q;

//----Allocate grid here----
int icount=0;

int ***grid= new int**[zn];

for(int i=0;i<zn;i++)
{
grid[i]=new int*[yn];
for(int j=0;j<yn;j++)
{
grid[i][j]=new int[xn];
for(int k=0;k<xn;k++,icount++)
{
grid[i][j][k]=icount;
}
}
}

//Allocate detector
double detzn,detyn,count=0;
double **detector=new double*[detzn];
for(int i=0;i<detzn;i++)
{
detector[i]=new double[detyn];
for(int j=0;j<detyn;j++,icount++)
{
detector[i][j]=count;
}
}


cout<<" the contents are "<<read1(xn,yn,zn,detyn,detzn,xd,yd,det_yd,det_zd,Q)<<endl;
cout<<"the contents are "<<read2(xn,yn,zn,detyn,detzn,grid,detector)<<endl;
return 0;

}
Topic archived. No new replies allowed.