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

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 121 收藏 0 点赞 0 评论 0

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);

}

评论列表
文章目录