user27812
user27812

Reputation: 13

Segmentation Fault using fscanf

Ive written this C code for some basic text processing work.First im reading from a file and then splitting it according to certain characters. However Im getting a segmentation fault when compiling with gcc and when I used gdb it output that fscanf line is causing the segmentation fault. Ive gone thru a number of articles on StackOverflow but none of the suggested slutions solved my problem. Thanks

output

Number of Lines 9
LXI B
Segmentation fault

gdb output for cause of SIGSEGV

#0  0xb7eb0c5d in __isoc99_fscanf ()
   from /lib/i386-linux-gnu/i686/cmov/libc.so.6
#1  0x08048af5 in setupTables () at simGen.c:132
#2  0x08048687 in main () at simGen.c:26

simGen.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<ctype.h>
#include<malloc.h>

#define newlineCheck(t,flg){\
                            if(t == '\n'){\
                                flg=1;\
                            }}\


//GLOBAL VARS
char mnem[50][10],op1[50][5],op2[50][5];


FILE *fp;

void setupTables();
int compare(char *,char *);

int main(){
    setupTables();
    return 0;
}

int compare(char *a,char *b){
    if(strcmp(a,b)){
        return 0;
    }else{
        return 1;
    }
}

void setupTables(){
    char tempch = 'a';
    int commapos = 0,spacepos = 0,digitFlag = 0,colonpos = 0,i = 0;
    char instr[10];
    int counter,num_lines = 1,line_counter = 1;
    FILE* fp = fopen("asm.txt","r");
    while(fscanf(fp,"%c",&tempch)!=EOF){
        if(tempch == '\n'){
            num_lines++;
        }
    }

    printf("Number of Lines %d\n",num_lines);
    rewind(fp);
    do{
        counter = 0;
        spacepos = 0;
        commapos = 0;
        digitFlag = 0;
        colonpos = 0;
        do{
            fscanf(fp,"%c",&tempch);// <- this line seems to be causing segmentation fault
            instr[counter] = tempch;
            if(isdigit(tempch))
                digitFlag = 1;
            if(tempch == ' ')
                spacepos = counter;
            if(tempch == ',')
                commapos = counter;
            if(tempch == ':')
                colonpos = counter;
            counter++;
        }while(tempch != '\n');
        instr[counter - 2] = '\0';

        if(digitFlag == 0){
            i = 0;
            if(colonpos == 0){
                do{
                    mnem[line_counter - 1][i] = instr[i];
                    i++;
                }while(instr[i-1] != '\0');
            }else{
                do{
                    //mnem[line_counter - 1][i] = instr[i];
                    i++;
                }while(instr[i-1] != ':');
                int j = 0;
                do{
                    mnem[line_counter - 1][j] = instr[i];
                    j++;
                    i++;
                }while(instr[i-1] != '\0');
            }
        }else{
            if(colonpos == 0){
                    if(commapos == 0){
                        for(i = 0;i <= spacepos;i++){
                            mnem[line_counter - 1][i] = instr[i];
                        }
                        mnem[line_counter - 1][spacepos] = '\0';
                    }else{
                        for(i = 0;i <= commapos;i++){
                            mnem[line_counter - 1][i] = instr[i];
                        }
                        mnem[line_counter - 1][commapos] = '\0';
                    }
                }else{
                    int j = 0;
                    do{
                    //mnem[line_counter - 1][i] = instr[i];
                    j++;
                }while(instr[j-1] != ':');
                    if(commapos == 0){
                        for(i = 0;i <= spacepos - j + 2;i++){
                            mnem[line_counter - 1][i] = instr[j];
                            j++;
                        }
                        mnem[line_counter - 1][spacepos] = '\0';
                    }else{
                        for(i = 0;i <= commapos - j + 3;i++){
                            mnem[line_counter - 1][i] = instr[j];
                            j++;
                        }
                        mnem[line_counter - 1][commapos] = '\0';
                    }
                }
            }
            printf("%s\n",mnem[line_counter - 1]);
            line_counter++;
        }while(line_counter!=num_lines);
}

asm.txt

LXI B,501A

pol:LDA 4150

LABEL:MOV E,A

LOB:STAX 2130

MOV D,A

LOOP:MVI A,23

LXI H,76AC

HLT

Upvotes: 0

Views: 736

Answers (1)

GoldRoger
GoldRoger

Reputation: 1263

Your problem is this line.

 char instr[10];

When it reaches 3rd line onasm.txt, i.e LABEL:MOV E,A the space allocated by you is insufficient. I increased the space to 20 and program worked.

 char instr[20];

Upvotes: 1

Related Questions