當前位置:首頁 » 編程語言 » c語言讀寫結構體

c語言讀寫結構體

發布時間: 2025-05-10 12:19:16

1. C語言中可否將整個結構體寫入文件,然後取出來也是按結構體輸出

可以,寫的時候用指針一次性寫進去,但是如果文件中寫如了多個結構體你可能讀取的時候要區分一下否則一次全部讀出來了,讀出來的時候直接賦值就行。

2. 用C語言編寫 文件讀寫

第一種:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
structstudent//結構體
{
charname[20];//姓名
intenglish;//英語
intmath;//數學
intprogram;//程序設計
}s[50];
voidru(structstudents[],int*n)//文件導入函數
{
FILE*p;
inti=*n;
if((p=fopen("students.txt","r"))==NULL)
{
printf("無法打開此文件!");
}
else
{
while(!feof(p))
{
fscanf(p,"%s%d%d%d",s[i].name,&s[i].english,&s[i].math,&s[i].program);
i++;
*n=*n+1;
}
}
fclose(p);
}
voidpaixu(structstudents[],intn)//排序函數
{
inti,j;
structstudentstu;
intallscore[2];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
allscore[0]=s[i].english+s[i].math+s[i].program;
allscore[1]=s[j].english+s[j].math+s[j].program;
if(allscore[0]<allscore[1])
{
stu=s[i];
s[i]=s[j];
s[j]=stu;
}
}
}
}
voiddayin(structstudents[],intn)//顯示所有信息
{
inti;
printf(" 姓名 英語 數學 程序設計 總分 ");
for(i=0;i<n;i++)
{
printf("%s %d %d %d %d ",s[i].name,s[i].english,s[i].math,s[i].program,(s[i].english+s[i].math+s[i].program));
}
}
intmain()//主函數
{
intk,n=0;
ru(s,&n);
paixu(s,n);
dayin(s,n);
return0;
}

輸出結果:

第二種二進制的導入也差不多,這里就不寫了

3. C語言文件讀寫結構體裡面的數據怎樣存到磁碟文件上

1、首先打開VC++6.0。

熱點內容
微信找回密碼申訴要多少時間 發布:2025-05-10 14:14:05 瀏覽:435
大眾寶來速騰選哪個配置 發布:2025-05-10 14:10:53 瀏覽:128
數字機頂盒密碼是多少 發布:2025-05-10 14:10:06 瀏覽:334
取消訪問網路需要密碼 發布:2025-05-10 13:44:20 瀏覽:64
shell編程運行 發布:2025-05-10 13:37:54 瀏覽:640
win7訪問xp共享需要密碼 發布:2025-05-10 13:34:10 瀏覽:344
飯團看書為什麼緩存不了小說 發布:2025-05-10 13:17:03 瀏覽:13
如何配置登錄源地址限制 發布:2025-05-10 13:12:52 瀏覽:591
農行修改網銀密碼忘了怎麼辦 發布:2025-05-10 13:12:43 瀏覽:521
android鍵盤開發 發布:2025-05-10 13:06:16 瀏覽:816