當前位置:首頁 » 編程語言 » 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);
熱點內容
android獲取數組 發布:2025-08-22 02:24:04 瀏覽:645
徵型壓縮機 發布:2025-08-22 02:10:15 瀏覽:494
真空壓縮袋能上飛機嗎 發布:2025-08-22 02:10:01 瀏覽:94
怎麼刪除伺服器文件 發布:2025-08-22 02:04:07 瀏覽:168
爐石傳說威脅腳本投降 發布:2025-08-22 01:54:10 瀏覽:331
大大哇腳本 發布:2025-08-22 01:49:32 瀏覽:94
python2pip 發布:2025-08-22 01:48:56 瀏覽:388
php和null 發布:2025-08-22 01:48:49 瀏覽:964
安卓手機怎麼強制解鎖 發布:2025-08-22 01:48:48 瀏覽:81
安卓422怎麼樣 發布:2025-08-22 01:48:48 瀏覽:625