hasanasghar00
Programmer
hello
i wrote pgm for reading hex file. the program is working fine btu the problem is that after giving output tc terminates givign illegal operation message.
pls help
thanks
THE CODE:
int main(void)
{
FILE *fptr;
char fname[10],temp[10];
int n=0,i=0,j=0,k=0,*bof;
char ch,(*addr)[4];
char (*records)[44];
clrscr();
cout<<"Enter file name: ";
cin>>fname;
if( (fptr=fopen(fname,"r"
) == NULL )
{
cout<<"File not opened";
getch();
exit(0);
}
while( (ch=getc(fptr)) != EOF )
if(ch=='\n')
n=n+1;
rewind(fptr);
records=(char(*)[44])malloc(n*sizeof(*records));
if(records==NULL)
{
cout<<"Memory not allocated";
exit(0);
}
while( (ch=getc(fptr)) != EOF )
{
records[j]=ch;
j=j+1;
if(ch=='\n')
{
records[--j]='\0';
i=i+1;
j=0;
}
}
fclose(fptr);
cout<<"\nHex file is:\n";
for(i=0;i<n;i++)
puts(records);
if( ( addr=(char(*)[4])malloc(n*sizeof(*addr)) ) ==NULL )
{
cout<<"Memory not allocated";
exit(0);
}
if( ( bof=(int*)malloc(n*sizeof(int)) ) ==NULL )
{
cout<<"Memory not allocated";
exit(0);
}
cout<<"\nNo. of bytes are:\n";
for(i=0;i<n;i++)
{
k=0;
for(j=1;j<3;j++)
{
temp[k++]=records[j];
}
temp[k]='\0';
bof=atoi(temp);
cout<<" "<<bof;
}
cout<<"\nAddresses are:\n";
for(i=0;i<n;i++)
{
k=0;
for(j=3;j<7;j++)
{
temp[k++]=records[j];
}
temp[k]='\0';
strcpy(addr,temp);
cout<<" "<<addr;
}
free(records);
free(addr);
free(bof);
if(heapcheck()==_HEAPCORRUPT)
cout<<"\nHeap is corrupted";
getch();
return 0;
}
i wrote pgm for reading hex file. the program is working fine btu the problem is that after giving output tc terminates givign illegal operation message.
pls help
thanks
THE CODE:
int main(void)
{
FILE *fptr;
char fname[10],temp[10];
int n=0,i=0,j=0,k=0,*bof;
char ch,(*addr)[4];
char (*records)[44];
clrscr();
cout<<"Enter file name: ";
cin>>fname;
if( (fptr=fopen(fname,"r"
{
cout<<"File not opened";
getch();
exit(0);
}
while( (ch=getc(fptr)) != EOF )
if(ch=='\n')
n=n+1;
rewind(fptr);
records=(char(*)[44])malloc(n*sizeof(*records));
if(records==NULL)
{
cout<<"Memory not allocated";
exit(0);
}
while( (ch=getc(fptr)) != EOF )
{
records[j]=ch;
j=j+1;
if(ch=='\n')
{
records[--j]='\0';
i=i+1;
j=0;
}
}
fclose(fptr);
cout<<"\nHex file is:\n";
for(i=0;i<n;i++)
puts(records);
if( ( addr=(char(*)[4])malloc(n*sizeof(*addr)) ) ==NULL )
{
cout<<"Memory not allocated";
exit(0);
}
if( ( bof=(int*)malloc(n*sizeof(int)) ) ==NULL )
{
cout<<"Memory not allocated";
exit(0);
}
cout<<"\nNo. of bytes are:\n";
for(i=0;i<n;i++)
{
k=0;
for(j=1;j<3;j++)
{
temp[k++]=records[j];
}
temp[k]='\0';
bof=atoi(temp);
cout<<" "<<bof;
}
cout<<"\nAddresses are:\n";
for(i=0;i<n;i++)
{
k=0;
for(j=3;j<7;j++)
{
temp[k++]=records[j];
}
temp[k]='\0';
strcpy(addr,temp);
cout<<" "<<addr;
}
free(records);
free(addr);
free(bof);
if(heapcheck()==_HEAPCORRUPT)
cout<<"\nHeap is corrupted";
getch();
return 0;
}