2010年9月10日星期五

从磁盘文件读出数据写入链表,操作后将链表数据导出到磁盘文件

/**************************************************************************************
*
*功能:初始化链表,导入信息,删除节点,遍历节点,导出信息
*创建日期:2010-09-09
*创建人:willard_moogle
*版本:V1.00
*备注:读出磁盘文件,写入链表,删除节点,写入磁盘文件
*
*
*
*************************************************************************************/
#include
#include
#include

#ifdef d
#define DEBUG(format,...) printf(format,##__VA_ARGS__)
#else
#define DEBUG(format,...) do{}while(0)
#endif

typedef struct student
{
char name[20];
int chinese; /*语文成绩*/
int math; /*数学成绩*/
struct student *next;
}STU;
/*************************************************************************************
*
*功能:链表初始化
*
************************************************************************************/
void init(STU** pHead)
{
if(NULL!=*pHead)
{
DEBUG("In init,pHead is not NULL\n");
exit(-1);
}
*pHead=(STU *)malloc(sizeof(STU));
if (NULL==*pHead) {
DEBUG("In init error!\n");
}
memset(*pHead,0,sizeof(STU));
DEBUG("In init OK\n");
}
/*******************************************************************************
*
*功能:遍历打印
*
* ****************************************************************************/
void displayNode(STU* pHead)
{
if(pHead==NULL)
{
DEBUG("In displayNode,the pHead is NULL!\n");
exit(-1);
}
if(pHead->next==NULL)
{
DEBUG("The pHead is empty!\n");
exit(-1);
}
while(pHead->next!=NULL) {
printf("name:%-20schinese:%-5dmath:%-5d\n",pHead->next->name,pHead->next->chinese,pHead->next->math);
pHead=pHead->next;
}
DEBUG("displayNode success!\n");
}


/********************************************************************************
*
*功能:从文件导出数据到链表
*传入参数:文件指针
*返回值:无
*
*
* *****************************************************************************/
void fileRead(STU* pHead)
{
STU *p1=NULL;
FILE* pfile=fopen("backup.txt","r+");

if (NULL==pfile) /*文件打开失败,返回空指针NULL*/
{
DEBUG("fopen error!\n");
exit(-1);
}
if (NULL==pHead) /*链表未初始化*/
{
DEBUG("In fileRead,pHead is NULL!\n");
exit(-1);
}
while(NULL!=pHead->next) /*pHead->next指向链表末尾,在尾部导入磁盘文件数据*/
{
pHead=pHead->next;
}
pHead->next=(STU*)malloc(sizeof(STU)); /*申请一块空间*/
if (NULL==pHead)
{
DEBUG("In fileRead,malloc error!\n");
exit(-1);
}
pHead=pHead->next; /*pHead指向新分配的空间*/
while(fread(pHead,sizeof(STU),1,pfile)) /*从pfile指向的文件读出一个结构体长度,存到pHead开始的空间*/
{
pHead->next=(STU*)malloc(sizeof(STU)); /*申请一块空间,备下次存储数据*/
if (NULL==pHead->next) /*内存空间分配失败*/
{
DEBUG("In fileRead,malloc error!\n");
exit(-1);
}
p1=pHead;
pHead=pHead->next; /*pHead后移*/
pHead->next=NULL;
} /*退出时,多分配一块空间*/
fclose(pfile); /*文件使用完毕,关闭*/
free(pHead); /*释放多分配的内存空间*/
p1->next=NULL; /*链表末尾节点指向NULL*/
}

/********************************************************************************
*
*功能:将链表数据写入到磁盘文件
*传入参数:链表首指针
*返回值:无
*
*
* *****************************************************************************/
void fileWrite(STU* pHead)
{
FILE* pfile=fopen("backup.txt","w"); /*定义并初始化文件类型指针*/

if (NULL==pfile) /*打开文件失败*/
{
DEBUG("open the file \"backup.txt\" error!");
exit(-1);
}

if (NULL==pHead) /*链表未初始化*/
{
DEBUG("in fileWrite,pHead is NULL!\n");
exit(-1);
}
if (NULL==pHead->next) /*链表为空*/
{
DEBUG("the list is empty!\n");
return ;
}
while(NULL!=pHead->next) /*遍历节点*/
{
fwrite(pHead->next,1,sizeof(STU),pfile); /*将节点信息写入磁盘文件*/
DEBUG("finished one node!\n");
pHead=pHead->next; /*pHead后移*/
}
fclose(pfile); /"文件使用完毕,关闭"/

}

/**************************************************************************************
*
*功能:链表初始化,将磁盘文件数据写入链表,查找节点,删除节点,链表数据写入磁盘文件
*编写人:willard_moogle
*日期:2010-09-10
*版本:v1.00
*
* ***********************************************************************************/
int main(int argc, char *argv[])
{
STU *stud=NULL;
STU *stud1=NULL;
char str[20]={};

init(&stud); /*链表初始化*/
fileRead(stud); /*将磁盘文件数据写入链表*/
displayNode(stud); /*遍历显示节点*/

printf("Want to find which one?\n"); /*查找节点*/
scanf("%s",str);
stud1=findNode(stud,str);
if (stud1==NULL)
{
printf("In list,no this name,please check out.\n");
}
else
{
printf("Lucy's score:chinese:%-5dmath:%-5d\n",stud1->chinese,stud1->math);
}

printf("Want to del who?\n"); /*删除节点*/
scanf("%s",str);
delNode(stud,str);
displayNode(stud);

printf("And now,program will export the list into backup.txt,please wait...\n");
fileWrite(stud); /*将链表数据写入到磁盘文件*/
printf("export success!\n");

return 0;
}

1 条评论: