教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

电1C语言实验报告(11)

来源:网络收集 时间:2026-08-03
导读: #include #include #define N 5 struct stud { long num; char name[20]; float score[3]; float aver; }student[N]; main() {FILE *fp; char numstr[20]; int i,j; 31 《计算方法与程序设计实验》 float sum; if((
#include #include #define N 5 struct stud {

long num;

char name[20]; float score[3]; float aver; }student[N]; main() {FILE *fp;

char numstr[20]; int i,j;

31

《计算方法与程序设计实验》

float sum;

if((fp=fopen(\{printf(\getch(); exit(1);}

for(i=0;i

printf(\

printf(\printf(\for(j=0;j<3;j++)

{printf(\gets(numstr);

student[i].score[j]=atol(numstr); sum+=student[i].score[j]; }

student[i].aver=sum/3;

fwrite(&student[i],sizeof(student[i]),1,fp); }

fclose(fp); }

Key:输入截图

32

《计算方法与程序设计实验》

若要验证输入文件中的内容是否正确,可以将文件的内容读出来,但不能直接打开二进 制形式的文件,可以利用以下程序打开文件,调试运行并验证输入是否正确。 #include #include #include struct stud

{long num;char name[20];float score[3];float aver;}student; main()

{FILE *fp;int i;

if((fp=fopen(\{printf(\getch(); exit(1);}

while(fread(&student,sizeof(student),1,fp)==1) {printf(\printf(\for(i=0;i<3;i++)

printf(\printf(\printf(\}}

Key:输出截图

33

《计算方法与程序设计实验》

2. 以下函数的功能是把文本文件 B 的内容追加到文本文件A 的内容之后。补全程序并验证。 #include #define N 80 main() {

FILE *fp,*fp1,*fp2; int i;

char c[N],t,ch;

if( ==NULL) {

printf(\exit(1); }

printf(\

for(i=0;(ch=fgetc(fp))!=EOF;i++) {

c[i]=ch;

}

fclose(fp);

if((fp=fopen(\{

printf(\exit(1); }

printf(\

for(i=0;(ch=fgetc(fp))!=EOF;i++) {

c[i]=ch;

putchar(c[i]); }

fclose(fp);

if((fp1=fopen(\{

while((ch=fgetc(fp2))!=EOF) } else {

printf(\}

fclose(fp1); fclose(fp2);

printf(\if((fp=fopen(\{

printf(\exit(1); }

for(i=0;(ch=fgetc(fp))!=EOF;i++) {

c[i]=ch;

34

《计算方法与程序设计实验》

putchar(c[i]); }

putchar('\\n'); fclose(fp); }

Key:第一空应填:(fp=fopen(\

第二空应填:putchar(ch); 或 putchar(c[i]); /*输出变量或数组元素*/ 第三空应填:&&

第四空应填:fputc(ch,fp1);

注意:若此程序运行前,先用C++集成开发环境新建文本文件a.dat,其内容为asdfg;新建文本文件b.dat,其内容为qwerty。则第一次运行结果为:

此题最好改成如下形式:否则每次运行结果不同。

2. 以下函数的功能是把文本文件 B 的内容追加到文本文件A 的内容之后。补全程序并验证。 #include #define N 80 main() {

FILE *fp,*fp1,*fp2; int i;

char c[N]={'a','s','d','f','g'},t,ch; if( ==NULL) {

printf(\exit(1); }

printf(\for(i=0;i<5;i++) {

putchar(c[i]); }

fclose(fp);

if((fp=fopen(\{

printf(\exit(1); }

printf(\

for(i=0;(ch=fgetc(fp))!=EOF;i++) {

c[i]=ch;

putchar(c[i]); }

fclose(fp);

35

电1C语言实验报告(11).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/595017.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)