我做了一个程序,从文本文件中读取输入并创建一个链表,但做起来有困难。我还是一个初学者。问题是添加新项时头指针一直在移动。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <stdbool.h>
//Visitor information
struct visitData
{
char *SrNo;
char *ticketNo;
char *Name;
char *ID;
};
//Ticket linked list
struct ticket
{
struct visitData visitor;
struct ticket *nextPtr;
};
int main() { //main
FILE *fPtr = NULL;
fPtr = fopen("Tickets.txt", "a+");
if (fPtr == NULL)
{
printf("Error, please try again.\n");
return 0;
}
FILE *outfPtr = NULL;
outfPtr = fopen("output.txt", "w");
if (outfPtr == NULL)
{
puts("Error, could not create output file, please try again.");
return 0;
}
else {
struct ticket *head = NULL;
struct ticket *new = NULL;
struct visitData *nextPtr = NULL;
char sr[2];
char ticketNumber[20];
char name[30];
char id[24];
rewind(fPtr);
if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)
{
new = malloc(sizeof(struct ticket));
new->visitor.SrNo = strdup(sr);
new->visitor.ticketNo = strdup(ticketNumber);
new->visitor.Name = strdup(name);
new->visitor.ID = strdup(id);
new->nextPtr = NULL;
head = new;
printf("%s\n", head->visitor.SrNo);
while (!feof(fPtr))
{
if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)
new->visitor.SrNo = sr;
new->visitor.ticketNo = ticketNumber;
new->visitor.Name = name;
new->visitor.ID = id;
new->nextPtr = malloc(sizeof(struct ticket));
new = new->nextPtr;
new->nextPtr = NULL;
}
}
}
else
{
printf("%d",fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id));
new->visitor.SrNo = "0";
}
if (head == NULL)
{
printf("List is empty\n");
}
struct ticket *Ptr = NULL;
Ptr = head;
printf("%s\n", Ptr->visitor.SrNo);
while (Ptr != NULL)
{
printf("%s %s\n", Ptr->visitor.SrNo, Ptr->visitor.ticketNo);
Ptr = Ptr->nextPtr;
}
}
}
I did try to add a line which is `
new = malloc(sizeof(struct ticket));
`, but it became another problem, that the head pointer cannot link to the second node.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <stdbool.h>
//Visitor information
struct visitData
{
char *SrNo;
char *ticketNo;
char *Name;
char *ID;
};
//Ticket linked list
struct ticket
{
struct visitData visitor;
struct ticket *nextPtr;
};
int main() { //main
FILE *fPtr = NULL;
fPtr = fopen("Tickets.txt", "a+");
if (fPtr == NULL)
{
printf("Error, please try again.\n");
return 0;
}
FILE *outfPtr = NULL;
outfPtr = fopen("output.txt", "w");
if (outfPtr == NULL)
{
puts("Error, could not create output file, please try again.");
return 0;
}
else {
struct ticket *head = NULL;
struct ticket *new = NULL;
struct visitData *nextPtr = NULL;
char sr[2];
char ticketNumber[20];
char name[30];
char id[24];
rewind(fPtr);
if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)
{
new = malloc(sizeof(struct ticket));
new->visitor.SrNo = strdup(sr);
new->visitor.ticketNo = strdup(ticketNumber);
new->visitor.Name = strdup(name);
new->visitor.ID = strdup(id);
new->nextPtr = NULL;
head = new;
printf("%s\n", head->visitor.SrNo);
while (!feof(fPtr))
{
if (fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id) == 4)
{
new = malloc(sizeof(struct ticket));
new->visitor.SrNo = sr;
new->visitor.ticketNo = ticketNumber;
new->visitor.Name = name;
new->visitor.ID = id;
new->nextPtr = malloc(sizeof(struct ticket));
new = new->nextPtr;
new->nextPtr = NULL;
}
}
}
else
{
printf("%d",fscanf(fPtr, "%2s%20s%29s%24s", sr, ticketNumber, name, id));
new->visitor.SrNo = "0";
}
if (head == NULL)
{
printf("List is empty\n");
}
struct ticket *Ptr = NULL;
Ptr = head;
printf("%s\n", Ptr->visitor.SrNo);
while (Ptr != NULL)
{
printf("%s %s\n", Ptr->visitor.SrNo, Ptr->visitor.ticketNo);
Ptr = Ptr->nextPtr;
}
}
}
1条答案
按热度按时间bsxbgnwa1#
向列表添加节点的典型方法是:
您在保存旧值之前更改了头,因此丢失了列表。