C语言怎样把链表存入文件,然后从文件读取到链表

树濑杨先生 2016-03-23 02:51:10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX 200
typedef struct node{
int number;
char name[MAX];
int counter;
struct node *prior,*next;
}node;
node* head = NULL;
void output_one(node* n)
{
printf("%d\t%s\t%d\n", n->number, n->name, n->counter);
}
void output()
{
node* pos = head;
if(head == NULL)
{
return;
}
while (pos)
{
output_one(pos);
pos = pos->next;
}
}
int insert()
{
node* pos = head;
node* n = malloc(sizeof(node));
n->prior = NULL;
n->next = NULL;
printf("请输入商品编号:");
scanf("%d", &n->number);
printf("请输入商品名称:");
scanf("%s", &n->name);
printf("请输入商品数量:");
scanf("%d", &n->counter);
if(head==NULL)
{
head = n;
return 1;
}
while (pos)
{
if(pos->number > n->number)
{
if(pos->prior)
pos->prior->next = n;
n->prior = pos->prior;
pos->prior = n;
if(pos->next)
pos->next->prior = n;
n->next = pos;
return 1;
}
else if(pos->number == n->number)
{
free(n);
return 0;
}
if (!pos->next)
{
pos->next = n;
n->prior = pos;
return 1;
}
pos = pos->next;
}
return 1;
}
void init()
{
while (1)
{
insert();
printf("按任意键继续输入,按Esc停止输入\n");
if(getch()==27)
break;
}
}
int delete()
{
int num;
node* pos = head;
printf("请输入要删除的编号:");
scanf("%d", &num);

if(head == NULL)
{
return 0;
}
while (pos)
{
if(pos->number == num)
{
if(pos->prior)
pos->prior->next = pos->next;
if(pos->next)
pos->next->prior = pos->prior;
free(pos);
printf("出库完毕!");
return 1;
}
pos = pos->next;
}
return 0;
}
int amend()
{
int num, count;
node* pos = head;
printf("请输入要修改的编号:");
scanf("%d", &num);
printf("请输入要修改的数量:");
scanf("%d", &count);
if(head == NULL)
{
return 0;
}

while (pos)
{
if(pos->number == num)
{
if (count == 0)
{
if(pos->prior)
pos->prior->next = pos->next;
if(pos->next)
pos->next->prior = pos->prior;
free(pos);
return 1;
}
pos->counter = count;
return 1;
}
pos = pos->next;
}
return 0;
}
void search()
{
int num;
node* pos = head;
printf("请输入要查找的编号:");
scanf("%d", &num);
if(head == NULL)
{
return;
}
while (pos)
{
if(pos->number == num)
{
output_one(pos);
return;
}
else
{
printf("找不到该商品!\n");
}
pos = pos->next;
}
}
void search_by_name()
{
char name[MAX];
node* pos = head;
printf("请输入要查找的名称:");
scanf("%s", name);
if(head == NULL)
{
printf("找不到该商品!\n");
return;
}
while (pos)
{
if(!strcmp(pos->name, name))
{
output_one(pos);
}
pos = pos->next;
}
}
void jiemian()
{
printf(" 仓库管理系统\n");
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" ! 请选择: !\n");
printf(" ! 1.商品入库 !\n");
printf(" ! 2.商品出库 !\n");
printf(" ! 3.修改库存 !\n");
printf(" ! 4.商品查找 !\n");
printf(" ! 5.商品盘点 !\n");
printf(" ! 6.退出程序 !\n");
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
int chazhao(di)
{
char ch;
if(di==1)
{
init();
}
if(di==2)
{
delete();
}
if(di==3)
{
amend();
}
if(di==4)
{
printf("1. 按编号查找\n2. 按名称查找\n");
ch = getch();
if(ch == '1')
{
search();
}
else if(ch == '2')
{
search_by_name();
}
}
if(di==5)
{
output();
}
if(di==6)
{
exit(0);
}
return di;
}
int main()
{
int d;
jiemian();
scanf("%d",&d);
chazhao(d);
while (1)
{
char ch;
system("cls");
jiemian();
ch = getch();
switch (ch)
{
case '1':
init();
break;

case '2':
delete();
break;

case '3':
amend();
break;

case '4':
printf("1. 按编号查找\n2. 按名称查找\n");
ch = getch();
if(ch == '1')
{
search();
}
else if(ch == '2')
{
search_by_name();
}
break;

case '5':
output();
break;

case '6':
return 0;
break;
}
printf("按任意键继续...\n");
getch();
}
return 0;
}
...全文
1177 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolong806124 2016-03-25
  • 打赏
  • 举报
回复
自己去找下c如何读写文件的知识点看下。网上一堆
赵4老师 2016-03-23
  • 打赏
  • 举报
回复
仅供参考:
//文件1:
//编号12 23 34;
//文件2:
//编号 x y z
//1 3.45 4.2 3.6
//2 2.45 4.2 3.6
//  ........
//277 4.00 8.38 9.92
//278 5.00 8.38 9.92
#include <stdio.h>
FILE *f1,*f2,*f3;
char ln[80];
int r,n,n2,n21;
float x,y,z;
int xyz_of_line(int nn,float *xx,float *yy,float *zz) {
    n2=-1;//跳过第一行:编号 x y z
    rewind(f2);
    while (1) {
        if (NULL==fgets(ln,80,f2)) break;
        n2++;
        if (n2==nn) {
            r=sscanf(ln,"%d%f%f%f",&n21,xx,yy,zz);
            if (4==r) {
                if (n21==nn) {
                    return 1;
                } else {
                    printf("WARNING:编号 error in line %4d:%s",nn,ln);
                    return 1;
                }
            } else {
                printf("ERROR:Format error in line %4d:%s",nn,ln);
                return 0;
            }
        }
    }
    return 0;
}
void main() {
    f1=fopen("文件1","r");
    if (NULL==f1) {
        printf("ERROR:fopen 文件1 error!\n");
        return;
    }
    f2=fopen("文件2","r");
    if (NULL==f2) {
        fclose(f1);
        printf("ERROR:fopen 文件2 error!\n");
        return;
    }
    f3=fopen("文件3","w");
    if (NULL==f3) {
        fclose(f2);
        fclose(f1);
        printf("ERROR:fopen 文件3 error!\n");
        return;
    }
    while (1) {
        r=fscanf(f1,"%d",&n);
        if (1==r) {
            r=xyz_of_line(n,&x,&y,&z);
            if (1==r) {
                printf("%4d:x=%g,y=%g,z=%g\n",n,x,y,z);
                fprintf(f3,"%4d:x=%g,y=%g,z=%g\n",n,x,y,z);
            } else {
                printf("ERROR:Can not read line %4d!\n",n);
            }
        } else if (0==r) {
            fgetc(f1);
        } else break;
    }
    fclose(f3);
    fclose(f2);
    fclose(f1);
}
树濑杨先生 2016-03-23
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
fprintf fscanf
不是很懂额,我文件部分学的不好
赵4老师 2016-03-23
  • 打赏
  • 举报
回复
fprintf fscanf
1.基于数组的“学生信息管理系统” 实验内容: 编写并调试程序,实现学校各专业班级学生信息的管理。定义学生信息的结构体类型,包括:学号、姓名、专业、班级、3门成绩。N定义为符号常量,定义N名学生信息的结构体数组。 实验要求: main函数:以菜单形式将各项功能提供给用户,根据用户的选择,调用相应的函数。 功能: (1)定义函数Input:功能是可以从键盘输入任意个学生信息。 (2)定义函数Save:将输入的学生信息全部或者选择性的存入指定文件(如:输入5个学生信息,选择其中前3或者2个存入文件,或者全部存入文件)。 (3)定义函数Output:将某个学生信息格式化输出(学生信息文件读取,并且提供可选择的学号)。 (4)定义函数Fetch:文件中随机读取某个学生的信息。 (5)定义函数Del:删除指定学号学生信息,并保存到原文件中。 (6)定义函数:实现输出所有学生信息的功能(包括学生的平均分和总分)。 (7)定义函数Max:求所有学生某门课程的最高分,并将此学生的分数以及学生姓名输出(注意:当有多名相同最高分数时,可将所有学生姓名输出)。 (8)定义函数Sort_select:对某个专业的学生,按总平均成绩由低到高进行简单选择排序。 (9)定义函数Sort_buble:对某个专业中某个班级的学生,按总平均成绩由高到低进行起泡排序。 (10)定义函数Sort_insert:对某个专业中某个班级的学生,按某门课程成绩由低到高进行直接插入排序。 (11)定义函数Search:实现某专业中某班级的成绩综合查找(如智能专业1班,总分240分以上同学)。 (12) 定义函数printmenu: 打印菜单 退出 2.基于链表的“学生信息管理系统” 实验内容:编写并调试程序,实现学校各专业班级学生信息的管理。定义学生信息的链表结点类型,包括:学号、姓名、班级、专业、3门成绩。 实验要求: (1)main函数:以菜单形式将各项功能提供给用户,根据用户的选择,调用相应的函数。 (2)定义函数CreateList:按学号由小到大,建立有序的链表。逆序输入 n 个学生信息(调用n次input),学号大的先输入,建立带头结点的单链表。 (3)定义函数Output:以指向某个学生结点的指针为参数,将学生信息格式化输出。 (4)定义函数Save:将某个学生信息存入文件。 (5)定义函数Fetch:文件中随机读取某个学生的信息。 (6)定义函数Search_num:查找指定学号的学生,返回指向该学生结点的指针。 (7)定义函数InsertList:在函数中输入一个学生的信息,将该学生信息插入到链表中的相应位置,并保持此链表按学号的有序性。 (8)定义函数Delete_num:从链表中删除指定学号的学生。 (9)定义函数Search_major _subject_score:查找某个专业的、某门课程的成绩小于某个分数的学生,返回指向该学生结点的指针。 (10)定义函数Delete_ major _subject:从链表中删除某个专业的、某门课程的成绩小于某个分数的学生。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧