[問題] 請問一下這段程式的bug

看板C_and_CPP作者 (有妹妹真好)時間10年前 (2015/05/01 21:07), 編輯推噓2(2011)
留言13則, 7人參與, 最新討論串1/1
#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
要不試著debug看看
05/01 21:56, 3F

05/01 22:24, , 4F
Google the return value of fscanf...
05/01 22:24, 4F

05/02 13:42, , 5F
不太懂google之後 要看哪些東西
05/02 13:42, 5F

05/02 15:00, , 6F
你為什麼會用這種方法判斷end of file?
05/02 15:00, 6F

05/02 20:01, , 7F
這樣不好嗎?
05/02 20:01, 7F

05/02 21:46, , 8F
叫你debug了,bug還要我找?opcode從來沒變,當然出
05/02 21:46, 8F

05/02 21:46, , 9F
不去迴圈
05/02 21:46, 9F

05/02 21:55, , 10F
難怪google之後還是不知道要看什麼。
05/02 21:55, 10F

05/02 23:16, , 11F
樓上已經有提示關鍵了.基於尊重,通常貼code會排版或用網頁
05/02 23:16, 11F

05/07 22:13, , 12F
你想想要跟eof比較的是甚麼?是fscanf的回傳值還是op這
05/07 22:13, 12F

05/07 22:13, , 13F
個變數?那該是什麼東西==?
05/07 22:13, 13F
文章代碼(AID): #1LGtgSmA (C_and_CPP)