當前位置:首頁 » 編程語言 » 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);
熱點內容
linux怎麼編譯c文件 發布:2025-09-18 09:55:16 瀏覽:301
python安裝後無法運行 發布:2025-09-18 09:45:57 瀏覽:236
安卓手機怎麼剪輯音樂 發布:2025-09-18 09:44:16 瀏覽:781
伺服器地址修改在哪找 發布:2025-09-18 09:42:41 瀏覽:441
sntp伺服器地址 發布:2025-09-18 09:28:36 瀏覽:552
phpunit 發布:2025-09-18 09:25:19 瀏覽:571
怎麼改伺服器的ip地址嗎 發布:2025-09-18 09:24:33 瀏覽:12
編譯703n固件 發布:2025-09-18 08:50:59 瀏覽:539
三星手機系統文件夾是哪個文件夾 發布:2025-09-18 08:48:45 瀏覽:282
rmijava 發布:2025-09-18 08:38:26 瀏覽:20