gpt4 book ai didi

C ,fscanf(), 最后一行被读取两次

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:08 25 4
gpt4 key购买 nike

<分区>

您好,我正在为我的系统软件(汇编器、加载器等)类(class)做一个 C 文件 I/O 测试程序,我的问题是最后一行被读了两次,我记得我的老师告诉我这是由于某些我错过了一些语法或错误,我忘记了它是什么,请看一下并快速帮助我。

程序

#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;
}

源文件.txt

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

输出.txt

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

//最后一行两次

终端输出

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

//最后一行两次

我不希望最后一行被打印或写入两次,我做错了什么,求助!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com