当前位置:首页 » 编程语言 » c语言从文件中读取

c语言从文件中读取

发布时间: 2023-08-28 22:27:55

Ⅰ 如何用c语言从txt文件中读取数据

//其中的in.txt就是你要读取数据的文件,当然把它和程序放在同一目录
-------------------------------------
#include
<stdio.h>
int
main()
{
int
data;
file
*fp=fopen("in.txt","r");
if(!fp)
{
printf("can't
open
file\n");
return
-1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return
0;
}

Ⅱ c语言从文件读取数据

四个整型 一个浮点型, 所以 用数组的话 只能是用浮点型数组, 即float a[5];

或者用五个变量, 可以是四个整型,一个浮点型. int a,b,d,e; float c;

打开文件部分相同.

FILE*fp=fopen("input1.txt","r");

读取数据, 数组方式:

inti;
floata[5];
fscanf(fp,"%f,",&a[0]);
for(i=1;i<5;i++)
fscanf(fp,"%f",&a[i]);

变量方式:

inta,b,d,e;
floatc;
fscanf(fp,"%d,%d%f%d%d",&a,&b,&c,&d,&e);
热点内容
java二进制读取 发布:2025-07-03 11:55:52 浏览:739
我的世界服务器记分板排版 发布:2025-07-03 11:39:22 浏览:569
安卓前期用什么处理器 发布:2025-07-03 11:37:54 浏览:870
如何更换安卓手机内存 发布:2025-07-03 11:18:52 浏览:57
魔兽清理缓存 发布:2025-07-03 10:46:38 浏览:521
神州防火墙web怎么配置代码 发布:2025-07-03 10:37:54 浏览:328
安卓看小说哪个软件免费又最好 发布:2025-07-03 10:25:30 浏览:437
linuxprofile 发布:2025-07-03 10:25:29 浏览:719
存储蓝盘 发布:2025-07-03 09:55:10 浏览:887
java必学 发布:2025-07-03 09:21:57 浏览:450