I have this section of code. Basically it uses fgets to read from a file, and at the end it does this:
Code:
new->name=name;
a[currentarea].items=new;
printf("%d:%s", currentarea, a[currentarea].items->name);
When I do the test print seen above, everything is fine, each a[currentarea].items->name prints just fine. However, once I leave the while loop and try to print name, it gives me the same name for every a[currentarea], despite the fact that a[currentarea].items is a different pointer, which means its overwriting new->name with whatever the last fgets had. I just don't understand why or how.
Code:
while (1){
if (fgets(input, 255, file) == NULL){return;} if ((p = strchr(input, '\n')) != NULL) *p = '\0';
currentarea=atoi(input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
strcpy(a[currentarea].name, input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
strcpy(a[currentarea].desc, input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
a[currentarea].n=atoi(input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
a[currentarea].s=atoi(input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
a[currentarea].w=atoi(input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
a[currentarea].e=atoi(input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
a[currentarea].u=atoi(input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
a[currentarea].d=atoi(input);
fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
new=createitem("NULL", "NULL", currentarea);
name=strtok(input,",");
printf("%s", name);
desc=strtok(NULL, " ");
new->name=name;
new->desc=desc;
new->loc=&a[currentarea];
a[currentarea].items=new;
printf("%d:%s", currentarea, a[currentarea].items->name);
if (fgets(input, 255, file) == NULL){return;}
}