📄 creat.c
字号:
#ifndef CREATFILE
#define CREATFILE
#include "stdio.h"
/* 初始化商品信息文件,在磁盘文件中存放若干条商品信息记录供顾客查询 */
/* 输入序号,名称,价格,折扣,会员价 */
void CreatFile()
{
FILE * fp=NULL;
product TempS;
char DataFile[10]="product";
fp=fopen(DataFile,"wb+");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key.\n",DataFile);
perror("Open file fail");
fflush(stdin);
getch();
exit(0);
}
clrscr();
/* 如成功打开或他建文件,则提示输入商品序号名称,价格要素等相关信息 */
printf("input product infomation record.number is 0 means input is end.\n");
while(1)
{
printf("\ninput 'number=0' means end input.\n");
printf("number:");
scanf("%ld",&TempS.Number);
if(TempS.Number==0)
break;
/* 提示输入商品名 */
printf("Name:");
scanf("%s",TempS.Name);
/* 提示输入商品名价格 */
printf("price:");
scanf("%f",&TempS.price);
/* 提示输入商品折扣 */
printf("discount:");
scanf("%f",&TempS.discount);
/* 用公式自动计算会员价 */
TempS.memberprice=TempS.price*TempS.discount;
printf("\n");
/* 如遇无法写入文件的异常,则加以提示 */
if(fwrite(&TempS,sizeof(product),1,fp)!=1)
{
printf("\nwrite file %s fail!End with any key.\n",DataFile);
perror("Open file fail");
fflush(stdin);
getch();
return;
}
}
fclose(fp);
/* 在屏幕上显示文件内容 */
printf("The data you input is store successful.\n");
printf("Content as follow:\n");
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key.\n");
perror("Open file fail");
fflush(stdin);
getch();
return;
}
printf("\nNumber\tname\tprice\t\tdiscount\t\tmemberprice\n");
while(fread(&TempS,sizeof(product),1,fp)!=(int)NULL)
{
printf("%ld\t%s\t%.2f\t\t%-8.2f\t\t%.3f\n",TempS.Number,TempS.Name,
TempS.price,TempS.discount,TempS.memberprice);
}
fclose(fp);
printf("press any key to continue...");
fflush(stdin);
getch();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -