C++复制文件(一)

#include<iostream>

#include<fstream>

/*****************************************************************************************

Function:      CopyFile

Description:    复制文件

Input:          SourceFile:原文件路径 NewFile:复制后的文件路径

Return:        1:成功 0:失败

******************************************************************************************/

int CopyFile(char *SourceFile, char *NewFile)

{

std::ifstream in;

std::ofstream out;

try

{

in.open(SourceFile, std::ios::binary);//打开源文件

if (in.fail())//打开源文件失败

{

std::cout << "Error 1: Fail to open the source file." << std::endl;

in.close();

out.close();

return 0;

}

out.open(NewFile, std::ios::binary);//创建目标文件

if (out.fail())//创建文件失败

{

std::cout << "Error 2: Fail to create the new file." << std::endl;

out.close();

in.close();

return 0;

}

else//复制文件

{

out << in.rdbuf();

out.close();

in.close();

return 1;

}

}

catch (std::exception e)

{

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,731评论 0 3
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,481评论 0 10
  • 接着上节 atomic,本节主要介绍condition_varible的内容,练习代码地址。本文参考http://...
    jorion阅读 8,544评论 0 7
  • C++入门笔记:高级编程 文件和流 打开文件void open (const char *filename, io...
    云之外阅读 871评论 0 4
  • 值得怀念的东西,不仅仅因为它美好,也因为它的短暂。 开始一段旅途时,最先遐想的是旅途的美好,对未来路上风景的期待,...
    栗子lizi阅读 438评论 0 0