add.c

来自「商品管理系统 1)进货员对商品基本信息进行输入、删除、修改和查询 (2)销售员」· C语言 代码 · 共 79 行

C
79
字号
#ifndef ADDRECORD
 #define ADDRECORD
 #include "stdio.h"
 /* 添加商品信息文件*/
/* 输入序号,名称,价格,折扣,会员价 */
 void AddRecord()
 {
  FILE * fp=NULL;
  product TempS;
  char DataFile[10]="product";
  fp=fopen(DataFile,"ab+");
  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 + =
减小字号Ctrl + -
显示快捷键?