C语言程序设计(第3版_乌云高娃)同步实训指导参考答案2.1 (25)[3页]

2022-07-02 16:32:42   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《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

相关推荐
推荐阅读