【#第一文档网# 导语】以下是®第一文档网的小编为您整理的《C语言程序设计(第3版_乌云高娃)同步实训指导参考答案2.1 (25)[3页]》,欢迎阅读!
深圳职业技术学院 C语言程序设计 同步实训指导参考答案 序号:10.2 任务1:从键盘输入5本教材的信息(书名、作者、价格、出版社),然后保存在文件“book.dat”中。 #include "stdio.h" struct type_book { char chName[10]; char chAuthor[10]; float fPrice; char chPress[10]; }; void main(){ struct type_book stBook[5]; FILE *filePointer; int i; filePointer=fopen("book.dat","wb"); if(filePointer==NULL){ printf("cannot open this file\n"); exit(0); } for(i=0;i<5;i++) { printf("please input the book name,author,price,press:"); scanf("%s%s%f%s",stBook[i].chName,stBook [i].chAuthor, &stBook [i].fPrice,&stBook [i].chPress); 1 深圳职业技术学院 C语言程序设计 fwrite(&stBook [i],sizeof(struct type_book),1,filePointer) ; } fclose(filePointer); getch(); } 任务2:将“book.dat”的内容输出在屏幕上(注意每条信息输出为一行)。 #include "stdio.h" #include "stdio.h" struct type_book { char chName[10]; char chAuthor[10]; float fPrice; char chPress[10]; }; void main() { struct type_book stBook[5]; FILE *filePointer; int i; filePointer=fopen("book.dat","rb"); if(filePointer==NULL){ printf("cannot open this file\n"); exit(0); } printf("Name Author Price Press\n"); for(i=0;i<5;i++) { 2 深圳职业技术学院 C语言程序设计 fread(&stBook[i],sizeof(struct type_book),1,filePointer) ; } fclose(filePointer); for(i=0;i<5;i++){ printf("%s\t%s\t%f\t%s\n",stBook[i].chName, stBook [i].chAuthor, stBook [i].fPrice,stBook [i].chPress); } getch(); } 3 本文来源:https://www.dywdw.cn/0fe3e65aab956bec0975f46527d3240c8447a162.html