The aim of the program is to read contents from 2 files and store it in struct variables.(This is actually a part of another program which stimulates pass2 of two pass assembler)
This code works fine.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct optbl
{
char opco[10];
int value;
}o_tbl[100];
struct symtbl
{
char sym[10];
int addr;
}s_tbl[100];
int count=0,n=0;
main()
{
int i;
char a[100];
FILE *fp2;
fp2=fopen("OP_TBL.TXT","r");
if(fp2)
printf("Opened");
while(1)
{
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
strcpy(o_tbl[count].opco,a);
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
o_tbl[count++].value=atoi(a);
}
for(i=0;i<count;i++)
{
printf("%s",o_tbl[i].opco);
}
fclose(fp2);
fp2=fopen("SYM_TBL.TXT","r");
while(1)
{
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
strcpy(s_tbl[n].sym,a);
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
s_tbl[n++].addr=atoi(a);
}
for(i=0;i<n;i++)
{
printf("%d",s_tbl[i].addr);
}
fclose(fp2);
}
But if I use this code as a part of the original program it gives segmentation fault jus after the execution starts in this part of code The original code is below(Forgive me for the long code :) )
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct optbl
{
char opco[10];
int value;
}o_tbl[100];
struct symtbl
{
char sym[10];
int addr;
}s_tbl[100];
int count=0,n=0,flag=0;
void getsym_op()
{
int i;
char a[100];
FILE *fp2;
printf("Going to open file");
fp2=fopen("OP_TBL.TXT","r");
if(fp2)
printf("Opened");
while(1)
{
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
strcpy(o_tbl[count].opco,a);
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
o_tbl[count++].value=atoi(a);
}
for(i=0;i<count;i++)
{
printf("%s",o_tbl[i].opco);
}
fclose(fp2);
fp2=fopen("SYM_TBL.TXT","r");
while(1)
{
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
strcpy(s_tbl[n].sym,a);
fscanf(fp2,"%s",a);
if(feof(fp2))
break;
s_tbl[n++].addr=atoi(a);
}
for(i=0;i<n;i++)
{
printf("%d",s_tbl[i].addr);
}
fclose(fp2);
}
main()
{
int obco;
char c[100];
char *a,*b;
FILE *fp,*fp1;
fp=fopen("int.txt","r");
fp1=fopen("obj.txt","w");
getsym_op();
while(1)
{
fscanf(fp,"%s",a);
if(feof(fp))
break;
if(strcmp(a,"START")!=0)
{
fprintf(fp1,"%d ",atoi(a));
fscanf(fp,"%s",a);
if(feof(fp))
break;
fscanf(fp,"%s",b);
if(feof(fp))
break;
obco=check(a,b);
if(flag)
{
strcpy(a,b);
fscanf(fp,"%s",b);
if(feof(fp))
break;
obco=check(a,b);
}
fprintf(fp1,"%d\n",atoi(a));
}
}
fclose(fp);
fclose(fp1);
}
The input files are attatched.