【#第一文档网# 导语】以下是®第一文档网的小编为您整理的《C语言程序设计(第3版_乌云高娃)同步实训指导参考答案2.1 (26)[3页]》,欢迎阅读!
深圳职业技术学院 C语言程序设计 同步实训指导参考答案 序号:10.3 任务1:从键盘输入5个手机的信息(商标名、价格、颜色),然后保存在文件“phone.dat”中。 #include "stdio.h" struct type_phone { char chBrand[10]; float fPrice; char chColor[10]; }; void main(){ struct type_phone stPhone[5]; FILE *filePointer; int i; filePointer=fopen("Phone.dat","wb"); if(filePointer==NULL){ printf("cannot open this file\n"); exit(0); } for(i=0;i<5;i++) { printf("please input the brand name, price,color:"); scanf("%s%f%s",stPhone[i].chBrand,&stPhone [i].fPrice,&stPhone [i].chColor); fwrite(&stPhone [i],sizeof(struct type_phone),1,filePointer) ; 1 深圳职业技术学院 } fclose(filePointer); getch(); } C语言程序设计 任务2:将文件“phone.dat”中的第1、3、5条记录读出,并给每条记录的价格加上100元,然后将这三条记录输出在屏幕上(注意每条信息输出为一行)。 #include "stdio.h" #include "stdio.h" struct type_phone { char chBrand[10]; float fPrice; char chColor[10]; }; void main() { struct type_phone stPhone[3]; FILE *filePointer; int i; filePointer=fopen("Phone.dat","rb"); if(filePointer==NULL){ printf("cannot open this file\n"); exit(0); } for(i=0;i<3;i++) { fseek(filePointer,2* sizeof(struct type_phone),SEEK_SET); fread(stPhone[i], sizeof(struct type_phone),1,filePointer); stPhone.fPrice+=100; 2 深圳职业技术学院 } fclose(filePointer); C语言程序设计 printf("BrnadName Price Color\n"); for(i=0;i<3;i++) { printf("%s\tt%f\t%s\n",stPhone[i].chBrand, stPhone [i].fPrice, stPhone [i].chColor); } getch(); } 3 本文来源:https://www.dywdw.cn/1b26cbf282c758f5f61fb7360b4c2e3f5627255d.html