1. 选择题。

1-1,判断各种变量所占的字节数。

#include<stdio.h>

int main()
{
char p[] = {'6','2','3'}, *q = p;
int t;
printf("%d\n",sizeof(t));
printf("%d\n",sizeof(p));
printf("%d\n",sizeof(char));
printf("%d\n",sizeof(*q));
printf("%d\n",sizeof(p[0])); printf("%c", *p); return 0; }

结果:

4

3

1

1

1

1-2

3. 程序题。

1-41 程序通过定义学生结构体变量,存储了学生的学号、姓名和三门课成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是重写形参filename所指文件中最后一个学生的数据,即用新的学生数据覆盖原来的学生数据,其他的数据不变。

 #include  <stdio.h>
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n)
{ FILE *fp;
/**********found**********/
fp = fopen(filename, "rb+");
/**********found**********/
fseek(fp, -(long)sizeof(STU), SEEK_END); // 整个意思为从文件末尾向前偏移这个结构体那么多的长度给文件指针fp
/**********found**********/
fwrite(&n, sizeof(STU), 1,fp);
//参数&n :是一个指针,对fwrite来说,是要输出数据的地址。
//参数sizeof(struct strdent_type) :要写入内容的单字节数;
//参数1 : 要进行写入sizeof(struct strdent_type)字节的数据项的个数是1.
//参数fp:目标文件指针。
fclose(fp);
}
main()
{ STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}};
STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n\n");
for (j=0; j<N; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
fun("student.dat", n);
printf("\nThe data after modifing :\n\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j<N; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
}

最新文章

  1. [转]利用URLConnection来发送POST和GET请求
  2. MJExtension框架介绍
  3. hdu 1045 Fire Net(最小覆盖点+构图(缩点))
  4. C#...何时需要重写ToString()方法?
  5. 我的第一个Spring程序
  6. Database 2 Day DBA guide_Chapter2
  7. 从集合的无序性看待关系型数据库中的&quot;序&quot;
  8. 微信小程序-列表渲染多层嵌套循环
  9. Git从入门到差不多会用
  10. Win7 SP1 提示ADO的问题
  11. Spring源码研究--下载-编译-导入eclipse-验证
  12. UVALIVE 3891 The Teacher&#39;s Side of Math
  13. hdu1430 魔板(康拓展开 bfs预处理)
  14. SQLyog点击“测试连接”后,报2058错误
  15. java中常用的集合的一些区别 (2013-10-07-163写的日志迁移
  16. (13)zabbix External checks 外部命令检测
  17. mysql_mssql_access_2017年最新手机号段归属地数据库(17年4月更新)360569记录
  18. 地图POI类别标签体系建设实践
  19. Eclipse For Mac下中文乱码解决
  20. Python+Tornado+Tampermonkey 获取某讯等主流视频网站的会员视频解析播放

热门文章

  1. ASP.NET 内容管理系统CMS
  2. 不重装nginx实现动态添加模块
  3. TCP/IP通信过程(以发送电子邮件为例)(转)
  4. Gamification and Game-Based Learning
  5. darw colorful more
  6. webpack打包vue单文件组件
  7. 10-网页,网站,微信公众号基础入门(使用微信自带配置选项实现Airkiss配网)
  8. SIT系统整合测试
  9. 论文阅读:Single Image Dehazing via Conditional Generative Adversarial Network
  10. java复制对象之深拷背