C标准I/O建立一个文件仓库
实现了 增删改查
创建了一个结构体 保存 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;
}
C标准I/O建立一个文件仓库的更多相关文章
- 在iMac上建立一个git仓库
前沿:一般我们去git clone 的时候,数据直接指向了某某分支的最后一次提交.想想 我们每次提交的不同点都在哪,去 当前工程文件下找到 .git 文件夹,去下面自己看看吧 - .git - .co ...
- 【转载】在Linux下,一个文件也有三种时间,分别是:访问时间、修改时间、状态改动时间
在windows下,一个文件有:创建时间.修改时间.访问时间.而在Linux下,一个文件也有三种时间,分别是:访问时间.修改时间.状态改动时间. 两者有此不同,在Linux下没有创建时间的概念,也就是 ...
- 如何用Docker建立一个Node.js的开发环境
建立一个文件夹 用管理员身份打开powershell. 在文件夹下面运行npm init, 根据提示填入信息,以便产生一个package.json文件. 在文件中加入需要的dependencies ...
- vue+nodejs+express+mysql 建立一个在线网盘程序
vue+nodejs+express+mysql 建立一个在线网盘程序 目录 vue+nodejs+express+mysql 建立一个在线网盘程序 第一章 开发环境准备 1.1 开发所用工具简介 1 ...
- time模块目录下自己建立一个名为log的文件夹
使用python调用ping命令,然后在日志中记录ping的结果,用来监测网络连通情况. 代码: [python]from time import *from subprocess import *w ...
- 建立你的第一个 Git 仓库
虽然 Git 确实是被许多重要软件选作版本控制工具,但是并不是仅能用于这些重要软件;它也能管理你购物清单(如果它们对你来说很重要的话,当然可以了!).你的配置文件.周报或日记.项目进展日志.甚至源代码 ...
- git学习笔记02-创建一个仓库提交一个文件-原来就是这么简单
打开安装好的git bash,设置你的git信息 (这个随便写就行) 初始化一个Git仓库,分三步.(创建文件夹也可以手动创建,也可以命令行创建) 第一步,进到一个目录 cd e: 第二步,创建一 ...
- Git 系列(三):建立你的第一个 Git 仓库
现在是时候学习怎样创建你自己的 Git 仓库了,还有怎样增加文件和完成提交. 在本系列前面的文章中,你已经学习了怎样作为一个最终用户与 Git 进行交互:你就像一个漫无目的的流浪者一样偶然发现了一个开 ...
- Protel99se教程一:建立一个数据库文件
学习Protel99 SE的第一步,是建立一个DDB文件,也就是说,使用protel99se进行电路图和PCB设计,以及其它的数据,都存放在一个统一的DDB数据库中的 一.打开protel 99se后 ...
随机推荐
- web 开发:CSS3 常用属性——速查手册!
web 开发:CSS3 常用属性——速查手册! CSS3 简介:http://www.runoob.com/css3/css3-intro.html 1.目录 http://caniuse.com/ ...
- CSS之基础
css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化.存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式对优缺点. 语法:style = &quo ...
- JQuery实现仿腾讯的固定导航栏
1.描述 窗口滚动一定高度之后才让导航栏固定 2.要点 浏览器滚动的事件:$(window).scroll(functiuon(){ 文档滚过的高度: $(doucment).scrollTop(); ...
- 省市二级联动--使用app-jquery-cityselect.js插件
只有省市二级联动,三级联动还没处理好,会尽快完善. 嵌入id: <div class="form-group"> <label>地址</label&g ...
- 一个简单易懂的javascrip selection&range小案例
在制作富文本编辑器,尤其是在制作以div元素为编辑器区域时,当鼠标离开编辑区域以后会失去焦点,失去选区,这时候就要通过selection&range来重新设置选区.[以下代码尚未考虑IE低版本 ...
- hibernate--对象的三种状态Transient,Persistent,Detached
Transient:刚new出来的对象 Persistent: sava或者update之后的状态 Detached: close或者clear之后 test函数: @Test public void ...
- CDOJ 1272 Final Pan's prime numbers
有些问题,不做实践与猜测,可能一辈子也想不出答案,例如这题. #include<stdio.h> #include<math.h> long long x; int main( ...
- ArcEngine部分工作总结
Arcengine工作总结地物点查询本部分可以在一个窗体中实现,也可以在两个窗体中实现.由于工作要求本人是在两个窗体中实现的:弹出窗体的名称为FormQuery主窗体单机查询时间的代码FormQuer ...
- Django 缓存系统
Django 是动态网站,一般来说需要实时地生成访问的网页,展示给访问者,这样,内容可以随时变化,但是从数据库读多次把所需要的数据取出来,要比从内存或者硬盘等一次读出来 付出的成本大很多. 缓存系统工 ...
- mysql中SQL执行过程详解与用于预处理语句的SQL语法
mysql中SQL执行过程详解 客户端发送一条查询给服务器: 服务器先检查查询缓存,如果命中了缓存,则立刻返回存储在缓存中的结果.否则进入下一阶段. 服务器段进行SQL解析.预处理,在优化器生成对应的 ...