如何从txt文件中读取链表?

问题描述 投票:0回答:0

所以我有一个链接列表,我可以将其保存在 txt 文件中。现在的问题是我如何从同一个文件中读取它?我尝试了我在网上找到的所有可能的方法,但实际上没有任何效果。

我有 3 个文件:main、struct 和 function。

main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <stdbool.h>
#include "struct.h"
#include "function.h"

int main()
{
    system("cls");
    while(go)
    {
        userChose=Menu();
        Bloc();
    }
    return 0;
}

结构.h:

typedef struct node
{
    char name[256];
    int  amount;
    struct node *next;
} node;

int go = 1;
int userChose = 0;
int n;
node * head = NULL;

和功能.h:

int Menu()
{
    printf("\t\t\t--Menu--\n");
    printf("1. Create a linked list with n nodes.\n");
    printf("2. Show linked list.\n");
    printf("16. Save in txt.\n");
    printf("17. Read from txt.\n");
    int c;
    printf("\t\t\nOption: ");
    scanf("%d",&c);
    system("cls");
    return c;
}

node * CreateList()
{
    node * head = NULL;
    node * temp = NULL;
    node * ptr = NULL;
    printf("number of nodes: ");
    scanf("%d\n",&n);
    for (int i=0;i<n;i++)
    {
        temp = (node*)malloc(sizeof(node));
        getchar();
        gets(temp->Denumire);

        printf("amount: ");
        scanf("%d",&(temp->amount));

        temp->next = NULL;

        if (head == NULL)
        {
            head = temp;
        }
        else
        {
            ptr = head;
            while(ptr->next != NULL)
            {
                ptr = ptr->next;
            }
            ptr->next = temp;
        }
    }
    return head;
}

void ShowList()
{
    node * p = head;

    while(p != NULL)
    {
        printf("---------------------------\n");
        printf("Name: %s\n",p->name);
        printf("Amount: %d\n",p->amount);
        printf("curr adress: %p\n",p);
        printf("next adress: %p\n",p->next);
        printf("---------------------------\n");
        p = p->next;
    }
}

void Save()
{
    FILE * file = fopen("liste.txt","w");
    node * p = head;

    while(p != NULL)
    {
        fprintf(file,"---------------------------\n");
        fprintf(file,"name: %s\n",p->name);
        fprintf(file,"amount: %d\n",p->amount);
        fprintf(file,"adress: %p\n",p);
        fprintf(file,"next adress: %p\n",p->next);
        fprintf(file,"---------------------------\n");
        p=p->next;
    }
    fclose(file);
}

最后:

void PressAnyKey()
{
    printf("\n--Apasati pentru a continua--\n");
    _getch();
    system("cls");
}

void Bloc()
{
    switch (userChose)
    {
    case 1:
        {
            head = CreateList();
            PressAnyKey();
        }
        break;
    case 2:
        {
            ShowList();
            PressAnyKey();
        }
        break;
case 16:
        {
            Save();
            printf("List saved in txt.");
            PressAnyKey();
        }
        break;
    case 17:
        {
            Read();
            PressAnyKey();
        }
        break;
case 0:
        {
            go = 0;
            system("cls");
            printf("Exit done.\n");
            _getch();
        }
        break;
    default:
        system("cls");
        printf("Error\n");
        break;
    }
}

我尝试使用 fread() 但它似乎根本没有运行。

c linked-list txt
© www.soinside.com 2019 - 2024. All rights reserved.