C++ 将txt内容全部移动到另一个txt
时间: 2024-03-15 11:44:50 浏览: 21
可以使用C++中的文件流操作来实现。下面是一个简单的示例代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string source_file = "source.txt"; // 源文件名
string target_file = "target.txt"; // 目标文件名
ifstream source(source_file); // 打开源文件
ofstream target(target_file); // 创建目标文件
if(source.is_open() && target.is_open()) // 判断文件是否成功打开
{
string line; // 用于存储读取的一行文本
while(getline(source, line)) // 逐行读取源文件内容
{
target << line << endl; // 将读取的内容写入目标文件
}
cout << "移动完成!" << endl;
}
else
{
cout << "文件打开失败!" << endl;
}
source.close(); // 关闭源文件
target.close(); // 关闭目标文件
return 0;
}
```
这个程序将会把 `source.txt` 文件中的内容全部移动到 `target.txt` 文件中。