C++:文本内容复制

这是一个C++类,用于复制文件。类`copyFile`包含了源文件和目标文件的路径,以及相关的方法来读取源文件并写入目标文件。在主函数中,用户输入源文件和目标文件的路径,然后类的实例会执行文件复制操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

*   头文件

**/

#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 &copyFile::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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值