当前位置:网站首页>C文件读写加链表增删改查
C文件读写加链表增删改查
2022-07-24 05:16:00 【陌小呆^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("请输入操作:");
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("文件打开失败\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("文件打开失败\n");
}
else
{
printf("请输入添加书籍个数:");
scanf("%d",&n);
printf("请输入添加书籍信息:\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("文件打开失败\n");
}
else
{
char ch;
printf("读取信息为:\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("文件打开失败\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("请输入增加个数:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("请输入%d信息:",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("请输入删除书籍编号:");
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("没有信息\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("请输入修改编号:");
scanf("%d",&num);
printf("请输入修改个数为:");
scanf("%d",&gs);
struct b*ptr;
if(head==NULL){
printf("没有信息\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("打开失败\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);
}边栏推荐
猜你喜欢
随机推荐
ros启动非本机节点
C语言进阶篇 七.程序的编译和预处理
Implementation and comparison of nine sorting (ten thousand words summary)
Introduction to threads
Redis的使用
OSS文件上传
Pointer learning diary (IV) use structure and pointer (linked list)
【dp】数字三角形
Reading excerpts from Liu run's "bottom logic"
统计学之样本和总体的关系: 样本成功比例+中心极限定理(样本均值)
WIX 路径中带空格
jdbc封装一个父类减少代码重复
T 6-10
线程
C语言起步
Text summary acl2021
栈与队列的互相实现(C)
This is the first article
安装Pytorch+anaconda+cuda+cudnn
Theoretical basis of machine learning









