George_hacker 2022-05-27 14:10 采纳率: 50%
浏览 39

从文本文件读出显示到调试窗口出现乱码

文件读出出现乱码,请各位帮我看看怎么回事

这是打印到磁盘文件的代码:
Status Writelist(Link L)
{
if (L.head->next == NULL)
{
cout << "错误,无法存储数据!" << endl;
return error;
}
FILE* fp;
errno_t err;
LinkList p = L.head->next;
if ((err = fopen_s(&fp, "test", "w")) != 0)
{
printf("error,cannot open this file\n");
return error;
exit(1);
}
while (p != NULL)
{
fprintf(fp, "姓名:%s", p->data.name);
fprintf(fp, " 年龄:%s", p->data.age);
fprintf(fp, " 学号:%s", p->data.num);
fprintf(fp, " 性别:%s", p->data.sex);
fprintf(fp, " 籍贯:%s\n", p->data.place);
p = p->next;
}
cout << "数据存储成功!" << endl;
fclose(fp);
return OK;
}//存储数据

这是从磁盘文件读出并打印到调试窗口的代码
Status Readlist()
{
LinkList p;
p = new LNode[1];
FILE* fp;
errno_t err;
if ((err = fopen_s(&fp, "test", "r")) != 0)
{
printf("error,cannot open this file\n");
return error;
exit(1);
}
rewind(fp);
if (!feof(fp))
{
while (!feof(fp))
{
/LinkList current = (LinkList)malloc(sizeof(LNode));
if (current == NULL)
{
printf("No memory!");
exit(0);
}
current->next = NULL;
/
fscanf_s(fp, "%s(还有4个%s未写出,因为审核过不了,只能这样)", p->data.name,10, p->data.age,10 ,p->data.num,15, p->data.sex,10, p->data.place,20);/fread(&(current->data), sizeof(Person), 1, fp);/
fscanf_s(fp, "\n");
cout << p->data.name << endl;

        cout << p->data.age << endl;

        cout << p->data.num << endl;

        cout << p->data.sex << endl;

        cout << p->data.place << endl;
        /*if (feof(fp))
        {
            free(current);
            break;
        }*/
    }
    fclose(fp);
    cout << "历史数据读取成功!" << endl;
    return OK;
}
else
{
    cout << "无历史数据,读取失败!" << endl;
    return error;
}    

}//从文件读取数据

img

img

img

我想要达到的结果

正常从磁盘文件读出

  • 写回答

2条回答 默认 最新

  • 赵4老师 2022-05-27 14:23
    关注

    fprintf(fp, "%s", p->data.name);
    fprintf(fp, " %s", p->data.age);
    fprintf(fp, " %s", p->data.num);
    fprintf(fp, " %s", p->data.sex);
    fprintf(fp, " %s\n", p->data.place);

    fscanf_s(fp, "%s%s%s%s%s", p->data.name,10, p->data.age,10 ,p->data.num,15, p->data.sex,10, p->data.place,20);

    内容中不要有空格

    先保存,再读取

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月27日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog