求大佬指点错误,文件输出空白

weixin_46606896 2020-07-11 07:38:44
//头文件
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<Windows.h>
//extern list_single * list = createList();
struct student
{
char name[20];
char sex[2];
char num[20];
float math;
};
struct list_node
{
struct student data;
struct list_node *next ;
};

typedef struct list_node list_single ;
//创建链表
list_single * createList()
{
list_single * head_Node = (list_single *)malloc(sizeof(list_single));
head_Node->next = NULL;
return head_Node;
}

//创建新结点
list_single *createNode(struct student data)
{
list_single * new_Node = (list_single *)malloc(sizeof(list_single));
new_Node->data = data;
new_Node->next =NULL;
return new_Node;
}
//遍历链表
void print_list(list_single * head_Node)
{
list_single * p_Move = head_Node->next;
printf("num\tname\tsex\tmath\n");
while(p_Move)
{
printf("%s\t%s\t%s\t%.2f\n",p_Move->data.num,p_Move->data.name,p_Move->data.sex,p_Move->data.math);
p_Move = p_Move->next;
}
printf("\n");
}
//插入新结点,头插法
void insert_Node_by_Head(list_single * head_Node,struct student data)
{
list_single * new_Node = createNode(data);
new_Node->next = head_Node->next;
head_Node->next = new_Node;
}
//结点的指定删除
void delete_Node_by_Appion(list_single * head_Node,char* num)
{
list_single * pos_Node = head_Node->next;
list_single * pos_Front = head_Node;
if(pos_Node == NULL)
{
printf("没有找到相关信息,删除失败");
}
else
{
while(strcmp(pos_Node->data.num,num))
{
pos_Front = pos_Node;
pos_Node = pos_Front->next;
if(pos_Node == NULL)
{
printf("未找到相关信息");
return;
}

}
pos_Front->next = pos_Node->next;
free(pos_Node);
}
}
//查找信息
list_single * search_Information(list_single * head_Node,char* num)
{
list_single * p_Move = head_Node->next;
if(p_Move == NULL)
return NULL;
while(strcmp(p_Move->data.num,num))
p_Move = p_Move->next;
return p_Move;
}
//读取文件
void Read_File(list_single * head_Node,char * File_Name)
{
FILE *fp;
struct student data;
fp = fopen("File_Name","r");
if(fp == NULL)
{
printf("error:打开文件失败");
exit(0);
}
while(fscanf(fp,"%s\t%s\t%s\t%f\n",&data.num,&data.name,&data.sex,&data.math) != EOF)
{
insert_Node_by_Head(head_Node,data);//结点插入链表
}
fclose(fp);
}


//文件写
void Write_File(list_single * head_Node,char * File_Name)
{
FILE *fp;
fp = fopen(File_Name,"w");
list_single * p_Move = head_Node->next;
while(p_Move)
{
fprintf(fp,"%s\t%s\t%s\t%f\n",p_Move->data.num,p_Move->data.name,p_Move->data.sex,p_Move->data.math);
p_Move = p_Move->next;
}
fclose(fp);
}
//源#include"single _list.h"
void Menu();
void Uesr_Key();
void Add_Information();
void Printf_Information();
void Deletet_Information();
list_single * list = createList();
void main()
{
Read_File(list,"1.txt");//读取文件
print_list(list);//打印链表
/*while(1)
{
Menu();
Uesr_Key();
}*/

}















文件创建没有问题,内容写入文件也成功,但是输出文件的时候却全是空白。求大佬指点,题主c语言小白,感谢大佬帮助
...全文
150 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chxchxkkk 2020-07-12
  • 打赏
  • 举报
回复

void Read_File(list_single * head_Node,char * File_Name)
{
    FILE *fp;
    struct student data;

    //错误在这儿,File_Name加了双引号
   // fp = fopen("File_Name","r");

    fp = fopen(File_Name,"r");
    if(fp ==  NULL)
    {
        printf("error:打开文件失败");
        exit(0);
    }
    while(fscanf(fp,"%s\t%s\t%s\t%f\n",&data.num,&data.name,&data.sex,&data.math) != EOF)
    {
        insert_Node_by_Head(head_Node,data);//结点插入链表
    }
    fclose(fp);
}
Simple-Soft 2020-07-11
  • 打赏
  • 举报
回复
读写文件参考这个 我用#CSDN#这个app发现了有技术含量的博客,小伙伴们求同去《C语言文件读写(1)-文本文件读操作》, 一起来围观吧 https://blog.csdn.net/zhanghaiyang9999/article/details/107032563?utm_source=app

69,337

社区成员

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

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