#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
struct people
{
char name[10];
float score;
int age;
};
int main()
{
struct people s;
memset(&s, 0, sizeof(struct people));
FILE* pf = NULL;
pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
/*格式化的形式读文件--fscanf*/
fscanf(pf, "%s %f %d", &(s.name), &(s.score), &(s.age));
printf("%s %.2f %d\n", s.name, s.score, s.age);
fclose(pf);
pf = NULL;
return;
}
程序运行时,本地存在的test.txt文件
程序运行结果为
文件打开方式,如下表格: