编程实现当输入某产品代码则打印出该产品记录的功能
编辑: 面试啦 发布时间: 2023-05-14 02:26:11
已知文件中存有库存产品的记录,该记录由产品代码、产品名称、单价、数量等域组成,设该文件中的内容是按产品代码由小到大的顺序存储(产品代码由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);
}
词条:c面试题
上一篇:这段代码难道不该打印出56吗 下一篇:枚举和一组预处理的#define有什么不同
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题