After reading your comments, here is why its printing NULL:
fprintf(fp1, "%s\n", y1->prev->stdnum)
what happens when you are on the very first node of your linked list (y1)( first time you enter that inner while )? When you do y1->prev->stdnum you are accessing random memory Or if you have initialised your linked list to NULL values for empty values. That is what gets printed.
Then straight after printing that null
you do y1 = y1->next
. which takes you to the 2nd node of your linked list.
Now once again when you do :
fprintf(fp1, "%s\n", y1->prev->stdnum)
now you are printing the 'stdnum' value of the first node, which you mentioned in the comments is empty. So fprintf
prints out a empty space.
Can you verify that the null
and blank space
occur right next to each other?
You can fix it like this:
typedef struct frn{ //structure for friend
char stdnum[20];
struct frn *next = NULL;
struct frn *prev = NULL;
}friend;
fp1 = fopen("friends.txt", "w"); // I would highly recommend, you put an error check here to verify if the file opened or not
if(y != t){
while(y != t){
y1 = y->friendh;
while(y1 != NULL){
if(y1->prev==NULL){
y1 = y1->next;
}else{
fprintf(fp1, "%s\n", y1->prev->stdnum);
y1 = y1->next;
}
}
y = y->next;
}
}
fclose(fp1);