当前位置:首页 » 编程语言 » 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:37:04 浏览:142
ubuntuhttp服务器的搭建 发布:2025-05-10 14:33:06 浏览:37
微信找回密码申诉要多少时间 发布: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