當前位置:首頁 » 編程語言 » 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 00:23:56 瀏覽:361
存儲晶元的片選 發布:2025-03-15 00:14:26 瀏覽:713
javastring參數 發布:2025-03-15 00:04:58 瀏覽:864
chrome腳本推薦 發布:2025-03-15 00:03:29 瀏覽:528
itunes如何切換賬號密碼 發布:2025-03-14 23:50:47 瀏覽:206
壓縮包上級目錄 發布:2025-03-14 23:42:09 瀏覽:100
華為解壓文件夾 發布:2025-03-14 23:41:21 瀏覽:925
易語言發簡訊源碼 發布:2025-03-14 23:38:24 瀏覽:567
編程教訓 發布:2025-03-14 23:30:34 瀏覽:478
山西dns伺服器地址 發布:2025-03-14 23:26:48 瀏覽:970