长崎春华 2022-11-21 15:12 采纳率: 50%
浏览 1

C链表打印信息输出格式不符合预期

使用链表打印文本文件,打印格式有问题
struct student
{
    char name[20];
    int age;
    char sex[10];
    char tel[20];
};

struct Node
{
    struct student data;
    struct Node* next;   
}list;

struct Node* createlist()
{
    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->next = NULL;
    return head;
}

void printlist(struct Node* head)
{
    struct Node* pmove = head->next;
    while(pmove)
    {
        printf("%s %d %s %s",pmove->data.name,pmove->data.age,pmove->data.sex,pmove->data.tel);
        pmove = pmove->next;
    }
    printf("\n");
}

void insertNodeByHead(struct Node* head,struct student data)
{
    struct Node* insertNode = creatNode(data);
    insertNode->next = head->next;
    head->next = insertNode;
}

void readInfoToFile(struct Node* headNode,char *fileName)
{
    FILE *fp;
    struct student data;
    fp = fopen(fileName,"r");
    if(fp == NULL)
    {
        fp = fopen(fileName,"w+");
    }
    while(fscanf(fp,"%s %d %s %s",&data.name,&data.age,&data.sex,&data.tel) != EOF) // 格式化读取文件 
    {
        insertNodeByHead(headNode,data);
    }
    
    fclose(fp);
    
}

/*主函数*/
    struct Node* list = createlist();
    readInfoToFile(list,"tset.txt"); 
    printlist(list);
    return 0;
    
文本内容为:

img

输出内容为:

img

希望输出内容能按照文本格式来输出

  • 写回答

1条回答 默认 最新

  • 叶落花枯 2022-11-21 15:28
    关注

    fscanf读取文件的地方,name、sex、tel这三个数组不需要用 & 取地址,数组名本身就是地址。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月21日

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法