📄 proc_usr.c
字号:
#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define size 2048void inform(){ printf("you can do these things:\n"); printf("press 1 :insert a student;\n"); printf("press 2 :delete a student;\n"); printf("press 3 :to read all of students' information;\n"); printf("press 4 :exit system;\n");}int main(){ int command; char *filename="/proc/proc_text/student"; char name[10]; char id[10]; char buf[25]; char inbuf[size]; int numbytes; int fpw,fpr; inform();//打印菜单 scanf("%d",&command);//读命令号 while(command!=4)//若未选择退出则循环 { switch(command) { case 1://若为插入 printf("input student'name,than press enter:\n"); scanf("%s",name);//读姓名 char ch=getchar();//姓名后的回车 printf("input student'id,than press enter:\n"); scanf("%s",id);//读学号 ch=getchar();//学好后的回车 strcpy(buf,"insert-");//以“命令号-姓名-学号”的形式存入缓冲区,传给核心态 strcat(buf,name); strcat(buf,"-"); strcat(buf,id); fpw=open(filename,O_WRONLY); write(fpw,buf,strlen(buf));//调用写函数 printf("ok\n");//返回成功 close(fpw); break; case 2://若为删除 printf("input student'name,than press enter:\n"); scanf("%s",name); char ch2=getchar(); printf("input student'id,than press enter:\n"); scanf("%s",id); ch2=getchar(); strcpy(buf,"delete-"); strcat(buf,name); strcat(buf,"-"); strcat(buf,id); fpw=open(filename,O_WRONLY); write(fpw,buf,strlen(buf)); printf("ok\n"); close(fpw); break; case 3://若为读 fpr=open(filename,O_RDONLY); numbytes=read(fpr,inbuf,size); inbuf[numbytes]='\0'; printf("%s\n",inbuf); close(fpr); case 4://若退出 break; default://其它命令号显示错误 printf("error\n"); } inform();//为下一选择打印命令号菜单 scanf("%d",&command);//读入下一命令号 } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -