📄 diskfile.cpp
字号:
// DiskFile.cpp: implementation of the DiskFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DiskFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DiskFile::DiskFile(const char *fname=NULL,const char *mode=NULL):FileDescriptor(PFILE)
{
/*
char modestr[4]={0};
if(mode&READ&&mode&~WRITE&&mode&~APPEND)
strcat(modestr,"r");
else if(mode&~READ&&mode&WRITE&&mode&~APPEND)
strcat(modestr,"w");
else if(mode&~READ&&mode&~WRITE&&mode&APPEND)
strcat(modestr,"a");
else
strcat(modestr,"r"); //default is read only
if(mode&~BITFORMAT)
strcat(modestr,"")
*/
f = NULL;
this->open(fname,mode);
}
DiskFile::~DiskFile()
{
close();
}
bool DiskFile::open(const char *fname,const char *mode)
{
status = CLOSED;
f = fopen(fname,mode);
if(!f)
return false;
status = OPEN;
return true;
}
int DiskFile::write(const void* aBuffer, int aLen)
{
int i = -1;
if(f&&aLen>0&&aBuffer)
{
i = fwrite(aBuffer,sizeof(char),aLen,f);
if(i!=aLen&&ferror(f)!=0)
{
i = FILE_ERROR;
setLastError(ferror(f));
}
}
return i;
}
int DiskFile::read(void* aBuffer, int aBufLen)
{
int i = -1;
if(f&&aBuffer&&aBufLen>0)
{
i = fread(aBuffer,sizeof(char),aBufLen,f);
if(i==0&&feof(f))
i = FILE_EOF;
else if(i!=aBufLen&&ferror(f)!=0)
{
i = FILE_ERROR;
setLastError(ferror(f));
}
}
return i;
}
void DiskFile::close()
{
status = CLOSED;
if(f)
fclose(f);
f = NULL;
}
int DiskFile::getLastError()
{
return err;
}
void DiskFile::setLastError(int errCode)
{
err = errCode;
}
long DiskFile::size()
{
if(f)
{
fseek(f,0L,SEEK_SET);
long start = ftell(f);
fseek(f,0L,SEEK_END);
long end = ftell(f);
return end-start+1;
}
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -