/**
* 头文件
**/
#ifndef __COPYFILE__
#define __COPYFILE__
#define LEN 256
class copyFile{
public:
copyFile(const char *src = NULL, const char *dest = NULL);
copyFile(const copyFile &myObject);
copyFile & operator = (const copyFile &myObejct);
~copyFile(void);
void copy2File();
private:
char *pSrc;
char *pDest;
FILE *srcFile;
FILE *destFile;
int ch;
};
#endif
/**
* 源文件
**/
#include "iostream"
#include "copyFile++.h"
using namespace std;
copyFile::copyFile(const char *src, const char *dest){
ch = 0;
srcFile = NULL;
destFile = NULL;
if (src == NULL || dest == NULL) {
pSrc = new char[1];
pDest = new char[1];
if (pSrc == NULL || pDest == NULL) {
cout << "Memorry fail!" << endl;
exit(1);
}
*pSrc = '\0';
*pDest = '\0';
}
else {
int srcLen = strlen(src) + 1;
int destLen = strlen(dest) + 1;
pSrc = new char[srcLen];
pDest = new char[destLen];
if (pSrc == NULL || pDest == NULL) {
cout << "Memorry fail!" << endl;
exit(1);
}
strcpy(pSrc, src);
strcpy(pDest, dest);
}
}
copyFile::~copyFile(void){
delete []pSrc;
delete []pDest;
}
copyFile::copyFile(const copyFile &myObject) {
int srcLen = strlen(myObject.pSrc) + 1;
int destLen = strlen(myObject.pDest) + 1;
pSrc = new char[srcLen];
pDest = new char[destLen];
if (pSrc == NULL || pDest == NULL) {
cout << "Memorry fail!" << endl;
exit(1);
}
strcpy(pSrc, myObject.pSrc);
strcpy(pDest, myObject.pDest);
}
copyFile ©File::operator=(const copyFile &myObject) {
if (this == &myObject) {
return *this;
}
delete []pSrc;
delete []pDest;
int srcLen = strlen(myObject.pSrc) + 1;
int destLen = strlen(myObject.pDest) + 1;
pSrc = new char[srcLen];
pDest = new char[destLen];
if (pSrc == NULL || pDest == NULL) {
cout << "Memorry fail!" << endl;
exit(1);
}
strcpy(pSrc, myObject.pSrc);
strcpy(pDest, myObject.pDest);
return *this;
}
void copyFile::copy2File() {
if ((srcFile = fopen(pSrc, "r")) == NULL) {
cout << "Can not open source file:" << pSrc << endl;
}
else {
if ((destFile = fopen(pDest, "w")) == NULL) {
cout << "Can not open source file:" << pDest << endl;
fclose(srcFile);
}
else {
while ((ch = fgetc(srcFile)) != EOF)
fputc(ch, destFile);
cout << "Can open source file\"" << pSrc << "\"" << endl;
cout << "Can open source file\"" << pDest << "\"" << endl;
cout << "Successful to copy a file!\n";
fclose(srcFile);
fclose(destFile);
}
}
}
int main(int argc, char *argv[]) {
char srcPath[LEN];
char destPath[LEN];
cout << "Please input the srcAddre:" ;
cin >> srcPath;
cout << "Please input the destAddre:" ;
cin >> destPath;
copyFile *pFile = new copyFile(srcPath, destPath);
pFile->copy2File();
delete pFile;
pFile = NULL;
system("pause");
return 1;
}