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

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

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《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”中的第135条记录读出,并给每条记录的价格加上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

相关推荐
推荐阅读