當前位置:首頁 » 編程語言 » 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返回this 發布:2025-10-20 08:28:16 瀏覽:551
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:836
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:540
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:719
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:643
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:959
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:213
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:70
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:762
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:668