将数据从链接列表写入C中的txt文件 [英] Writing data from a linked list to a txt file in C

查看:48
本文介绍了将数据从链接列表写入C中的txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个程序,该程序首先询问您拥有多少只宠物,然后将每个宠物的名称和年龄存储在结构中(全部使用链接列表).

I created this program that first asks how many pets you own, then stores the name and age of each pet in a struct (all using linked lists).

我的问题是:我正在尝试使用过程 writeToFile()将数据写入.txt文件,但是在执行时,.txt文件不包含任何数据.我不明白为什么?

My question is: I'm trying to write the data into a .txt file using a procedure writeToFile() but upon execution, the .txt file does not contain any data. I don't understand why?

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

struct Node {
    char *name;
    int age;
    struct Node *next;
};

struct Node * petRecord;
struct Node * newRecord;

void printPetRecord()
{  
    while(petRecord != NULL)
    {
        printf("Name of Pet: %s\n", petRecord->name);
        printf("Age of Pet: %d\n", petRecord->age);
        petRecord = petRecord->next;
    }
}

void writeToFile()
{
    FILE * fptr;
    fptr = fopen("petnames.txt", "w");

    if(fptr==NULL)
    {
        printf("Error\n");
    }

    else
    {
        while(petRecord != NULL)
        {
            fprintf(fptr, "\nPet Name: %s\nAge: %d\n", petRecord->name, petRecord->age);
            petRecord = petRecord->next;
        }
    }

    fclose(fptr);
    }

int main()
{ 
    int count, i;
    printf("How many pets do you have? ");
    scanf("%d", &count);

    for(i=0; i<count; i++)
    {
        if(i==0)
        {
            petRecord = malloc(sizeof(struct Node));
            newRecord = petRecord;
        }
        else
        {
            newRecord->next = malloc(sizeof(struct Node));
            newRecord = newRecord->next;
        }
        newRecord->name = malloc(50*sizeof(char));
        printf("Name of Pet: ");
        scanf("%s", newRecord->name);
        printf("Age of Pet: ");
        scanf("%d", &newRecord->age);
    }
    newRecord->next = NULL;
    printf("\n\n");
    printPetRecord();
    writeToFile();
}

推荐答案

您的函数printPetRecord()将指针设置为null.

Your function printPetRecord() leaves your pointer set to null.

在printPetRecord()内部进行如下操作:

Inside printPetRecord() make something like this:

struct Node * iterator = petRecord;

,然后使用迭代器进行迭代.

and then itertae using iterator.

#include <stdio.h>
#include <stdlib.h>

struct Node {
    char *name;
    int age;
    struct Node *next;
};

struct Node * petRecord;
struct Node * newRecord;

void printPetRecord()
{
    struct Node * iterator = petRecord;
    while(iterator != NULL)
    {
        printf("Name of Pet: %s\n", iterator->name);
        printf("Age of Pet: %d\n", iterator->age);
        iterator=iterator->next;
    }
}

void writeToFile()
{
    FILE * fptr;
    fptr = fopen("petnames.txt", "w");
    struct Node * iterator = petRecord;

    if(fptr==NULL)
    {
        printf("Error\n");
    }

    else
    {
        while(iterator!= NULL)
        {
            fprintf(fptr, "\nPet Name: %s\nAge: %d\n", iterator->name, iterator->age);
            iterator= iterator->next;
        }
    }

    fclose(fptr);
    }

int main()
{
    int count, i;
    printf("How many pets do you have? ");
    scanf("%d", &count);

    for(i=0; i<count; i++)
    {
        if(i==0)
        {
            petRecord = malloc(sizeof(struct Node));
            newRecord = petRecord;
        }
        else
        {
            newRecord->next = malloc(sizeof(struct Node));
            newRecord = newRecord->next;
        }
        newRecord->name = malloc(50*sizeof(char));
        printf("Name of Pet: ");
        scanf("%s", newRecord->name);
        printf("Age of Pet: ");
        scanf("%d", &newRecord->age);
    }
    newRecord->next = NULL;
    printf("\n\n");
    printPetRecord();
    writeToFile();
}

执行:

> gcc -o main main.c
> ./main
How many pets do you have? 2
Name of Pet: a
Age of Pet: 2
Name of Pet: b
Age of Pet: 3


Name of Pet: a
Age of Pet: 2
Name of Pet: b
Age of Pet: 3
> cat petnames.txt

Pet Name: a
Age: 2

Pet Name: b
Age: 3

这篇关于将数据从链接列表写入C中的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆