用C++将几个文本文件中的内容输出到一个文本文件中
时间: 2024-02-05 20:14:02 浏览: 42
可以使用以下的代码来实现:
```cpp
#include <iostream>
#include <fstream>
#include <string>
int main() {
// 打开多个输入文件
std::ifstream file1("file1.txt");
std::ifstream file2("file2.txt");
std::ifstream file3("file3.txt");
// 打开输出文件
std::ofstream outfile("output.txt");
// 将文件内容输出到输出文件中
std::string line;
if (file1.is_open()) {
while(getline(file1, line)) {
outfile << line << "\n";
}
file1.close();
}
if (file2.is_open()) {
while(getline(file2, line)) {
outfile << line << "\n";
}
file2.close();
}
if (file3.is_open()) {
while(getline(file3, line)) {
outfile << line << "\n";
}
file3.close();
}
outfile.close();
return 0;
}
```
以上代码将文件 `file1.txt`、`file2.txt` 和 `file3.txt` 中的内容逐行输出到一个新的文件 `output.txt` 中。你可以根据自己的需要修改文件名和路径。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)