前言
最近一个大一的朋友找我说是不会c语言的文件读写,我就想着给他写个demo,没想到却遇到了一个问题(之前也遇到过,但是没有记录下来,现在居然错了第二次了,所以现在记录下来)。
问题
用feof来作为循环控制条件时会重复读取一次文本末尾的内容
下面是我写的错误demo:
#include <stdio.h>
#include <stdlib.h>
typedef struct stu {
//结构体
int stuId;
int score;
struct stu *next;
} stu, *p_stu;
int main(void) {
p_stu head, p1, p2;
p1 = p2 = head = (p_stu)malloc(sizeof(stu));
fscanf