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.

Attachments
LDA	1
STA	2
LDCH	3
STCH	4
JLT	5
LOOP	1003
THREE	1012
INPUT1	1018
C1	1027
CINPUT	1030

In the second program, you have this bit of code:

char *a,*b;
FILE *fp,*fp1;

fp=fopen("int.txt","r");
fp1=fopen("obj.txt","w");
getsym_op();

while(1)
{
fscanf(fp,"%s",a);

Looks to me like you're assigning a string to a, but a is a pointer (not an array as you've used before with the same name), and a has been given no valid address yet.

I would NEVER have a char array and a char pointer, in the same program, using the same name - for heaven's sake. :(

Also, you use feof(), which doesn't work as you'd expect. Better to use the return you get from fscanf() to know when to break. Better yet, if you're just getting one string per row, use fgets():

while((fgets(buffer, sizeof(buffer), filePointer)) != NULL) {

}

Edited 6 Years Ago by Adak: n/a

This question has already been answered. Start a new discussion instead.