文件的存放、读取、利用文件创建链表、链表排序以及将其写入文件

(笔记!)

功能:

void createFile(char *FName); //创建文件,存放结构体数据void readFile(char *FName); //从文件读出数据到结构体struct Jd *createchain(char *FName); //利用文件创建链表void printchain(struct Jd *head); //输出链表所有结点数据void sortchain(struct Jd *head); //根据地址对链表进行排序void writeToFile(struct Jd *head, char *FName); //把链表结点数据写往文件

主要代码如下

#include

#include

#include

struct STU

{

char name[20];

int num;

int age;

char addr[20];

};

struct Jd

{

struct STU student;

struct Jd *next;

};

void createFile(char *FName); //创建文件,存放结构体数据

void readFile(char *FName); //从文件读出数据到结构体

struct Jd *createchain(char *FName); //利用文件创建链表

void printchain(struct Jd *head); //输出链表所有结点数据

void sortchain(struct Jd *head); //根据地址对链表进行排序

void writeToFile(struct Jd *head, char *FName); //把链表结点数据写往文件

int main(void)

{

struct Jd *head = NULL;

char FileName[128] = {'\0'};

strcpy(FileName,"stu.dat");

createFile(FileName);

readFile(FileName);

head = createchain(FileName);

printf("* * * * * 未排序的链表数据 * * * * * \n");

printchain(head);

sortchain(head);

printf("* * * * * 根据地址排序后的链表数据 * * * * * \n");

printchain(head);

strcpy(FileName, "paixu.dat");

writeToFile(head,FileName);

readFile(FileName);

return 0;

}

void createFile(char *FName)

{

int i = 1;

struct STU stud;

FILE *fp;

if ( (fp = fopen(FName, "wb")) == NULL ) //利用FName的字符串作文件名

{

printf("cannot open file\n");

exit(0);

}

while ( i!=0 )

{

printf("enter data of student:\n");

printf("姓名:");

scanf("%s", stud.name);

printf("学号:");

scanf("%d", &stud.num);

printf("年龄:");

scanf("%d", &stud.age);

printf("地址:");

scanf("%s", stud.addr);

if ( fwrite(&stud,sizeof(struct STU),1,fp) != 1 ) //把一个结构体数据写到文件中,并判断是否真实写入

{

printf("file write error\n");

}

printf("还需要输入新的学生数据(1=继续,0=退出)");

scanf("%d", &i);

}

fclose(fp);

}

void readFile(char *FName)

{

struct STU stud;

FILE *fp;

if ( (fp = fopen(FName, "rb")) == NULL )

exit(0);

printf("* * * * * %s 文件中的记录数据 * * * * * \n", FName);

//从文件里读出一个结构体数据,知道读不出一个完整的结构体数据

while ( fread(&stud,sizeof(struct STU),1,fp) == 1 )

{

printf("%-20s %4d %4d %-20s\n", stud.name, stud.num, stud.age, stud.addr);

}

fclose(fp);

}

struct Jd *createchain(char *FName)

{

FILE *fp;

struct Jd *pt, *pEnd, *head = NULL;

struct STU stud;

if ( (fp=fopen(FName,"rb")) == NULL )

{

exit(0);

}

while ( fread(&stud, sizeof(struct STU), 1, fp) == 1 ) //从文件里读出一个结构体数据

{

pt = (struct Jd *)malloc(sizeof(struct Jd)); //创建链表结点空间

pt->student = stud; //把前面读出的结构体数据赋值给结点的student成员

if ( head == NULL )

{

head = pEnd = pt;

pt->next = NULL;

}

else //在尾部插入结点

{

pEnd->next = pt; //将新结点链入链表

pt->next = NULL; //将新结点的next指针置空,标识成尾结点

pEnd = pt; //让pEnd指向尾结点,为下一次插入做准备

}

}

fclose(fp);

return head;

}

void printchain(struct Jd *head)

{

struct STU stud;

while ( head != NULL ) //从头结点开始输出,直到链表最后的尾结点

{

stud = head->student;

printf("%-20s %4d %4d %-20s\n", stud.name, stud.num, stud.age, stud.addr);

head = head->next;

}

}

void sortchain(struct Jd *head) //根据addr的值从小到大的排序

{

struct Jd *pt1, *pt2, *pt;

struct STU temp;

for ( pt1 = head; pt1->next != NULL; pt1 = pt1->next )

{

pt = pt1;

for ( pt2 = pt1->next; pt2 != NULL; pt2 = pt2->next )

{

if ( strcmp(pt->student.addr,pt2->student.addr) > 0 )

pt = pt2;

}

if ( pt != pt1 )

{

temp = pt1->student;

pt1->student = pt->student;

pt->student = temp;

}

}

}

void writeToFile(struct Jd *head, char *FName)

{

FILE *fp;

struct Jd *pt;

if ( (fp = fopen(FName,"wb")) == NULL)

{

exit(0);

}

pt = head;

//依次把链表结点的学生信息写往文件中,直到输出链表最后一个结点

while ( pt != NULL )

{

fwrite(&(pt->student), sizeof(struct STU), 1, fp); //只将链表中学生信息写入

pt = pt->next;

}

fclose(fp);

}

精彩内容

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。