rewind
语法:#include <stdio.h> void rewind( FILE *stream ); |
函数rewind()把文件指针移到由stream(流)指定的开始处, 同时清除和流相关的错误和EOF标记.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
用于学生管理系统,文件操作中:
void CStuDataBase::Initialize(char* pszDataBaseName)
{
if (NULL == pszDataBaseName)
throw "Connection to database fail,[Invalid FileName]";
m_pfReadFile = fopen(pszDataBaseName,"a+b");
if (NULL == m_pfReadFile)
throw "Connection to database fail!";
//开始读取数据
rewind(m_pfReadFile); //跳到文件开头
int iCount = 0;
while (fread(&m_stuObject[iCount],sizeof(m_stuObject[iCount]),1,m_pfReadFile) == 1)
{
iCount++;
}
fclose(m_pfReadFile);
m_RecordCount = iCount; //保存记录
if (m_pszDataBaseName)
{
delete []m_pszDataBaseName;
m_pszDataBaseName = NULL;
}
//以下保存文件名以便以后使用
int iFileNameMax = strlen(pszDataBaseName);
m_pszDataBaseName = new char[iFileNameMax + 1];
strncpy(m_pszDataBaseName,pszDataBaseName,iFileNameMax);
m_pszDataBaseName[iFileNameMax] = '\0';
}
评论