0

こんにちは、システム ソフトウェア (アセンブラ、ローダーなど) コースの C ファイル I/O でテスト プログラムを実行しています。問題は、最後の行が 2 回読み込まれることです。見逃したバグ、それが何であるか忘れました。見て、すぐに助けてください。

プログラム

#include<stdio.h>
#include<stdlib.h>
//read from source.txt and write to output.txt

int main()
{
FILE *f1=fopen("source.txt","r");
FILE *f2=fopen("output.txt","w");
int address;
char label[20],opcode[20];
while(!feof(f1))//feof returns 1 if end of file
{
fscanf(f1,"%s\t%s\t%d",label,opcode,&address);
printf("%s\t%s\t%d\n",label,opcode,address);
fprintf(f2,"%s\t%s\t%d\n",label,opcode,address);
}
int check=fclose(f1);
int check2=fclose(f2);
printf("close status %d %d",check,check2);
return 0;
}

source.txt

NULL    LDA     4000
ALPHA   STA     5000
BETA    ADD     4020// I stopped right here, DID NOT PRESS 'ENTER' , so that ain’t the issue

output.txt

NULL    LDA     4000
ALPHA   STA     5000
BETA    ADD     4020
BETA    ADD     4020

// 最後の行を 2 回

ターミナルでの出力

NULL    LDA     4000
ALPHA   STA     5000
BETA    ADD     4020
BETA    ADD     4020

// 最後の行を 2 回

最後の行を 2 回印刷したり書き込んだりしたくありません。何が間違っているのでしょうか。助けてください!

4

1 に答える 1