码迷,mamicode.com
首页 > 其他好文 > 详细

C标准I/O建立一个文件仓库

时间:2016-07-08 21:54:40      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

实现了 增删改查 

创建了一个结构体 保存 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;
}

C标准I/O建立一个文件仓库

标签:

原文地址:http://www.cnblogs.com/liudehao/p/5654622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!