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

📄 creat.c.txt

📁 小商品库存的小软件
💻 TXT
字号:
/*creat.c:用于创建商品信息文件*/
#include "stdio.h"
/*函数CreatFile*/
void CreatFile( )
{
         FILE *fp = NULL;                                   /*定义指向文件的指针*/
         product Tmps;                                      /*定义进行操作时存放结构体变量的*/
         char DataFile[40] = "";                          /*存储商品信息的文件名*/
         int count = 1;                                       /*计算可输入数据的最大范围*/
         /*====输入存放商品信息的文件名====*/
         printf("\n please input new file name of product information.");
         printf("\n Notice:Name of file can't exceed 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't exceed 8 characters,suffix can't
exceed 3 characters.part of exceed will be discarded.\n");
               gets(DataFile);
         }
         /*用二进制写的方式打开文件,即创建文件*/
         fp = fopen(DataFile,"wb+");
         /*如果当前文件不存在,提示打开文件失败*/
         if (fp == NULL)
         {
                printf("\n Open file %s fail!End with any key.\n",DataFile);
                printf("Open file fail");
         

                gatch( );
                exit(1);
          }
          /*如果成功打开或创建文件,则提示输入商品序号、名称、价格要素等相关信息*/
          printf("input product information record.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,Name);

           /*提示输入商品的商品价格*/
           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(*\nsorry,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=fpen(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\tdiscount\tmemberprice\n");
while(fread(&TmpS,sizeof(product),1,fp) != (int)NULL)
{
       printf("\n%ld\t%s\t%4.2f\t%4.2f\t\t4.2f\n",TmpS.Number,
TmpS.Name,TmpS.price,TmpS.discount,TmpS.memberprice);
}


fciose(fp);
}

⌨️ 快捷键说明

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