欢迎来到天天文库
浏览记录
ID:55738141
大小:34.00 KB
页数:3页
时间:2020-06-03
《同步实训指导参考答案同步实训指导参考答案10.2.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、同步实训指导参考答案序号:10.2任务1:从键盘输入5本教材的信息(书名、作者、价格、出版社),然后保存在文件“book.dat”中。#include"stdio.h"structtype_book{charchName[10];charchAuthor[10];floatfPrice;charchPress[10];};voidmain(){structtype_bookstBook[5];FILE*filePointer;inti;filePointer=fopen("book.dat","wb");if(filePointer==NULL){printf("cannotope
2、nthisfile");exit(0);}for(i=0;i<5;i++){printf("pleaseinputthebookname,author,price,press:");scanf("%s%s%f%s",stBook[i].chName,stBook[i].chAuthor,&stBook[i].fPrice,&stBook[i].chPress);fwrite(&stBook[i],sizeof(structtype_book),1,filePointer);}fclose(filePointer);getch();}任务2:将“book.dat”的内容输出在屏幕
3、上(注意每条信息输出为一行)。#include"stdio.h"#include"stdio.h"structtype_book{charchName[10];charchAuthor[10];floatfPrice;charchPress[10];};voidmain(){structtype_bookstBook[5];FILE*filePointer;inti;filePointer=fopen("book.dat","rb");if(filePointer==NULL){printf("cannotopenthisfile");exit(0);}printf("Name
4、AuthorPricePress");for(i=0;i<5;i++){fread(&stBook[i],sizeof(structtype_book),1,filePointer);}fclose(filePointer);for(i=0;i<5;i++){printf("%st%st%ft%s",stBook[i].chName,stBook[i].chAuthor,stBook[i].fPrice,stBook[i].chPress);}getch();}
此文档下载收益归作者所有