没有测试
class CFileStat
{
private:
char * filePath; //文本文件路径
int charCount; //文件中的字符数
public:
//将传入的path记录到 this.filePath中(this.filePath应动态分配空间),
//并计算该文件中的字符数,记录到this.charCount中
CFileStat(const char * path);
CFileStat(const CFileStat& stat); //拷贝构造函数
~CFileStat();
const char* getfilePath(); //返回 this.filePath
int getCharCount(); //返回 this.charCount
// 将传入的stat.filePath对应文本文件的内容添加到 this.filePath
// 对应的文本文件末尾,并将stat.charCount的值加到this.charCount中
CFileStat& operator +=(const CFileStat& stat);
};
CFileStat::CFileStat(const char * path)
{
int i=0,len=0;
while(path[len++]);
this->filePath=new char[len];
for(i=0;i<len;i++)this->filePath[i]=path[i];
FILE *fl=fopen(this->filePath,"rb");
if(fl)
{
fseek(fl,0,SEEK_END);
this->charCount=ftell(fl);
fclose(fl);
}
else
this->charCount=-1;
}
CFileStat::CFileStat(const CFileStat& stat)
{
int i=0,len=0;
while(stat.filePath[len++]);
this->filePath=new char[len];
for(i=0;i<len;i++)this->filePath[i]=stat.filePath[i];
FILE *fl=fopen(stat.filePath,"rb");
if(fl)
{
fseek(fl,0,SEEK_END);
this->charCount=ftell(fl);
fclose(fl);
}
else
this->charCount=-1;
}
CFileStat::~CFileStat()
{
if(NULL!=this->filePath)delete[] this->filePath;
}
const char* CFileStat::getfilePath()
{
return this->filePath;
}
int CFileStat::getCharCount()
{
return this->charCount;
}
CFileStat& CFileStat::operator +=(const CFileStat& stat)
{
FILE *flsrc=fopen(stat.filePath,"rb"),*fldes=fopen(this->filePath,"ab+");
if(flsrc&&fldes)
{
fseek(flsrc,0,SEEK_END);
int len=ftell(flsrc);
fseek(flsrc,0,SEEK_SET);
char *rdch=new char[len];
fread(rdch,len,1,flsrc);
fclose(flsrc);
fwrite(rdch,len,1,fldes);
fclose(fldes);
this->charCount+=len;
delete[] rdch;
}
return *this;
}
|