实现了 增删改查
创建了一个结构体 保存 num name cout price
代码
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/mman.h>
typedef struct
{
int num;
char name[32];
int count;
int price;
}GOOD;
int main(int argc,char *argv[])
{
FILE *fp;
GOOD good;
fp=fopen(argv[1],"a+");
if(NULL==fp)
{
perror("fopen1");
return -1;
}
fclose(fp);
fp=fopen(argv[1],"r+");
if(NULL==fp)
{
perror("fopen2");
return -1;
}
int n;
printf("please input your select function\n\n");
printf("----1-------------store good\n");
printf("----2------------browse good\n");
printf("----3--------------seek good\n");
printf("----4-------------alter good\n");
printf("----5------------delete good\n");
printf("----6--------------rank good\n");
printf("----7------------exit system\n");
while(scanf("%d",&n)==1)
{
getchar();
if(n==1)
{
printf("please input you want store good's num name count price\n");
scanf("%d%s%d%d",&good.num,good.name,&good.count,&good.price);
fprintf(fp,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
printf("store success\n");
}
else if(n==2)
{
int flag;
rewind(fp);
while(1)
{
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
printf("\nlist above is all good\n\n\n");
break;
}
printf("%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
}
else if(n==3)
{
char str[32];
int find=0;
int flag;
rewind(fp);
printf("please input you want seek good name\n");
scanf("%s",str);
getchar();
while(1)
{
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
if(find==0)
printf("\nI am sorry not have you want seek good\n\n\n");
else
printf("\nabove list is you want seek all good\n\n");
break;
}
if(strcmp(str,good.name)==0)
{
find++;
printf("\n%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
}
}
else if(n==4)
{
int number;
int flag;
rewind(fp);
printf("please input you want alter good's num\n");
scanf("%d",&number);
getchar();
while(1)
{
int place=(int)ftell(fp);
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
printf("not have fine you want alter num\n");
break;
}
if(number==good.num)
{
printf("please input you altered'information\n");
scanf("%d%s%d%d",&good.num,good.name,&good.count,&good.price);
fseek(fp,place,SEEK_SET);
fprintf(fp,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
printf("alter success!\n");
break;
}
}
}
else if(n==5)
{
int success=0;
int number;
int place_1;
int flag;
rewind(fp);
printf("please input you want delete good's num\n");
scanf("%d",&number);
getchar();
while(1)
{
int place=(int)ftell(fp);
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
//printf("not have fine you want delete num\n");
fseek(fp,place_1,SEEK_SET);
fprintf(fp,"%c",EOF);
printf("delete success\n!");
break;
}
if(success)
{
fseek(fp,place_1,SEEK_SET);
fprintf(fp,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
if(number==good.num)
{
success=1;
}
place_1=place;
}
}
else if(n==6)
{
}
else if(n==7)
{
fclose(fp);
printf("you exit success welcome come again\n");
break;
}
else
{
printf("your input error please input again\n\n");
}
printf("please input your select function\n\n");
printf("----1-------------store good\n");
printf("----2------------browse good\n");
printf("----3--------------seek good\n");
printf("----4-------------alter good\n");
printf("----5------------delete good\n");
printf("----6--------------rank good\n");
printf("----7------------exit system\n");
}
return 0;
}
更加完善之后的代码
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/mman.h>
typedef struct
{
int num;
char name[32];
int count;
int price;
}GOOD;
int main(int argc,char *argv[])
{
FILE *fp,*fp1;
GOOD good;
fp=fopen(argv[1],"a+");
if(NULL==fp)
{
perror("fopen1");
return -1;
}
fclose(fp);
fp=fopen(argv[1],"r+");
if(NULL==fp)
{
perror("fopen2");
return -1;
}
int n;
printf("please input your select function\n\n");
printf("----1-------------store good\n");
printf("----2------------browse good\n");
printf("----3--------------seek good\n");
printf("----4-------------alter good\n");
printf("----5------------delete good\n");
printf("----6--------------rank good\n");
printf("----7------------exit system\n");
while(scanf("%d",&n)==1)
{
getchar();
if(n==1)
{
rewind(fp);
int flag,success,flag_1;
GOOD in_good;
success=0;
flag_1=0;
fp1=fopen("./store_fu.txt","w+");
if(NULL==fp1)
{
perror("fopen5");
return 0;
}
printf("please input you want store good's information num name count price\n");
scanf("%d%s%d%d",&in_good.num,in_good.name,&in_good.count,&in_good.price);
getchar();
while(1)
{
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
if(success==0)
{
fprintf(fp,"%5d%16s%5d%5d\n",in_good.num,in_good.name,in_good.count,in_good.price);
fclose(fp1);
break;
}
fclose(fp);
fp=fopen(argv[1],"w+");
if(NULL==fp)
{
perror("fopen4");
return -1;
}
rewind(fp1);
while(1)
{
flag=fscanf(fp1,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
fclose(fp1);
break;
}
fprintf(fp,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
break;
}
if(in_good.num<good.num&&flag_1==0)
{ success=1;
flag_1=1;
fprintf(fp1,"%5d%16s%5d%5d\n",in_good.num,in_good.name,in_good.count,in_good.price);
}
fprintf(fp1,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
printf("store success\n");
}
else if(n==2)
{
int flag;
rewind(fp);
while(1)
{
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
printf("\nlist above is all good\n\n\n");
break;
}
printf("%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
}
else if(n==3)
{
char str[32];
int find=0;
int flag;
rewind(fp);
printf("please input you want seek good name\n");
scanf("%s",str);
getchar();
while(1)
{
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
if(find==0)
printf("\nI am sorry not have you want seek good\n\n\n");
else
printf("\nabove list is you want seek all good\n\n");
break;
}
if(strcmp(str,good.name)==0)
{
find++;
printf("\n%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
}
}
else if(n==4)
{
int number;
int flag;
rewind(fp);
printf("please input you want alter good's num\n");
scanf("%d",&number);
getchar();
while(1)
{
int place=(int)ftell(fp);
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
printf("not have find you want alter num\n");
break;
}
if(number==good.num)
{
printf("please input you altered'information\n");
fprintf(stdout,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
scanf("%d%s%d%d",&good.num,good.name,&good.count,&good.price);
fseek(fp,place,SEEK_SET);
fprintf(fp,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
printf("alter success!\n");
break;
}
}
}
else if(n==5)
{
int success=0;
int number;
int flag;
fp1=fopen("./store_fu.txt","w+");
if(NULL==fp1)
{
perror("fopen3");
return -1;
}
rewind(fp);
printf("please input you want delete good's num\n");
scanf("%d",&number);
getchar();
while(1)
{
flag=fscanf(fp,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{
fclose(fp);
fp=fopen(argv[1],"w+");
if(NULL==fp)
{
perror("fopen4");
return -1;
}
rewind(fp1);
while(1)
{
flag=fscanf(fp1,"%d%s%d%d\n",&good.num,good.name,&good.count,&good.price);
if(-1==flag||0==flag)
{ if(success)
{
printf("delete success!\n");
}
else
{
printf("not find you want delete num\n");
}
fclose(fp1);
break;
}
fprintf(fp,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
break;
}
if(number==good.num)
{
success=1;
}
else
{
fprintf(fp1,"%5d%16s%5d%5d\n",good.num,good.name,good.count,good.price);
}
}
}
else if(n==7)
{
fclose(fp);
printf("you exit success welcome come again\n");
break;
}
else
{
printf("your input error please input again\n\n");
}
printf("please input your select function\n\n");
printf("----1-------------store good\n");
printf("----2------------browse good\n");
printf("----3--------------seek good\n");
printf("----4-------------alter good\n");
printf("----5------------delete good\n");
printf("----6--------------rank good\n");
printf("----7------------exit system\n");
}
return 0;
}