【小知识】C语言中,当使用fgets函数读取文件到字符数组中时,会覆盖字符数组原有成员
//测试当使用fgets函数读取文件到字符数组中时,会覆盖字符数组原有成员
#include<stdio.h>
#include<conio.h>
void main() {
char str1[20] = {"student"};
char str2[20] = { "teacher" };
FILE *fp;
fp = fopen("test.txt","w+");
fputs(str1,fp);
fclose(fp);
fp = fopen("test.txt", "r+");
fgets(str2,20,fp);
printf("%s",str2);
getchar();
}
运行截图: