用C语言将链表中的数据写入txt文件



我创建了这个程序,它首先询问你拥有多少只宠物,然后将每只宠物的名字和年龄存储在一个结构体中(都使用链表)。

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

这是我的代码:

#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: %sn", petRecord->name);
        printf("Age of Pet: %dn", petRecord->age);
        petRecord = petRecord->next;
    }
}
void writeToFile()
{
    FILE * fptr;
    fptr = fopen("petnames.txt", "w");
    if(fptr==NULL)
    {
        printf("Errorn");
    }
    else
    {
        while(petRecord != NULL)
        {
            fprintf(fptr, "nPet Name: %snAge: %dn", 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("nn");
    printPetRecord();
    writeToFile();
}

函数printPetRecord()将指针设置为null。

在printPetRecord()中创建如下内容:

struct Node * iterator = petRecord;

然后使用迭代器进行迭代。

#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: %sn", iterator->name);
        printf("Age of Pet: %dn", iterator->age);
        iterator=iterator->next;
    }
}
void writeToFile()
{
    FILE * fptr;
    fptr = fopen("petnames.txt", "w");
    struct Node * iterator = petRecord;
    if(fptr==NULL)
    {
        printf("Errorn");
    }
    else
    {
        while(iterator!= NULL)
        {
            fprintf(fptr, "nPet Name: %snAge: %dn", 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("nn");
    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

经过3年7个月,不知怎么的,我在解决我的问题时得到了这个问题,将linked list的数据写入.txt文件。我来这里是为了寻求我问题的解决方法,但是这个问题还没有得到任何答案。所以我在这里试着回答这个问题。

  1. 程序中不需要全局变量
  2. 给程序中的函数发送一些参数,这会使你的任务更容易。
  3. 在这些函数内部迭代时,通过执行此petRecord = petRecord->next和主newRecord = newRecord->next,您将失去head结构变量。这是因为全局变量。因此,为了避免这种情况,您需要在每次调用的函数中声明一个struct Node类型的local指针变量。
  4. 不要这样做:newRecord->name = malloc(50*sizeof(char));,如果你知道char数组的确切大小,而不是这样做,修改你的struct模板为char name[50]。这比使用指针和动态分配要容易得多,因为在后一种情况下,您需要格外小心和预防。
  5. 打开data写入main()函数的文件,并将其发送给writeToFile()函数。
  6. 处理strings时要格外小心。
  7. 在分配内存或打开文件时检查错误是值得的。因此,每次在程序中执行这些操作时,最好检查操作过程中发生的任何类型的错误。

你为什么不试试这个程序:

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
    char name[50];
    int age;
    struct Node *next;
}Node;
// user-defined functions
void printPetRecord(Node *head);
void writeToFile(FILE *fptr, Node *head);
// main()
int main(void)
{
    int count, i;
    Node *petRecord, *newRecord;
    FILE *fp;
    if( (petRecord = malloc(sizeof(Node))) == NULL )
    {
        fprintf(stderr, "Unable to allocate memory.n");
        exit(2);
    }
    newRecord = petRecord;
    printf("How many pets do you have? ");
    scanf("%d", &count);
    for(i = 0; i < count; i++)
    {
        printf("Name of Pet: ");
        scanf("%50s", newRecord->name);
        printf("Age of Pet: ");
        scanf("%d", &newRecord->age);
        if(i == count-1)
        {
            newRecord->next = NULL;
        }
        else
        {
            if( (newRecord->next = malloc(sizeof(Node))) == NULL)
            {
                fprintf(stderr, "Memory Unavailable.n");
                exit(3);
            }
        }
        newRecord = newRecord->next;
    }
    printf("nn");
    // Modified arguments
    printPetRecord(petRecord);
    // Open file before sending to writeToFile
    if(!(fp = fopen("petname.txt", "w")))
    {
        fprintf(stderr, "Unable to open file "petname.txt"n");
        exit(1);
    }
    // Modified arguments
    writeToFile(fp, petRecord);
    
    fclose(fp);
    return 0;
}
// function to print linked_list
void printPetRecord(Node *head)
{
    if(head->next != NULL)
    {
        printf("Name of Pet: %snAge of Pet: %dn", head->name, head->age);
        printPetRecord(head->next);
    }
    else
        printf("Name of Pet: %snAge of Pet: %dn", head->name, head->age);
}
// function to print list to file
void writeToFile(FILE *fptr, Node *head)
{
    if(head->next != NULL)
    {
        fprintf(fptr, "nPet Name: %snAge: %dnn", head->name, head->age);
        writeToFile(fptr, head->next);
    }
    else
        fprintf(fptr, "nPet Name: %snAge: %dnn", head->name, head->age);
}

输出(控制台):

How many pets do you have? 3
Name of Pet: Tommy
Age of Pet: 3
Name of Pet: Julia
Age of Pet: 4
Name of Pet: Hedgehog
Age of Pet: 5

Name of Pet: Tommy
Age of Pet: 3
Name of Pet: Julia
Age of Pet: 4
Name of Pet: Hedgehog
Age of Pet: 5

输出(petname.txt文件内):


Pet Name: Tommy
Age: 3

Pet Name: Julia
Age: 4

Pet Name: Hedgehog
Age: 5

相关内容

  • 没有找到相关文章

最新更新