我有一個代碼,將位於另一個雙向鏈表中的雙向鏈表中的學生編號(stdnum)保存到文件中。我注意到有時會打印「(null)」和多餘的空格。我如何避免這些?這裏是我的代碼:保存到c中的文件
typedef struct frn{ //structure for friend
char stdnum[20];
struct frn *next;
struct frn *prev;
}friend;
typedef struct std{ //structure for student
char stdnum[20];
char name[20];
char course[10];
struct frn *friendh;
struct frn *friendt;
struct std *next;
struct std *prev;
}student;
FILE *fp1;
student *y = h->next;
friend *y1;
fp1 = fopen("friends.txt", "w");
if(y != t){
while(y != t){
y1 = y->friendh;
while(y1 != NULL){
fprintf(fp1, "%s\n", y1->prev->stdnum);
y1 = y1->next;
}
y = y->next;
}
}
fclose(fp1);
取決於你有這些鏈表中得到了什麼。 – sukhvir
鏈表是好的,我有其他看到列表的功能,它只有有效的字符在裏面。哦,還有,每個列表都有一個虛擬節點。 :) –
對於鏈表的某些部分,「stdnum」可能是空的嗎? – sukhvir