here is the header file data:
#ifndef UFileH
#define UFileH
#include <stdio.h>
#include <time.h>
#include <system.hpp>
//---------------------------------------------------------------------------
/* aMode type
r Open for reading only.
w Create for writing. .
a Append; open for writing at end-of-file or create for writing if the file does not exist.
r+ Open an existing file for update (reading and writing).
w+ Create a new file for update (reading and writing).
a+ Open for append; open (or create if the file does not exist) for update at the end of the file.
b Binary
t Text
*/
// File Exceptions
class EFile
{
private:
int aErrorCode;
AnsiString aErrorMsg;
public:
EFile()
{
aErrorCode = 0;
aErrorMsg = "";
};
EFile(int pErrorCode)
{
aErrorCode = pErrorCode;
aErrorMsg = "";
};
EFile(int pErrorCode,AnsiString pErrorMsg)
{
aErrorCode = pErrorCode;
aErrorMsg = pErrorMsg;
};
int ErrorCode()
{
return aErrorCode;
};
AnsiString ErrorMsg()
{
return aErrorMsg;
};
};
class ENoClose:EFile
{
};
class ECantClose:EFile
{
};
class ENoRemove:EFile
{
};
class ENoRename:EFile
{
};
class ENoSeek:EFile
{
};
class ENoOpen:EFile
{
};
class TFile
{
private:
AnsiString aFileName;
bool aOpen;
FILE *fFile;
public:
TFile(AnsiString pFileName);
~TFile();
void Close();
bool Eof();
bool FileExists();
long FilePos();
long FileSize();
bool IsOpen()
{
return aOpen;
}
void Open(const char *pMode);
int Read(void *pInfo, size_t pSize, size_t pCount = 1);
void Remove();
void Rename(AnsiString pName);
void SeekFromCur(long pPositions);
void SeekFromEnd(long pPosition);
void SeekTo(long pPosition);
int Write(void *pInfo, size_t pSize, size_t pCount = 1);
};
//---------------------------------------------------------------------------
#endif
here is the implementation (.cpp)
#include <vcl.h>
#pragma hdrstop
#include "UFile.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
TFile::TFile(AnsiString pFileName)
{
aFileName = pFileName;
aOpen = false;
}
//---------------------------------------------------------------------------
TFile::~TFile()
{
try
{
Close();
}
catch(...)
{
};
}
//---------------------------------------------------------------------------
void TFile::Open(const char *pMode)
{
fFile = fopen( aFileName.c_str(), pMode);
if (fFile == NULL)
throw ENoOpen();
aOpen = True;
}
//---------------------------------------------------------------------------
bool TFile::Eof()
{
return feof(fFile);
}
//---------------------------------------------------------------------------
void TFile::Remove()
{
if (!aOpen)
{
if(remove(aFileName.c_str()) != 0)
throw ENoRemove();
}
else
throw ENoClose();
}
//---------------------------------------------------------------------------
bool TFile::FileExists()
{
return ::FileExists(aFileName);
}
//---------------------------------------------------------------------------
void TFile::Close()
{
if (aOpen)
{
if(fclose(fFile) == 0)
aOpen = false;
else
throw ECantClose();
}
}
//---------------------------------------------------------------------------
int TFile::Write(void *pInfo, size_t pSize, size_t pCount)
{
return fwrite(pInfo, pSize, pCount, fFile);
}
//---------------------------------------------------------------------------
int TFile::Read(void* pInfo, size_t pSize, size_t pCount)
{
return fread(pInfo, pSize, pCount, fFile);
}
//---------------------------------------------------------------------------
void TFile::SeekTo(long pPosition)
{
if (fseek(fFile, pPosition, SEEK_SET) != 0)
throw ENoSeek();
}
//---------------------------------------------------------------------------
void TFile::SeekFromCur(long pPositions)
{
if(fseek(fFile, pPositions, SEEK_CUR) != 0)
throw ENoSeek();
}
//---------------------------------------------------------------------------
void TFile::SeekFromEnd(long pPosition)
{
if(fseek(fFile, pPosition, SEEK_END) != 0)
throw ENoSeek();
}
//---------------------------------------------------------------------------
long TFile::FilePos()
{
return ftell(fFile);
}
//---------------------------------------------------------------------------
long TFile::FileSize()
{
long position = ftell(fFile);
fseek(fFile, 0, SEEK_END);
long result = ftell(fFile);
fseek(fFile, position, SEEK_SET);
return result;
}
//---------------------------------------------------------------------------
void TFile::Rename(AnsiString pName)
{
if (!aOpen)
{
if(rename(aFileName.c_str(),pName.c_str()) != 0)
throw ENoRename();
else
aFileName = pName;
}
else
throw ENoClose();
}
//---------------------------------------------------------------------------
good luck