C语言 从磁盘文件读取格式化数据

首先创建一个txt文件
例如,该文件的内容可能如下:
10001.2 1008611.62
0.1123
120.65 4561.123
将该文件保存到项目的在“文件资源管理器中打开文件”中

代码如下:
//使用fscanf读取格式化的文件数据
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable : 4996)
int main() {
float f1, f2, f3, f4, f5;
FILE *fp;
if ((fp = fopen(“1.txt”, “r”)) == NULL) {
fprintf(stderr, “ERROR opening file.\n”);
exit(1);
}
fscanf(fp,"%f %f %f %f %f", &f1, &f2, &f3, &f4, &f5);
printf(“The values are %f ,%f,%f,%f,and %f\n.”,f1,f2,f3,f4,f5);
fclose(fp);
return 0;
}

运行结果:
C语言 从磁盘文件读取格式化数据
更多学习资源,加群!!C语言 从磁盘文件读取格式化数据