⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfileset.cpp

📁 对单个文件流进行操作
💻 CPP
字号:
CFileSet::CFileSet()
{
 this->m_bOpened = false;
 this->m_pFile = NULL;
 memset(this->m_RecSep,0,sizeof(this->m_RecSep));
 memset(this->m_FieldSep,0,sizeof(this->m_FieldSep));
}
CFileSet::~CFileSet()
{
 this->close();
}
void CFileSet::close()
{
 if ( this->m_bOpened )
 {
  fclose(m_pFile);
  m_pFile = NULL;
  this->m_bOpened = false;
 }
}
bool CFileSet::open(const char* filename,const char* fieldsep,const char* recsep,const char* method)
{
 this->m_pFile = fopen(filename,method);
 if ( m_pFile <= 0 )
  return false;
 sprintf(this->m_FieldSep,"%s",fieldsep);
 sprintf(this->m_RecSep,"%s",recsep);
 //搜索文件的最后
 fseek(this->m_pFile,0,SEEK_END);
 //获取文件的大小
 this->iFileSize = ftell(m_pFile);
 //把文件指针置回文件头
 fseek(this->m_pFile,0,SEEK_SET);
 this->iPos = 0;
 this->m_bOpened = true;
 return true;
}
bool CFileSet::next()
{
 //读取一定的字节到内存里
 int size = fread(this->m_Line,sizeof(char),MAXRECLENGTH,m_pFile);
 if ( size <= 0 )
 {
  this->bHasNext = false;
  return bHasNext;
 }
 this->m_Line[size] = 0;
 int iRecSepSize = strlen(this->m_RecSep);
 bool bFound = false;
 int i;
 for ( i = 0 ; i < size && !bFound ; i++ )
 {
  int j;
  for ( j = 0 ; j < iRecSepSize; j++)
  {
   if ( m_Line[i++] == m_RecSep[j] )
    continue;
   else
   {
    i = i - j - 1;
    break;
   }
  }
  if ( j == iRecSepSize )
   bFound = true;
 }
 if ( ! bFound )
  return false;
  //throw new exception("Record Length Is Larger than MAXRECLENGTH");
 this->iLineLength = i-1;
 this->m_Line[iLineLength - iRecSepSize] = 0;
 this->iPos += iLineLength;
 //恢复文件的指针,以便下次从下一行开始
 if ( ftell(m_pFile) != iPos )
  fseek(m_pFile,iPos,SEEK_SET);
 this->splitLine();
 this->bHasNext = true;
 return true;
}
bool CFileSet::getInteger(int index,int& value,const int& defval)
{
 if ( index >= this->m_LineRecs.size() )
 {
  value = defval;
  return false;
 }
 value = atoi(m_LineRecs[index].c_str());
 return true;
}
bool CFileSet::getString(int index,string& value,const string& defval)
{
 if ( index >= fieldCount )
 {
  value = defval;
  return false;
 }
 value = m_LineRecs[index];
 return true;
}
void CFileSet::splitLine()
{
 this->m_LineRecs.clear();
 char line[MAXRECLENGTH+1];
 sprintf(line,"%s",m_Line);
 fieldCount = 0;
 char* token = strtok(line,this->m_FieldSep);
 for ( ; token ; token = strtok(NULL,this->m_FieldSep) )
 {
  m_LineRecs.push_back(token);
  fieldCount++;
 }
}
bool CFileSet::getHoleLine(string& value)
{
 if ( bHasNext )
  value = this->m_Line;
 return bHasNext;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -