George_hacker 2022-05-27 14:34 采纳率: 50%
浏览 31
已结题

磁盘文件读出出现乱码

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

这是打印到磁盘文件的代码:
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

我想要达到的结果
正常从磁盘文件读出

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-05-27 16:28
    关注

    2022.5.30 ,今天重新做了修改完善,后附运行的截图,供参考:

    #include <string.h>
    #include <stdio.h>
    #include <iostream>
    #define OK 1
    #define error 0
    using namespace std;
    typedef int Status;
    typedef struct Person
    {
    char name[10];
    char age[10];
    char num[15];
    char sex[4];
    char place[20];
    }Person;
    
    typedef struct lnode {
        Person data;
        struct lnode* next;
    }LNode, * LinkList, Link;
    
    Status Writelist(LinkList L)
    {
        if (L->next == NULL){
            cout << "错误,无法存储数据!" << endl;
            return error;
        }
        FILE* fp;
        errno_t err;
        LinkList p = L->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 年龄:%s 学号:%s 性别:%s 籍贯:%s\n",
                p->data.name, p->data.age, p->data.num, p->data.sex, p->data.place);
            p = p->next;
        }
        cout << "数据存储成功!" << endl;
        fclose(fp);
        return OK;
    }//存储数据
    
    //这是从磁盘文件读出并打印到调试窗口的代码
    Status Readlist(LinkList* head)
    {
        FILE* fp;
        errno_t err;
        if ((err = fopen_s(&fp, "test", "r")) != 0){
            printf("error,cannot open this file\n");
            return error;
            exit(1);
        }
        if (!feof(fp)){
            char buff[1003] = { 0 };
            LinkList phead = NULL, p = NULL;
            phead = (*head) = (LinkList)malloc(sizeof(LNode));
            (*head)->next = NULL;
            while(1) //(!feof(fp))
            {
                memset(buff, 0, 1003);
                if (fgets(buff, 1003, fp) == NULL) break;
                p = (LinkList)malloc(sizeof(LNode));
                if (p == NULL){
                    printf("No memory!");
                    exit(0);
                }
                p->next = NULL; 
                int ret = sscanf(buff, "%*[^:]:%s %*[^:]:%s %*[^:]:%s %*[^:]:%s %*[^:]:%s", 
                             p->data.name, p->data.age, p->data.num, p->data.sex, p->data.place);
                if (ret == 5) {
                    phead->next = p;
                    phead = p;
    
                    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;
                }
                else
                    free(p);
            }
            fclose(fp);
            cout << "历史数据读取成功!" << endl;
            return OK;
        }
        else
        {
            fclose(fp);
            cout << "无历史数据,读取失败!" << endl;
            return error;
        }
    }//从文件读取数据
    
    int main()
    {
        int i, n = 3;
        Link* L = (LinkList)malloc(sizeof(LNode)), * q = L;
        L->next = NULL;
        for (i = 0; i < n; i++) {    //输入3个学生的信息
            LinkList p = (LinkList)malloc(sizeof(LNode));
            p->next = NULL;
            printf("姓名:");
            scanf("%s", p->data.name);
            printf("年龄:");
            scanf("%s", p->data.age);
            printf("学号:");
            scanf("%s", p->data.num);
            printf("性别:");
            scanf("%s", p->data.sex);
            printf("籍贯:");
            scanf("%s", p->data.place);
            q->next = p;
            q = p;
        }
        Writelist(L);  //写入文件
        q = NULL;
        Readlist(&q);  //文件读出到链表
        while (q->next) { //主函数显示读取的文件内容到链表
            printf("%s %s %s %s %s\n", q->next->data.name, q->next->data.num, 
                    q->next->data.age, q->next->data.sex, q->next->data.place);
            q = q->next;
        }
        return 0;
    }
    
    

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月28日
  • 已采纳回答 6月20日
  • 创建了问题 5月27日

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。