📄 modnum.c
字号:
#ifndef MODIFYBYNUMBER
#define MODIFYBYNUMBER
#include "stdio.h"
#include "shead.h"
#include "malloc.h"
/* 按编号修改商品信息 */
void ModifyByNumber()
{
int i,j,k;
long modnum;/* 存放要删节除的商品编号 */
/* 各项修改后的信息 */
long Number;
char Name[20];
float price;
float discount;
float memberprice;
product TempS;
product *s,*t;
int total=PRODUCTSIZE;/* PRODUCTSIZE在头文件中定义 */
int recNumber;/* 原文件中的记录数 */
char DataFile[10]="product",next;
FILE * fp=NULL;
s=(product*)malloc(total*sizeof(product));
if(s==NULL)
{
printf("malloc memory fail!End with any key.\n");
fflush(stdin);
getch();
exit(0);
}
clrscr();
begin:
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key\n",DataFile);
perror("Open file fail");
fflush(stdin);
free(s);/* 释放内存空间 */
getch();
return;
}
/* 输入要修改的商品序号 */
printf("Please input the employee's seatnum withc you want modify:");
scanf("%ld",&modnum);
printf("the product you will delete is%ld\n",modnum);
/* 输入要修改的各项内容 */
Number=modnum;
printf("Name:");
scanf("%s",Name);
printf("price:");
scanf("%f",&price);
printf("discount:");
scanf("%f",&discount);
/* 自动计算商品的会员价 */
memberprice=price*discount;
/* 将文件中要修的信息存入结构体空间 */
recNumber=0;
while(fread(&s[recNumber],sizeof(product),1,fp)!=(int)NULL)
{
if(s[recNumber].Number==modnum)
{
s[recNumber].Number=Number;
strcpy(s[recNumber].Name,Name);
s[recNumber].price=price;
s[recNumber].discount=discount;
s[recNumber].memberprice=memberprice;
}
recNumber++;
if(recNumber>=total)
{
total+=PRODUCTINCREASE;
t=(product*)realloc(s,total*sizeof(product));
if(t==NULL)
{
printf("allot memory fail!\n");
fflush(stdin);
getch();
free(s);
exit(0);
}
s=t;
t=NULL;
}
}
fclose(fp);
/* 将修改后的剩余记录写入文件 */
fp=fopen(DataFile,"wb+");
if(fp==NULL)
{
printf("\nOpen file fail!End with any key.\n");
perror("Open file fail");
fflush(stdin);
free(s);/* 释放内存空间 */
return;
}
for(i=0;i<recNumber;i++)
{
if(fwrite(&s[i],sizeof(product),1,fp)!=1)
{
printf("\nWrite file %s fail!End with any key.\n",DataFile);
perror("Write File fail");
fflush(stdin);
getch();
free(s);/* 释放内存空间 */
return;
}
}
fclose(fp);
/* 显示修改后的文件 */
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key",DataFile);
perror("Open file fail");
fflush(stdin);
getch();
free(s);/* 释放内存空间 */
return;
}
printf("the file after delete is:\n");
printf("\nNumber\tName\t\tprice\t\tdiscount\t\tmemberprice\n");
while(fread(&TempS,sizeof(product),1,fp)!=(int)NULL)
{
if(TempS.Number!=0)
{
printf("%ld\t%s\t\t%.2f\t\t%-8.2f\t\t%.2f\n",TempS.Number,TempS.Name,TempS.price,TempS.discount,TempS.memberprice);
}
}
fclose(fp);
printf("Go on(Y/N)?");
fflush(stdin);
next=getchar();
putchar('\n');
if(next=='Y'||next=='y') goto begin;
free(s);/* 释放内存空间 */
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -