当前位置:网站首页>C document reading and writing plus linked list addition, deletion, modification and query
C document reading and writing plus linked list addition, deletion, modification and query
2022-07-24 05:31:00 【Mo xiaodai ^o^】
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define size sizeof(struct b)
struct b{
int num;
char name[10];
int gs;
struct b*next;
};
void read();//fscanf
void read1();//fgetc
void write();
void list();
void bianli();
void zengjia();
void shanchu();
void xiugai();
void bianlixie();
struct b*head=NULL;
int main()
{
int c;
do{
printf(" Please input the operation :");
scanf("%d",&c);
switch(c){
case 1:read();
break;
case 2:write();
break;
case 3:list();
break;
case 4:zengjia();
break;
case 5:shanchu();
break;
case 6:bianli();
break;
case 7:xiugai();
break;
case 0:
break;
}
}while(c!=0);
return 0;
}
void list()
{
FILE*fp;
int num;
char name[10];
int gs;
struct b*p,*tail,*ptr;
head=tail=NULL;
fp=fopen("E:\\book.txt","r");
if(fp==NULL)
{
printf(" File opening failure \n");
}
else{
while(fscanf(fp, "%d%s%d", &num, name, &gs) != -1)
{
p=(struct b*)malloc(size);
p->num=num;
strcpy(p->name,name);
p->gs=gs;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
//printf("%d\t%s\t%d\n",num,name,gs);
}
}
fclose(fp);
}
void bianli()
{
struct b*ptr;
if(head==NULL){
printf("No\n");
}
else{
for(ptr=head;ptr!=NULL;ptr=ptr->next){
printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->gs);
}
}
printf("\n");
}
void write()
{
FILE*fp;
int num,gs;
char name[10];
int i,n;
fp=fopen("E:\\book.txt","a+");
if(fp==NULL)
{
printf(" File opening failure \n");
}
else
{
printf(" Please enter the number of books added :");
scanf("%d",&n);
printf(" Please enter the information of adding books :\n");
for(i=1;i<=n;i++){
scanf("%d%s%d",&num,name,&gs);
fprintf(fp,"\n%d\t%s\t%d",num,name,gs);
}
}
fclose(fp);
}
void read1()
{
FILE*fp;
fp=fopen("E:\\book.txt","r");
if(fp==NULL)
{
printf(" File opening failure \n");
}
else
{
char ch;
printf(" Read information as :\n");
while(!feof(fp))
{
ch=fgetc(fp);
putchar(ch);
}
fclose(fp);
}
}
void read()
{
FILE*fp;
int num;
char name[10];
int gs;
fp=fopen("E:\\book.txt","r");
if(fp==NULL)
{
printf(" File opening failure \n");
}
else{
while(fscanf(fp, "%d%s%d", &num, name, &gs) != -1)
printf("%d\t%s\t%d\n",num,name,gs);
}
printf("\n");
fclose(fp);
}
void zengjia()
{
int n,i;
int num,gs;
char name[10];
struct b*ptr,*L,*ptr1;
printf(" Please enter the number of increases :");
scanf("%d",&n);
for(i=0;i<n;i++){
printf(" Please enter %d Information :",i+1);
scanf("%d%s%d",&num,name,&gs);
if(head==NULL){
ptr=(struct b*)malloc(size);
ptr->num=num;
strcpy(ptr->name,name);
ptr->gs=gs;
head=ptr;
}
else{
L=head;
while(L->next!=NULL){
L=L->next;
}
ptr1=(struct b*)malloc(size);
ptr1->num=num;
strcpy(ptr1->name,name);
ptr1->gs=gs;
ptr1->next=NULL;
L->next=ptr1;
}
}
bianlixie();
}
void shanchu()
{
int num;
printf(" Please enter the deletion book number :");
scanf("%d",&num);
struct b*ptr1,*ptr2,*p;
if(head->num==num&&head!=NULL)
{
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL)
{
printf(" No information \n");
}
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL){
if(ptr2->num==num){
ptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr1->next;
}
bianlixie();
}
void xiugai()
{
int gs;
int num;
printf(" Please enter the revision number :");
scanf("%d",&num);
printf(" Please enter the number of modifications as :");
scanf("%d",&gs);
struct b*ptr;
if(head==NULL){
printf(" No information \n");
}
else{
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
if(ptr->num==num)
{
ptr->gs=gs;
}
}
}
bianlixie();
}
void bianlixie()
{
FILE*fp;
int num,gs;
char name[10];
int i,n;
struct b*ptr;
fp=fopen("E:\\book.txt","w");
if(fp==NULL){
printf(" Open the failure \n");
}
else{
for(ptr=head;ptr!=NULL;ptr=ptr->next){
fprintf(fp,"%d\t%s\t%d\n",ptr->num,ptr->name,ptr->gs);
}
}
fclose(fp);
}边栏推荐
猜你喜欢
随机推荐
day(0~6)代表每月第一天起始位置,stop代表每月天数,每天之间空两个空格。输入不同的day和stop,输出每月日历的样子。假设day为2,stop为31,则输出样式为
C语言从入门到入土(三)
special effects - 鼠标移动,出现自定义的表情拖尾
special effects - 蜘蛛网背景特效
OPENGL在屏幕上绘制2个点,右边一个蓝色的点,采用反走样技术,左边一个红色的点,不采用反走样技术。比较两个点的区别。
Promise_ Async and await
special effects - 樱花飘落背景特效
umi之define属性
Hurry in!! Take you to understand what is multi file, and easily master the usage of extern and static C language keywords!!!
New grammar 01_ ES6 new syntax
4. 在屏幕上绘制一个红色三角形,一个黄色正方形。三角形在后,小;正方形在前,大。使用融合技术,使得可以透过正方形看到三角形,源和目标融合因子分别为GL_SRC_ALPHA和GL_ONE_MINUS
02 mobile terminal page adaptation
Promise续(尝试自己实现一个promise)更详细的注释和其它接口暂未完成,下次咱们继续。
3. 在屏幕上绘制一个底是正方形的五面锥体,锥体的底面在XOZ平面上,锥顶在Y轴上。用下图给锥体的四个三角形面做纹理映射,使得锥体的四个面分别是红橙黄绿色。
JS输出字符串中出现最多次数的字符
Geoserver自动化上传Shapefile
Pure white tutorial using Druid database connection pool in idea
利用二分法从数组中寻找具体数值
Scikit learn notes
vscode配置autoprefixer




![JS:为什么 [] == ![] 返回 true ?](/img/36/94839bf4ce6bd06d2cbe989828c791.png)




