/***********************************************************
* 版权所有 (C)2016,test
*
* 文件名称:学生信息管理
* 文件标识:
* 内容摘要:模拟学生信息管理系统
* 其它说明:主函数
* 当前版本: V8.0
* 作 者:王曼
* 完成日期: 20161229
**********************************************************/
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
# define Suh sizeof(struct Student)
using namespace std;
struct Student {
char num[10]; //学号
char name[20]; //姓名
char sex[2]; //性别
int birth;
char ad[20];
char phone[12];
char email[12];
struct Student *next;
};
char fiSuhame[90]; //全局变量,用来保存要打开的文件名字
/*********************************************************
* 功能描述:生成链表
* 输入参数:学生信息
* 输出参数:无
* 返回值 :链表
* 其它说明:调用print_in输出需要输入的数据名称
************************************************************/
struct Student *Creat(int n)
{
void menu_print_in(void);
struct Student *head;
struct Student *p1, *p2;
system("cls");
for(int i=1;i<n+1;i++)
{
p1 = (struct Student*)malloc(Suh); //将返回的指针强制转换
menu_print_in();
cin>>p1->num>>p1->name>>p1->sex>>p1->birth>>p1->ad>>p1->phone>>p1->email;
p1->next = NULL;
if(i==1) //尾插
{
head = p2 = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
}
return(head);
}
/*学生信息存盘(wb只写)*/
void WriteData_wb(struct Student *head)
{
FILE *fp;
struct Student *p;
if((fp = fopen(fiSuhame, "wb"))==NULL)
printf("不能打开此文件!");
p = head;
while(p!=NULL)
{
if(fwrite(p,Suh,1,fp)!=1)
{
printf("写入学生信息出错\n");
fclose(fp);
return;
}
p=p->next;
}
fclose(fp);
}
/*学生信息存盘(ab追加)*/
void WriteData_ab(struct Student *head)
{
FILE *fp;
struct Student *p;
if((fp = fopen(fiSuhame, "ab"))==NULL)
printf("错误,不能读取文件");
p = head;
while(p!=NULL)
{
if(fwrite(p,Suh,1,fp)!=1)
{
printf("写入学生信息出错\n");
fclose(fp);
return;
}
p=p->next;
}
fclose(fp);
}
/*读取学生信息*/
/*读取学生信息文件保存到链表中 ,返回指向此链表头指针*/
struct Student *ReadData(void)
{
struct Student *head = NULL;
struct Student *p1, *p2;
FILE *fp;
if((fp=fopen(fiSuhame,"rb+"))==NULL)
{
printf("打开文件出错\n");
exit(0);
}
while(!feof(fp))
{
if((p1=(struct Student*)malloc(Suh))==NULL)
{
printf("内存申请出错\n");
fclose(fp);
exit(0);
}
if(fread(p1,Suh,1,fp)!=1)
{
free(p1);
break;
}
if(head==NULL)
head=p2=p1;
else
{
p2->next=p1;
p2=p1;
}
}
fclose(fp);
return (head);
}
/*********************************************************
* 功能描述:查询指定文件下所有学