C语言尾插法,为什么我输入‘王一,132’,却输出一堆乱码,求大神解答
#include <stdio.h>#include <stdlib.h>
typedef struct Node
{
char n[10];
char t[12];
struct Node* next;
}Node, *Linklist;
void InitLinklist(Linklist* L) //初始化单链表,建立空的带头结点的链表
{
*L = (Node*)malloc(sizeof(Node));
(*L)->next = NULL;
}
void CreateLinklist(Linklist L) //尾插法建立单链表
{
Node *r, *s;
r = L;
char in[10];
char it[12];
printf("请输入顾客姓名,电话号码:\n");
scanf("%s",in);
scanf("%s",it);
while(in[0]!='0') //当成绩为负时,结束输入
{
s = (Node*)malloc(sizeof(Node));
s->n[0]= in[0];
s->t[0] = it[0];
r->next = s;
r =s;
printf("请输入顾客姓名,电话号码:\n");
scanf("%s",in);
scanf("%s",it);
}
r->next = NULL; //将最后一个节点的指针域置为空
}
int WriteLinklistToFile(const char* strFile, Linklist L)
{
FILE *fpFile;
Node *head = L->next;
if(NULL == (fpFile = fopen(strFile,"a"))) //以写的方式打开
{
printf("Open file failed\n");
return 0;
}
while(NULL != head)
{
fprintf(fpFile,"%s\t%s\n",head->n,head->t);
head = head->next;
}
if(NULL != fpFile)
fclose(fpFile);
return 1;
};
int ReadFromFile(const char* strFile)
{
FILE *fpFile;
if(NULL == (fpFile = fopen(strFile,"r"))) //以读的方式打开
{
printf("Open file failed\n");
return 0;
}
printf("The contents of File are:\n");
while(!feof(fpFile))
putchar(fgetc(fpFile));//证明fprintf()输出到文件中的绝对是字符串
if(NULL != fpFile)
fclose(fpFile);
return 1;
}
void Destroy(Linklist L)
{
Node *head =L;
while (head)
{
Node* temp = head;
head = head->next;
free(temp);
}
}
int main()
{
char* strName = "曾经顾客信息.txt";
Linklist L;
InitLinklist(&L);
CreateLinklist(L);
WriteLinklistToFile(strName, L);
ReadFromFile(strName);
Destroy(L);
return 0;
}