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.

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) {

}

Thank you Adak :). It was a stupid mistake. t works now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.