⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 add.c.txt

📁 小商品库存的小软件
💻 TXT
字号:
/*add.c:添加商品信息记录*/
#include "stdio.h"
void AddRecord( )
{
     FILE *fp = NULL;                                            /*定义指向文件的指针*/
     product TmpS;                                               /*定义进行操作时的临时结构体变量*/
     char DataFile[40] = "";                                    /*存储商品信息的文件名*/
     int count = 1;                                                 /*计算可输入数据的最大范围*/
     /*====输入要添加商品信息的文件名====*/
     printf("\n please input the product information file name to add record:");
     printf("\n Notice:Name of file can'texceed 8 characters.suffix can't 
exceed 3 characters,part of exceed will be discarded.\n");
     gets(DataFile);
     /*如顾客没有输入,则循环提示顾客输入*/
     while(*DataFile == ('\0'))
     {
            printf(*\n please input new file name to store data,end with enter.");
            printf("\n Notice:Name of file can'texceed 8 characters,suffix can't 
exceed 3 characters.part of exceed will be discarded.\n");
            gets(DataFile);
     }


     fp = fopen (DataFile,"a+");/*a+:当文件存在时,追加,当文件不存在时,创建*/
     /*如果当前文件不存在,提示打开文件失败*/
     if (fp == NULL)
     {
          printf(*\n Open file %s fail!End with any key.\n*,DataFile);
          perror("Open file fail");


          getch( );
          exit(1);
      }
      /*如果成功打开或创建文件,则提示输入商品序号、名称、价格要素等相关信息*/
       printf("input number,name and salary.number is 0 means input is end.\n");
       printf("Number is not exceed 9 figures,Name is not exceed 20 characters,range of grade:0.00~1000.00\n");
       /*循环从键盘上读取顾客输入的序号、名称、价格要素等相关信息*/
       while(count <=SIZE)
       {
              /*输入序号,如为0则停止输入*/
               printf("\n input 'number =0' means end input.\n");
               printf("number=");
               scanf("%ld",&TmpS.Number);
               if (TmpS.Number == 0 )
                   break;
              /*提示输入商品名称*/
              printf("name=");
              scanf("%s",TmpS.price);

              /*提示输入商品价格*/
              printf("price=");
              scanf("%f",&TmpS.price);

              /*提示输入商品折扣*/
              printf("discount=");
              scanf("%f",&TmpS.discount);


              /*用公式自动计算商品会员价*/
              TmpS.memberprice=TmpS.price*TmpS.discount;


              printf("\n");
              /*如遇无法写入文件的异常,则加以提示*/
              if(fwrite(&TmpS,sizeof(product),1,fp)!=1)
              {
                       printf("\nwrite file %s fail!End with any key\n",DataFile);
                       perror("Write file fail ");
                       getch( );
                       exit(1);
              }
              count++;
      }
      /*如果输入的数据量超过最大允许的范围,则提示数据不能录入*/
      if (count>SIZE)
          printf("\nsory,number of data can not exceed%d\n",SIZE);
      fclose(fp);







      /*====在屏幕上显示文件内容====*/
      /*       clrscr( );*/
      printf("The data you input is store successful %s in file.\n",DataFile);
      printf("Countent as follow:\n");


      fp=fopen(DataFile,"rb");
      if   (fp == NULL)
      {
            printf("\nOpen file%sfail!End with any key \n",DataFile);
            perror("Open file fail");
            getch( );
            exit(1);
       }


       printf("\nNumber\tName\tprice\tdiscountmemberprice\n");
       while(fread(&TmpS,sizeof(product),1,fp) != (int)NULL)
       {
              printf(*\n%ld\t%s\t%4.1f\t%4.1f\t%4.1f\n",TmpS.Number,TmpS.Name,TmpS.price,TmpS.discount,Tmps.memberprice);
       }


       fclose(fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -