我创建了这个程序,首先询问您拥有多少宠物,然后将每个宠物的名称和年龄存储在一个结构中(所有使用链接列表)。
我的问题是:我尝试使用过程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: %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();
}
答案 0 :(得分:3)
您的函数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: %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
答案 1 :(得分:1)
3年7个月后,我以某种方式解决了将linked list
的数据写入.txt
文件的问题时遇到了这个问题。我来这里是为了寻找解决问题的方法,但是这个问题没有得到任何答案。因此,在这里我将尝试回答这个问题。
- 程序中不需要全局变量。
- 向程序中的函数发送一些参数,这会使您的任务容易得多。
- 在这些函数内部进行迭代时,通过执行此
head
和在主petRecord = petRecord->next
内部,您正在丢失newRecord = newRecord->next
结构变量。这是由于全局变量而发生的。因此,为避免这种情况,您需要每次在被调用函数内部声明local
类型的struct Node
指针变量。- 请勿执行以下操作:
newRecord->name = malloc(50*sizeof(char));
,如果您知道char
数组的确切大小而不是这样做,请将struct
模板修改为char name[50]
。比使用指针和动态分配要容易得多,因为在后一种情况下,您需要格外小心和谨慎。- 打开文件,其中将在
data
函数中写入main()
,并将其发送到函数writeToFile()
。- 处理
strings
时应格外小心。- 值得在分配内存或打开文件时检查错误。因此,每次在程序中执行任何上述操作时,最好检查一下在操作过程中发生的任何错误。
您为什么不尝试此程序:
#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("\n\n");
// 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: %s\nAge of Pet: %d\n", head->name, head->age);
printPetRecord(head->next);
}
else
printf("Name of Pet: %s\nAge of Pet: %d\n", head->name, head->age);
}
// function to print list to file
void writeToFile(FILE *fptr, Node *head)
{
if(head->next != NULL)
{
fprintf(fptr, "\nPet Name: %s\nAge: %d\n\n", head->name, head->age);
writeToFile(fptr, head->next);
}
else
fprintf(fptr, "\nPet Name: %s\nAge: %d\n\n", 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