Posted in 面试题 onMay 03, 2014
已知文件中存有库存产品的记录,该记录由产品代码、产品名称、单价、数量等域组成,设该文件中的内容是按产品代码由小到大的顺序存储(产品代码由1001-1010中间无缺货)。试编程实现当输入某产品代码则打印出该产品记录的功能。
解:#include
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 %d\n”,p.type,p.name,p.price,p.number);
fclose(fp);
}
解:#include
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 %d\n”,p.type,p.name,p.price,p.number);
fclose(fp);
}
编程实现当输入某产品代码则打印出该产品记录的功能
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@