struct Node
{
int id;
char name[25];
int age;
int No;
int RoomNo;
struct Node* next;
};
void PrintList(struct Node *temp)
{
while(temp->next!=NULL)
{
printf("ID: %d \n", temp->id);
printf("Name: %s\n", temp->name);
printf("Age: %d\n", temp->age);
printf("No: %d\n", temp->No);
printf("Room No: %d\n", temp->RoomNo);
temp = temp->next;
printf("-----------\n");
}
printf("\n\n");
}
int main()
{
int i;
FILE *fptr;
fptr = fopen("test.txt", "r");
struct Node *List = (struct Node *) malloc(sizeof(struct Node *));
List->next = NULL;
struct Node *temp;
temp = List;
for(i=0;i<3;i++)
{
temp ->next = (struct Node *) malloc(sizeof(struct Node *));
fscanf(fptr, "%d;%[^;];%d;%d;%d", &temp ->id, temp ->name, &temp ->age, &temp ->No, &temp ->RoomNo);
temp = temp ->next;
temp ->next = NULL;
getc(fptr);
}
PrintList(List);
}
This is the content of txt file. I want to store each data in a linked list like "List->id = 5 List->name Carl" like that. When there is a new line program should create another node and store the second line into second node. It goes like that. But with the code above I can only store the first line.
5;Carl;24;22410392;322
12;Jack;30;22410394;400
6;Adam;42;22513392;295
ID: 5
Name: Carl
Age: 12
No: 1801675082
Room No: 256
-----------
ID: 12
Name: Jack
Age: 6
No: 1835099201
Room No: 256
-----------
ID: 6
Name: Adam
Age: 42
No: 22513392
Room No: 295
-----------
This is the output, only the id and name are correct for the first two. But the last one is correct for some reason.