user-0 提问: 求助，链表输出乱码

回复:
user: user-0
content: 引用 2 楼 cfjtaishan 的回复:#include <stdio.h> #include <stdlib.h> #include <string.h>  struct book //链表结构声明 {     char name[32];     char phone[12];     char address[32];     struct book *next; }; typedef struct book BOOK;  BOOK *create() //创建链表并将文件内的内容赋给链表 {      FILE *fp;     BOOK *head, *ptr, *p; //链表的节点     head = (BOOK *)malloc(sizeof(BOOK)); //分配内存     if (!head)     {         printf("内存分配失败\n");         exit(1);     }     char nh[100],deli[]="\\";     char *buf = NULL;     if ((fp=fopen("date.dat","r"))==NULL)     {         exit(1);     }     //初始化head     strcpy(head->name, "姓名");     strcpy(head->phone, "电话");     strcpy(head->address, "地址");     head->next = NULL;     ptr = head;     while (!feof(fp)) //将后面行的字符串分割赋给链表     {         p = (BOOK *)malloc(sizeof(BOOK));         if (!p)         {             exit(1);         }         fgets(nh, 100, fp);         buf = strtok(nh, deli);         if (!buf)             continue;         strcpy(p->name,  buf);         buf = strtok(NULL, deli);         if (!buf)             continue;         strcpy(p->phone, buf);         buf = strtok(NULL, deli);         if (!buf)             continue;         strcpy(p->address, buf);         p->next = ptr->next;         ptr->next = p;         ptr = ptr->next;     }     return head; }  void PrintNode(BOOK *ptr) //链表输出 {     while (ptr) //链表遍历循环     {         printf("%s\t%s\t%s\n",ptr->name,ptr->phone,ptr->address); //输出节点数据         ptr = ptr->next; //指向下一个节点     }     printf("\n"); }  int main() {     BOOK *head;     head = create();     PrintNode(head);     system("pause"); }非常感谢，原来问题是字符串赋给链表的时候出问题了

user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  struct book //链表结构声明 {     char name[32];     char phone[12];     char address[32];     struct book *next; }; typedef struct book BOOK;  BOOK *create() //创建链表并将文件内的内容赋给链表 {      FILE *fp;     BOOK *head, *ptr, *p; //链表的节点     head = (BOOK *)malloc(sizeof(BOOK)); //分配内存     if (!head)     {         printf("内存分配失败\n");         exit(1);     }     char nh[100],deli[]="\\";     char *buf = NULL;     if ((fp=fopen("date.dat","r"))==NULL)     {         exit(1);     }     //初始化head     strcpy(head->name, "姓名");     strcpy(head->phone, "电话");     strcpy(head->address, "地址");     head->next = NULL;     ptr = head;     while (!feof(fp)) //将后面行的字符串分割赋给链表     {         p = (BOOK *)malloc(sizeof(BOOK));         if (!p)         {             exit(1);         }         fgets(nh, 100, fp);         buf = strtok(nh, deli);         if (!buf)             continue;         strcpy(p->name,  buf);         buf = strtok(NULL, deli);         if (!buf)             continue;         strcpy(p->phone, buf);         buf = strtok(NULL, deli);         if (!buf)             continue;         strcpy(p->address, buf);         p->next = ptr->next;         ptr->next = p;         ptr = ptr->next;     }     return head; }  void PrintNode(BOOK *ptr) //链表输出 {     while (ptr) //链表遍历循环     {         printf("%s\t%s\t%s\n",ptr->name,ptr->phone,ptr->address); //输出节点数据         ptr = ptr->next; //指向下一个节点     }     printf("\n"); }  int main() {     BOOK *head;     head = create();     PrintNode(head);     system("pause"); }

user: user-0
content: 岐岐\12345678901\岐岐家	 叽叽\12345678901\叽叽家	 溜溜\12345678901\溜溜家 这是文件里的内容   用 while (!feof(fp)) 	{ 		fgets(ch, 100, fp); 		se=strtok(ch, de); 		printf("%s\n", se); 		se = strtok(NULL, de); 		printf("%s\n", se); 		se = strtok(NULL, de); 		printf("%s\n", se); 	} 输出又没问题