当前位置:首页 » 编程语言 » 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);
热点内容
被登记上传染病会怎样 发布:2025-03-15 10:26:47 浏览:968
编译原理视频下载 发布:2025-03-15 10:25:46 浏览:844
ftp用户权限设置linux 发布:2025-03-15 10:19:32 浏览:239
极光大数据库 发布:2025-03-15 10:11:48 浏览:582
智e付忘了登录密码在哪里修改 发布:2025-03-15 10:05:20 浏览:650
手机热点密码忘了怎么办 发布:2025-03-15 09:28:26 浏览:363
缓解压力锻炼方法 发布:2025-03-15 09:23:01 浏览:426
impdp存储过程 发布:2025-03-15 09:20:05 浏览:741
pythoniris 发布:2025-03-15 09:05:27 浏览:190
浪淘沙服务器怎么没有了 发布:2025-03-15 09:05:26 浏览:100