//頭檔案
#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語言小白,感謝大佬幫助
uj5u.com熱心網友回復:
讀寫檔案參考這個我用#CSDN#這個app發現了有技術含量的博客,小伙伴們求同去《C語言檔案讀寫(1)-文本檔案讀操作》, 一起來圍觀吧 https://blog.csdn.net/zhanghaiyang9999/article/details/107032563?utm_source=app
uj5u.com熱心網友回復:
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/29334.html
標籤:C語言
上一篇:新人求助帖
下一篇:CLion如何輸出到外部控制臺?
