使用fscanf进行分段错误

时间:2014-04-10 17:30:26

标签: c

我已经为一些基本的文本处理工作编写了这个C代码。首先从文件中读取,然后根据某些字符将其拆分。然而,当使用gcc进行编译时,我得到了分段错误,当我使用gdb时,它输出fscanf行导致分段错误。我已经通过StackOverflow上的一些文章,但没有一个建议的流量解决了我的问题。 感谢

输出

Number of Lines 9
LXI B
Segmentation fault

用于SIGSEGV原因的gdb输出

#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

1 个答案:

答案 0 :(得分:1)

你的问题是这一行。

 char instr[10];

当它在asm.txt上达到第3行时,即LABEL:MOV E,A,您分配的空间不足。我将空间增加到20并且程序正常工作。

 char instr[20];