簡體   English   中英

使用fscanf的分段錯誤

[英]Segmentation Fault using fscanf

我已經為一些基本的文本處理工作編寫了C語言代碼。首先是從文件中讀取,然后根據某些字符將其拆分。 但是,我在使用gcc進行編譯時遇到分段錯誤,當我使用gdb時,它輸出fscanf行導致分段錯誤。 我在StackOverflow上瀏覽了許多文章,但是沒有建議的解決方案解決了我的問題。 謝謝

產量

Number of Lines 9
LXI B
Segmentation fault

gdb輸出,原因為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

波爾:LDA 4150

標簽:MOV E,A

客:STAX 2130

MOV D,A

回路:MVI A,23

LXI H,76AC

HLT

您的問題是這條線。

 char instr[10];

當它到達asm.txt第三asm.txt ,即LABEL:MOV E,A ,您分配的空間不足。 我將空間增加到20,程序正常運行。

 char instr[20];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM