Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created April 22, 2010 09:57
Show Gist options
  • Save johnhmj/375049 to your computer and use it in GitHub Desktop.
Save johnhmj/375049 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <cstring>
// 緩衝區大小
#define BUFFERSIZE 4096
// 讀檔模式
#define READ (ios_base::in)
#define WRITE (ios_base::out | ios_base::app)
#define REWRITE (ios_base::out | ios_base::trunc)
// Integrated Development Environment
// Visual C++
using namespace std;
//
class CMyTxtFile
{
public:
// 檔案名稱
char* m_filename;
// 建構子
CMyTxtFile(const char* _filename);
// 解構子
~CMyTxtFile(void);
// 複製檔案內容
CMyTxtFile& operator =(const CMyTxtFile& _mytxtfile);
// 在目的檔案尾端加入來源檔案的內容
CMyTxtFile& operator +=(const CMyTxtFile& _mytxtfile);
// 列印檔案內容
void print(void);
// 檔案是否存在
bool is_exist(void);
private:
// 檔案串流
fstream m_file;
// 暫存緩衝區的指標
char* m_buffer;
// 初始化記憶體
void initmem(void* _buffer, size_t _size);
};
void main(int argc, char** argv)
{
char *srcfn = "source.txt";
char *desfn = "destination.txt";
CMyTxtFile src(srcfn), des(desfn);
cout<<src.m_filename<<"=>"<<(src.is_exist()?"Exist":"Doesnot exist")<<endl;
cout<<des.m_filename<<"=>"<<(des.is_exist()?"Exist":"Doesnot exist")<<endl;
src.print();
des = src;
des += src;
des.print();
system("PAUSE");
}
CMyTxtFile::CMyTxtFile(const char* _filename)
{
this->m_file.clear();
size_t _lenfilename = strlen(_filename);
this->m_filename = new char[_lenfilename + 1];
this->initmem((void*) this->m_filename, (_lenfilename + 1));
strcpy(this->m_filename, _filename);
this->m_buffer = NULL;
}
CMyTxtFile::~CMyTxtFile(void)
{
this->m_file.clear();
if ( this->m_filename != NULL )
{
delete [] this->m_filename;
this->m_filename = NULL;
}
}
CMyTxtFile& CMyTxtFile::operator =(const CMyTxtFile& _mytxtfile)
{
if ( this != &_mytxtfile )
{
fstream _source;
_source.open(_mytxtfile.m_filename, READ);
if ( _source.is_open() )
{
this->m_file.open(this->m_filename, REWRITE);
this->m_buffer = new char[BUFFERSIZE];
size_t _readsize = BUFFERSIZE - 1;
while ( !_source.eof() )
{
this->initmem((void*)this->m_buffer, BUFFERSIZE);
_source.read(this->m_buffer, _readsize);
this->m_file<<this->m_buffer;
}
delete [] this->m_buffer;
this->m_buffer = NULL;
this->m_file.close();
_source.close();
}
}
return (*this);
}
CMyTxtFile& CMyTxtFile::operator +=(const CMyTxtFile& _mytxtfile)
{
if ( this != &_mytxtfile )
{
fstream _source;
_source.open(_mytxtfile.m_filename, READ);
if ( _source.is_open() )
{
this->m_file.open(this->m_filename, WRITE);
this->m_buffer = new char[BUFFERSIZE];
size_t _readsize = BUFFERSIZE - 1;
while ( !_source.eof() )
{
this->initmem((void*)this->m_buffer, BUFFERSIZE);
_source.read(this->m_buffer, _readsize);
this->m_file<<this->m_buffer;
}
delete [] this->m_buffer;
this->m_buffer = NULL;
this->m_file.close();
_source.close();
}
}
return (*this);
}
void CMyTxtFile::print(void)
{
cout<<"FileName= "<<(this->m_filename)<<endl;
this->m_file.open(this->m_filename, READ);
if ( this->m_file.is_open() )
{
this->m_buffer = new char[BUFFERSIZE];
size_t _readsize = BUFFERSIZE - 1;
while ( !this->m_file.eof() )
{
this->initmem((void*)this->m_buffer, BUFFERSIZE);
this->m_file.read(this->m_buffer, _readsize);
cout<<this->m_buffer;
}
cout<<endl;
delete [] this->m_buffer;
this->m_buffer = NULL;
this->m_file.close();
}
}
bool CMyTxtFile::is_exist(void)
{
this->m_file.open(this->m_filename, READ);
if ( this->m_file.is_open() )
{
this->m_file.close();
return true;
}
return false;
}
void CMyTxtFile::initmem(void* _buffer, size_t _size)
{
if ( _buffer != NULL )
{
memset((void*) _buffer, NULL, _size);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment