使用链表打印文本文件,打印格式有问题
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;
文本内容为:
输出内容为:
希望输出内容能按照文本格式来输出