Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tc terminates after displaying output

Status
Not open for further replies.

hasanasghar00

Programmer
Jul 10, 2002
2
PK
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<<&quot;Enter file name: &quot;;
cin>>fname;

if( (fptr=fopen(fname,&quot;r&quot;)) == NULL )
{
cout<<&quot;File not opened&quot;;
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<<&quot;Memory not allocated&quot;;
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<<&quot;\nHex file is:\n&quot;;
for(i=0;i<n;i++)
puts(records);

if( ( addr=(char(*)[4])malloc(n*sizeof(*addr)) ) ==NULL )
{
cout<<&quot;Memory not allocated&quot;;
exit(0);
}

if( ( bof=(int*)malloc(n*sizeof(int)) ) ==NULL )
{
cout<<&quot;Memory not allocated&quot;;
exit(0);
}

cout<<&quot;\nNo. of bytes are:\n&quot;;
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<<&quot; &quot;<<bof;
}

cout<<&quot;\nAddresses are:\n&quot;;
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<<&quot; &quot;<<addr;
}

free(records);
free(addr);
free(bof);

if(heapcheck()==_HEAPCORRUPT)
cout<<&quot;\nHeap is corrupted&quot;;

getch();
return 0;
}
 
Hi hasanasghar00,
see the corrected code below.

--Maniraja S

#include <stdio.H>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>

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<<&quot;Enter file name: &quot;;
cin>>fname;
if( (fptr=fopen(fname,&quot;r&quot;)) == NULL )
{
cout<<&quot;File not opened&quot;;
getch();
exit(0);
}
while( (ch=getc(fptr)) != EOF )
if(ch=='\n')
n=n+1;
cout << &quot;Number of lines in the program : &quot; << n << endl;
rewind(fptr);
records=(char(*)[44])malloc(n*sizeof(*records));
if(records==NULL)
{
cout<<&quot;Memory not allocated&quot;;
exit(0);
}
while( (ch=getc(fptr)) != EOF )
{
records[j]=ch; // error : in records = ch; assigning a character to a pointer variable.
j=j+1;
if(ch=='\n')
{
records[--j]='\0'; // same type of error as before.
i=i+1;
j=0;
}
}
fclose(fptr);

cout<<&quot;\nHex file is:\n&quot;;
for(i=0;i<n;i++)
puts(records); // Error : puts(records);

if( ( addr=(char(*)[4])malloc(n*sizeof(*addr)) ) == NULL )
{
cout<<&quot;Memory not allocated&quot;;
exit(0);
}

if( ( bof=(int*)malloc(n*sizeof(int)) ) ==NULL )
{
cout<<&quot;Memory not allocated&quot;;
exit(0);
}

cout<<&quot;\nNo. of bytes are:\n&quot;;
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<<&quot; &quot;<<bof;
}

cout<<&quot;\nAddresses are:\n&quot;;
for(i=0;i<n;i++)
{
k=0;
for(j=3;j<7;j++)
{
temp[k++]=records[j]; // Error : records[j]; same type of error.
}
temp[k]='\0';
strcpy(addr,temp); //error : addr is a pointer vaiable to a 4 element array.
cout<<&quot; &quot;<<addr;
}

free(records);
free(addr);
free(bof);

if(heapcheck()==_HEAPCORRUPT)
cout<<&quot;\nHeap is corrupted&quot;;

getch();
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top