作者winbill84063 (有妹妹真好)
看板C_and_CPP
標題[問題] 請問一下這段程式的bug
時間Fri May 1 21:07:38 2015
#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
→ tpps86511: & 05/01 21:13
→ TobyH4cker: 不是&的問題 05/01 21:51
→ TobyH4cker: 要不試著debug看看 05/01 21:56
推 johnjohnlin: Google the return value of fscanf... 05/01 22:24
→ winbill84063: 不太懂google之後 要看哪些東西 05/02 13:42
→ MOONRAKER: 你為什麼會用這種方法判斷end of file? 05/02 15:00
→ winbill84063: 這樣不好嗎? 05/02 20:01
→ TobyH4cker: 叫你debug了,bug還要我找?opcode從來沒變,當然出 05/02 21:46
→ TobyH4cker: 不去迴圈 05/02 21:46
→ MOONRAKER: 難怪google之後還是不知道要看什麼。 05/02 21:55
→ dirkc: 樓上已經有提示關鍵了.基於尊重,通常貼code會排版或用網頁 05/02 23:16
推 overhead: 你想想要跟eof比較的是甚麼?是fscanf的回傳值還是op這 05/07 22:13
→ overhead: 個變數?那該是什麼東西==? 05/07 22:13