#include<stdio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct trial{
char *data;
int save;
struct trial *next;
};
struct trial *head = NULL;
int main (){
int x, ctr, y;
char filestr[500];
char *data, *save, *filestr2;
FILE *fp;
fp = fopen("Untitled1.txt", "r");
printf("Count: ");
scanf("%d", &x);
while(x > 0){
if(fgets(filestr, sizeof(filestr), fp) != NULL){
data = strtok(filestr, " ");
filestr2 = strtok(NULL, "");
save = strtok(filestr2, "");
printf("%s, %s", data, save);
struct trial *link = (struct trial*) malloc(sizeof(struct trial));
link->data = data;
link->save = atoi(save);
link->next = head;
head = link;
}
x--;
}
printf("\n");
struct trial *ptr = head;
ctr = 0;
while(ptr != NULL){
printf("Data %d: %s, %d\n", ctr + 1, ptr->data, ptr->save);
ptr = ptr->next;
ctr++;
}
return 0;
}
/*Untitled1.txt is as follows
dragon 12
shadow 19
spirit 6
wiser 4
civil 8
fairy 7
*/
now here goes the problem, when x = 3; it should be:
Count: 3
dragon, 12
shadow, 19
spirit, 6
Data 1: spirit, 6
Data 2: shadow, 19
Data 3: dragon, 12
But here is what happens.
Data 1: spirit, 6
Data 2: spirit, 19
Data 3: spirit, 12
why is that the save variable is moving and the *data is not? What should I add and where to place? Thanks for the help. [sorry for the unnecessary variables, it is a part of a whole]