写一个程序将文件中的内容复制到另一个文件中

写一个函数int fileCopy(char *destFileName, char *resFileName)将文件resFileName的内容复制到文件destFileName的末尾。如果文件复制成功,则返回1,否则返回0。主要功能如下。如果成功,检查fileName2文件内容。

img

导出导入

int fileCopy(char *destFileName, char *resFileName){
    if (CopyFile(destFileName,resFileName,FALSE)return true;
    return false
} 

需要头文件:Windows.h

简单读取和写入

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int readAudioDateToBufferMemory(std::string& destFileName, std::string& resFileName)
{
    // 读取resFileName文件
    std::ifstream infile(resFileName, std::ios::in | std::ios::binary);
    if (!infile.is_open())
    {
        return 0;
    }

    infile.seekg(0, std::ios::end);
    int p_nLen = infile.tellg();

    infile.seekg(0, std::ios::beg);
    char* p_pAudio = new char[p_nLen];

    try
    {
        infile.read(p_pAudio, p_nLen);
    }
    catch (std::exception& e)
    {
        infile.close();
        return 0;
    }
    infile.close();

    // 写入destFileName文件
    ofstream outFile(destFileName, ios::out | ios::binary | ios_base::app);    //ios_base::app:以追加方式打开文件,所有写文件的数据都是追加在文件末尾。
    if (!outFile.is_open())
    {
        return 0;
    }
    try
    {
        outFile.write(p_pAudio, p_nLen);
    }
    catch (std::exception& e)
    {
        outFile.close();
        return 0;
    }
    outFile.close();

    return 1;
}

int main(int argc, char const* argv[])
{
    std::string resFileName = "1.txt";
    std::string destFileName = "2.txt";
    int flag = readAudioDateToBufferMemory(destFileName, resFileName);
    if (0 == flag)
    {
        // 失败
    }
    else
    {
        // 成功
    }

    return 0;
}