[問題] 請問一下這段程式的bug
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main () {
char line[4];
char label[7],opcode[7],operand[8];
//char startAddr[5];
int LOCCTR = 0;
char comment[50];
char checkOP[7];
int count = 0;
FILE *input,*inter,*output,*OP;
input = fopen("input.txt","r");
inter = fopen("intermediate.txt","w");
OP = fopen("OPTAB.txt","r");
if(input == NULL){
printf("Can't read input\n");
}
else{
if(fscanf(input,"%s %s %s %s",line,label,opcode,operand)!=EOF){ //處理
第一行的部分
if(!strcmp(opcode,"START")){
fprintf(inter,"%s %04d %s %s %s\n",line,LOCCTR,label,opcode,ope
rand);//將第一階段結果寫入中間檔案
fscanf(input,"%s %s %s %s",line,label,opcode,operand); //讀取下一行
}
else{
printf("failed\n");
}
}
while(strcmp(opcode,"END")){
if(strcmp(label,".")){ //若不是敘述就執行
while(fscanf(OP,"%s",checkOP)!=EOF){ //------------------------->有問題!
!
printf("begin here\n");
if(!strcmp(opcode,checkOP)){
LOCCTR = LOCCTR+3;
}
else if(!strcmp(opcode,"WORD")){
LOCCTR = LOCCTR+3;
}
else if(!strcmp(opcode,"RESW")){
LOCCTR = LOCCTR + 3*atoi(operand);
}
else if(!strcmp(opcode,"RESB")){
LOCCTR = LOCCTR + atoi(operand);
}
else if(!strcmp(opcode,"BYTE")){
if(operand[0] == 'C'){
if(operand[count]!='\0'){
count++;
}
LOCCTR = LOCCTR + (count-3);
}
else if(operand[0] == 'X'){
LOCCTR = LOCCTR + 1;
}
}
}
}
else{ //如果是敘述 則將敘述部分直接貼上
fgets(comment,50,input);
fputs(comment,inter);
fscanf(input,"%s %s %s %s",line,label,opcode,operand); //讀取下一行
}
fprintf(inter,"%s %04d %s %s %s\n",line,LOCCTR,label,opcode,oper
and);//將第一階段結果寫入中間檔案
}
}
system("pause");
return 0;
}
小弟覺得是 while(fscanf(OP,"%s",checkOP)!=EOF)的問題 因為把OP換input 結果雖然
一定是錯的 但不會停止運作
--
Sent from my Android
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.244.195.7
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1430485660.A.C0A.html
→
05/01 21:13, , 1F
05/01 21:13, 1F
→
05/01 21:51, , 2F
05/01 21:51, 2F
→
05/01 21:56, , 3F
05/01 21:56, 3F
推
05/01 22:24, , 4F
05/01 22:24, 4F
→
05/02 13:42, , 5F
05/02 13:42, 5F
→
05/02 15:00, , 6F
05/02 15:00, 6F
→
05/02 20:01, , 7F
05/02 20:01, 7F
→
05/02 21:46, , 8F
05/02 21:46, 8F
→
05/02 21:46, , 9F
05/02 21:46, 9F
→
05/02 21:55, , 10F
05/02 21:55, 10F
→
05/02 23:16, , 11F
05/02 23:16, 11F
推
05/07 22:13, , 12F
05/07 22:13, 12F
→
05/07 22:13, , 13F
05/07 22:13, 13F