[英]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. 我已经为一些基本的文本处理工作编写了C语言代码。首先是从文件中读取,然后根据某些字符将其拆分。 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.
但是,我在使用gcc进行编译时遇到分段错误,当我使用gdb时,它输出fscanf行导致分段错误。 Ive gone thru a number of articles on StackOverflow but none of the suggested slutions solved my problem.
我在StackOverflow上浏览了许多文章,但是没有建议的解决方案解决了我的问题。 Thanks
谢谢
output 产量
Number of Lines 9
LXI B
Segmentation fault
gdb output for cause of SIGSEGV 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 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 asm.txt
LXI B,501A LXI B,501A
pol:LDA 4150 波尔:LDA 4150
LABEL:MOV E,A 标签:MOV E,A
LOB:STAX 2130 客:STAX 2130
MOV D,A MOV D,A
LOOP:MVI A,23 回路:MVI A,23
LXI H,76AC LXI H,76AC
HLT HLT
Your problem is this line. 您的问题是这条线。
char instr[10];
When it reaches 3rd line on asm.txt
, ie LABEL:MOV E,A
the space allocated by you is insufficient. 当它到达
asm.txt
第三asm.txt
,即LABEL:MOV E,A
,您分配的空间不足。 I increased the space to 20 and program worked. 我将空间增加到20,程序正常运行。
char instr[20];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.