C语言尾插法,为什么我输入'"王一,123,却输出乱码,文件里也是乱码

拉倒风的昵称 2017-03-16 12:14:01
#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;
}
...全文
189 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-03-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define     NAME_LEN   10
#define     TEL_LEN    12

typedef struct Node
{
    char n[NAME_LEN];
    char t[TEL_LEN];
    struct Node* next;
}Node, *Linklist;

void InitLinklist(Linklist* L)
{
    *L = (Linklist)malloc(sizeof(Node));
    (*L)->next = NULL;
}

void CreateLinklist(Linklist L)
{
    Node *r, *s;
    char in[10];
    char it[12];

    r = L;
    printf("请输入顾客姓名,电话号码:\n");
    scanf("%s",in);
    scanf("%s",it);
    while(in[0]!='0') {
        s = (Linklist)malloc(sizeof(Node));
        strncpy(s->n, in, NAME_LEN);
        strncpy(s->t, it, TEL_LEN);
        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;
    Linklist head = L->next;

    if(!(fpFile = fopen(strFile,"a"))) {
        printf("Open file failed\n");
        return 0;
    }
    while(head) {
        fprintf(fpFile,"%s\t%s\n",head->n, head->t);
        head = head->next;
    }
    fclose(fpFile);

    return 1;
};
int ReadFromFile(const char* strFile)
{
    FILE *fpFile;
    if (!(fpFile = fopen(strFile,"r")))
    {
        printf("Open file failed\n");
        return 0;
    }
    printf("The contents of File are:\n");
    while (!feof(fpFile))
        putchar(fgetc(fpFile));

    fclose(fpFile);
    return 1;
}
void Destroy(Linklist L)
{
    Linklist p = L, tmp;
    while (p) {
        tmp = p->next;
        free(p);
        p = tmp;
    }
}
int main()
{
    char* strName = "曾经顾客信息.txt";
    Linklist L;

    InitLinklist(&L);
    CreateLinklist(L);
    WriteLinklistToFile(strName, L);
    ReadFromFile(strName);
    Destroy(L);

    return 0;
}
拉倒风的昵称 2017-03-16
  • 打赏
  • 举报
回复
好感谢,终于解决了
幻夢之葉 2017-03-16
  • 打赏
  • 举报
回复

//s->n[0] = in[0]; //指向了临时变量,退出后会被销毁,你n的指向将会的内容是不可预测的
//s->t[0] = it[0];
strcpy(s->n, in); //char* 请使用strcpy函数进行深拷贝
strcpy(s->t, it);

70,011

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧