如果想要将一个文件的内容拷贝到另一个文件中,常规的做法是读取源文件,然后再把内容写入到目的文件中:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> readFile(const string& filename)
{
vector<string> ret;
ifstream iReadFile(filename);
if(iReadFile)
{
string tmpStr;
while(getline(iReadFile, tmpStr))
{
ret.emplace_back(tmpStr);
}
}
return ret;
}
void writeFile(const string& filename, const vector<string>& fileContent)
{
ofstream iWriteFile(filename);
if(iWriteFile)
{
for(const auto& s : fileContent)
{
iWriteFile<<s<<"\n";
}
}
}
int main()
{
auto&& fileContent = readFile("./src_file.txt");
writeFile("./des_file.txt",fileContent);
}
标签:sendfile,const,string,ret,filename,iReadFile,Linux,拷贝,include From: https://blog.csdn.net/jiemashizhen/article/details/136691515创建一个测试用的src_file.txt