当前位置:乐求学 >

好好学习 >考研 >

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

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

已知文件中存有库存产品的记录,该记录由产品代码、产品名称、单价、数量等域组成,设该文件中的内容是按产品代码由小到大的顺序存储(产品代码由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 %dn”,,,e,er);

fclose(fp);

}

  • 文章版权属于文章作者所有,转载请注明 https://m.leqiuxue.com/kywx/3kkk91.html