栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > C++面试题库

编程实现当输入某产品代码则打印出该产品记录的功能

编程实现当输入某产品代码则打印出该产品记录的功能

39. 已知文件中存有库存产品的记录,该记录由产品代码、产品名称、单价、数量等域组成,设该文件中的内容是按产品代码由小到大的顺序存储(产品代码由1001-1010中间无缺货)。试编程实现当输入某产品代码则打印出该产品记录的功能。解:   #include <stdio.h>struct product{  int type;char name[10];double price;int  number;};main(){FILE  *fp;struct  product p;int size;int t;long int i;size=sizeof(struct product);printf(“input the type(from 1001 to 1010):”);scanf(“%d”,&t);if((fp=fopen(“product”,”rb”))==NULL){printf(“cannot open the file!”);exit(0);}i=(t-1000)*size;fseek(fp,i,0);fread(&p,size,1,fp);printf(“%d %s %f %dn”,p.type,p.name,p.price,p.number);fclose(fp);}

已知文件中存有库存产品的记录,该记录由产品代码、产品名称、单价、数量等域组成,设该文件中的内容是按产品代码由小到大的顺序存储(产品代码由1001-1010中间无缺货)。试编程实现当输入某产品代码则打印出该产品记录的功能。

解:   #include <stdio.h>

struct product{  int type;

char name[10];

double price;

int  number;};

main()

{

FILE  *fp;

struct  product p;

int size;

int t;

long int i;

size=sizeof(struct product);

printf(“input the type(from 1001 to 1010):”);

scanf(“%d”,&t);

if((fp=fopen(“product”,”rb”))==NULL)

{

printf(“cannot open the file!”);

exit(0);

}

i=(t-1000)*size;

fseek(fp,i,0);

fread(&p,size,1,fp);

printf(“%d %s %f %dn”,p.type,p.name,p.price,p.number);

fclose(fp);

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/265886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号