程序代码:
// FileManage.h: interface for the FileManage class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_FILEMANAGE_H__47DCD9E0_8915_48AA_9FC6_13DE755B619D__INCLUDED_)
#define AFX_FILEMANAGE_H__47DCD9E0_8915_48AA_9FC6_13DE755B619D__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
struct tagFile
{
int m_AllStuNumber; //学生总数量
int m_FileNumber; //文件使用数量
};
//保存学生信息
//参数:要保存的学生信息
//返回:0失败 1成功
int SaveStudents(tagStudents *Students);
//查询学生信息
//参数:szbuff:要查询的字符串
//参数:chMark:查询方式 1 按ID查询 2按姓名查询 3 全部查询
//返回值:返回查到的学生数量
int SelectStudentForFile(char *szbuff, char chMark);
//检查学号是否重复
//参数:要检查的内容
//重复返回2,没有重复返回1
int RepeatnNumber(char *szbuff);
//删除学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2成功删除
int DeleteStudents(char *szStuNumber);
//修改学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2已找到
int ChengeStudents(char *szStuNumber);
#endif // !defined(AFX_FILEMANAGE_H__47DCD9E0_8915_48AA_9FC6_13DE755B619D__INCLUDED_)
// FileManage.cpp: implementation of the FileManage class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FileManage.h"
#define SAFE_CLOSE(p) if (p) { \
if (fclose(p))\
{\
p = NULL;\
return 0;\
}\
p = NULL;}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//保存学生信息
//参数:要保存的学生信息
//返回:0失败 1成功
int SaveStudents(tagStudents *Students)
{
FILE *fpStudents = NULL;
int nRet = 0;
int nSize = FILE_SIZE;
char Temp[200] = {0};
tagFile tfTemp;
//先读取文件
fpStudents = fopen("Students.dat", "rb+");
//如果文件不存在,则新建
if (fpStudents == NULL)
{
fpStudents = fopen("Students.dat", "wb+");
if (fpStudents == NULL)
{
nRet = 0;
goto EXIT_PROC;
}
//新建一个5M的数据文件
while (nSize)
{
fputc(0, fpStudents);
nSize--;
}
//写入记录
tfTemp.m_AllStuNumber = 0;
tfTemp.m_FileNumber = 0;
if (fseek(fpStudents, 0, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
if (fwrite(&tfTemp, 4, 2, fpStudents) != 2)
{
nRet = 0;
goto EXIT_PROC;
}
}
//指向文件头
if (fseek(fpStudents, 0, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
//读取学生数量
if (fread(&tfTemp, 4, 2, fpStudents) != 2)
{
nRet = 0;
goto EXIT_PROC;
}
//判断文件有没有满,满了在追加
if ((FILE_SIZE-100) / 200 == tfTemp.m_AllStuNumber)
{
SAFE_CLOSE(fpStudents);
fpStudents = fopen("Students.dat", "ab+");
if (fpStudents == NULL)
{
nRet = 0;
goto EXIT_PROC;
}
nSize = FILE_SIZE;
while (nSize)
{
fputc(0, fpStudents);
nSize--;
}
SAFE_CLOSE(fpStudents);
//追加完毕后在重新以读方式打开
fpStudents = fopen("Students.dat", "rb+");
if (fpStudents == NULL)
{
nRet = 0;
goto EXIT_PROC;
}
}
//指向100字节处
if (fseek(fpStudents, 100, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
while (1)
{
//读取一个结构体数据,一个结构体给200字节空间
if (fread(Temp, 200, 1, fpStudents) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
//如果遇到空数据,就添加进空地
if (((tagStudents *)Temp)->m_Mark == 0)
{
//退回200字节处
if (fseek(fpStudents, -200, SEEK_CUR))
{
nRet = 0;
goto EXIT_PROC;
}
if (fwrite(Students, 200, 1, fpStudents) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
//添加成功后增加学生数量
tfTemp.m_AllStuNumber++;
//如果是在文件追加的数据 加1
if (fread(Temp, 200, 1, fpStudents) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
//if (((tagStudents *)Temp)->m_Mark == 0)
//{
tfTemp.m_FileNumber++;
//}
//指向文件头
if (fseek(fpStudents, 0, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
//写入数据
if (fwrite(&tfTemp, 4, 2, fpStudents) != 2)
{
nRet = 0;
goto EXIT_PROC;
}
break;
}
}
nRet = 1;
EXIT_PROC:
SAFE_CLOSE(fpStudents);
return nRet;
}
//查询学生信息
//参数:szbuff:要查询的字符串
//参数:chMark:查询方式 1 按ID查询 2按姓名查询 3 全部查询
//返回值:返回查到的学生数量
int SelectStudentForFile(char *szbuff, char chMark)
{
FILE *fpStudent = NULL;
char Temp[200] = {0};
int nRet = 0, i = 0;
tagFile tfTemp;
fpStudent = fopen("Students.dat", "rb");
if (fpStudent == NULL)
{
goto EXIT_PROC;
}
//读取学生数量
if (fread(&tfTemp, 4, 2, fpStudent) != 2)
{
goto EXIT_PROC;
}
//跳过保留的100字节
if (fseek(fpStudent, 100, SEEK_SET))
{
goto EXIT_PROC;
}
switch (chMark)
{
case SELECTID: //按ID查询
nRet = 0;
for (i = 0; i < tfTemp.m_FileNumber; i++)
{
//按每个200字节查询
if (fread(Temp, 200, 1, fpStudent) != 1)
{
goto EXIT_PROC;
}
if (((tagStudents *)Temp)->m_Mark == 0)
{
continue;
}
if (strcmp(((tagStudents *)Temp)->m_StuNumber, szbuff) == 0)
{
nRet++;
StuInterfaceShow(*((tagStudents *)Temp));
break;
}
}
break;
case SELECTNAME: //按姓名查询
nRet = 0;
for (i = 0; i < tfTemp.m_FileNumber; i++)
{
//按每个200字节查询
if (fread(Temp, 200, 1, fpStudent) != 1)
{
goto EXIT_PROC;
}
if (((tagStudents *)Temp)->m_Mark == 0)
{
continue;
}
if (strcmp(((tagStudents *)Temp)->m_StuName, szbuff) == 0)
{
nRet++;
StuInterfaceShow(*((tagStudents *)Temp));
}
}
break;
case SELECTALL: //查询全部
nRet = 0;
for (i = 0; i < tfTemp.m_FileNumber; i++)
{
//按每个200字节查询
if (fread(Temp, 200, 1, fpStudent) != 1)
{
goto EXIT_PROC;
}
if (((tagStudents *)Temp)->m_Mark == 0)
{
continue;
}
nRet++;
StuInterfaceShow(*((tagStudents *)Temp));
}
break;
}
EXIT_PROC:
SAFE_CLOSE(fpStudent);
return nRet;
}
//检查学号是否重复
//参数:要检查的内容
//重复返回2,没有重复返回1
int RepeatnNumber(char *szbuff)
{
FILE *fpStudent = NULL;
char Temp[200] = {0};
int nRet = 0, i = 0;
tagFile tfTemp;
fpStudent = fopen("Students.dat", "rb");
if (fpStudent == NULL)
{
nRet = 0;
goto EXIT_PROC;
}
//读取学生数量
if (fread(&tfTemp, 4, 2, fpStudent) != 2)
{
nRet = 0;
goto EXIT_PROC;
}
//跳过保留的100字节
if (fseek(fpStudent, 100, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
for (i = 0; i < tfTemp.m_FileNumber; i++)
{
//按每个200字节查询
if (fread(Temp, 200, 1, fpStudent) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
if (((tagStudents *)Temp)->m_Mark == 0)
{
continue;
}
if (strcmp(((tagStudents *)Temp)->m_StuNumber, szbuff) == 0)
{
nRet = 2;
goto EXIT_PROC;
}
}
nRet = 1;
EXIT_PROC:
SAFE_CLOSE(fpStudent);
return nRet;
}
//删除学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2成功删除
int DeleteStudents(char *szStuNumber)
{
FILE *fpStudent = NULL;
char Temp[200] = {0};
int nRet = 0, i = 0;
tagFile tfTemp;
fpStudent = fopen("Students.dat", "rb+");
if (fpStudent == NULL)
{
nRet = 0;
goto EXIT_PROC;
}
//读取学生数量
if (fread(&tfTemp, 4, 2, fpStudent) != 2)
{
nRet = 0;
goto EXIT_PROC;
}
//跳过保留的100字节
if (fseek(fpStudent, 100, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
for (i = 0; i < tfTemp.m_FileNumber; i++)
{
//按每个200字节查询
if (fread(Temp, 200, 1, fpStudent) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
if (((tagStudents *)Temp)->m_Mark == 0)
{
continue;
}
if (strcmp(((tagStudents *)Temp)->m_StuNumber, szStuNumber) == 0)
{
StuInterfaceShow(*(tagStudents *)Temp);
//询问是否删除
if (SelectStuDelete())
{
//从当前指针偏移-200,清空数据
if (fseek(fpStudent, -200, SEEK_CUR))
{
nRet = 0;
goto EXIT_PROC;
}
//清空,并写入文件
memset(Temp, 0, 200);
if (fwrite(Temp, 200, 1, fpStudent) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
//学生总数量减1
tfTemp.m_AllStuNumber--;
//如果后面没有数据,学生数据大小也减1
if (fgetc(fpStudent) == 0)
{
tfTemp.m_FileNumber--;
}
//指向文件头
if (fseek(fpStudent, 0, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
//写入数据
if (fwrite(&tfTemp, 4, 2, fpStudent) != 2)
{
nRet = 0;
goto EXIT_PROC;
}
nRet = 2;
goto EXIT_PROC;
}
else
{
return -1;
}
}
}
nRet = 1;
EXIT_PROC:
SAFE_CLOSE(fpStudent);
return nRet;
}
//修改学生信息
//参数:学生学号
//返回值:为0则文件出错,为1则没有该学号,为2已找到
int ChengeStudents(char *szStuNumber)
{
FILE *fpStudent = NULL;
char Temp[200] = {0};
tagStudents * tsTemp = NULL;
int nRet = 0, i = 0;
tagFile tfTemp;
fpStudent = fopen("Students.dat", "rb+");
if (fpStudent == NULL)
{
nRet = 0;
goto EXIT_PROC;
}
//读取学生数量
if (fread(&tfTemp, 4, 2, fpStudent) != 2)
{
nRet = 0;
goto EXIT_PROC;
}
//跳过保留的100字节
if (fseek(fpStudent, 100, SEEK_SET))
{
nRet = 0;
goto EXIT_PROC;
}
for (i = 0; i < tfTemp.m_FileNumber; i++)
{
//按每个200字节查询
if (fread(Temp, 200, 1, fpStudent) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
if (((tagStudents *)Temp)->m_Mark == 0)
{
continue;
}
if (strcmp(((tagStudents *)Temp)->m_StuNumber, szStuNumber) == 0)
{
//从当前指针偏移-200,清空数据
if (fseek(fpStudent, -200, SEEK_CUR))
{
nRet = 0;
goto EXIT_PROC;
}
//先将修改好的写进内存
tsTemp = SelectChenge((tagStudents *)Temp);
//写入修改好的项目
if (fwrite(tsTemp, 200, 1, fpStudent) != 1)
{
nRet = 0;
goto EXIT_PROC;
}
nRet = 2;
goto EXIT_PROC;
}
}
nRet = 1;
EXIT_PROC:
SAFE_CLOSE(fpStudent);
return nRet;
}