PH_modest 2023-06-01 12:43 采纳率: 100%
浏览 59
已结题

将文件中的数据录入到链表中

代码可以正常运行,但是运行之后再打印链表你的数据的时候显示无数据,我感觉是插入的时候失败了,但不知道问题在哪里

img

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2023-06-01 16:44
    关注

    这个函数这样写,供参考:

    void Input(SLTNode** pphead)
    {
        SLTNode* ptra = NULL, * newnode = NULL;
        FILE* fp;
        fp = fopen("D:\\桌面\\student.txt", "r");
        if (fp == NULL) {
            printf("无法打开文件!\n");
            return;
        }
        if ((*pphead))
            for (ptra = (*pphead); ptra->next; ptra = ptra->next);
        while (1) {
            newnode = (SLTNode*)malloc(sizeof(SLTNode));
            newnode->next = NULL;
            if (fscanf(fp, "%lld %s %s %d %d %d %lf %lf %lf", &newnode->student.num, newnode->student.name, newnode->student.sex,
                &newnode->student, birthday[0], &newnode->student.birthday[1], &newnode->student.birthday[2],
                &newnode->student.score[0], &newnode->student.score[1], &newnode->student.score[2]) != 9) {
                free(newnode);
                break;
            }
            if (!(*pphead))
                (*pphead) = newnode;
            else
                ptra->next = newnode;
            ptra = newnode;
        }
        fclose(fp);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月16日
  • 已采纳回答 6月8日
  • 创建了问题 6月1日