当前位置:首页 » 编程语言 » 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-07-01 04:37:35 浏览:429
小鸡g4怎么连安卓 发布:2025-07-01 04:37:25 浏览:353
黄金线主图源码 发布:2025-07-01 04:35:38 浏览:295
阿里轻量服务器有固定IP吗 发布:2025-07-01 04:30:27 浏览:504
电脑什么配置要合适 发布:2025-07-01 04:24:15 浏览:164
哪个安卓恢复工具不要钱 发布:2025-07-01 04:21:54 浏览:740
sql加空格 发布:2025-07-01 04:09:38 浏览:578
如何关闭ftp防篡改 发布:2025-07-01 04:09:04 浏览:89
顽固的缓存 发布:2025-07-01 03:28:23 浏览:114
u盘插安卓手机上怎么加密 发布:2025-07-01 03:09:19 浏览:91