在这里,我们将了解如何使用 C++ 程序从一个文件中读取内容并将其写入另一个文件。让我们考虑两个文件 file1.txt 和 file2.txt。我们将读取 file.txt 的内容并将其写入 file2.txt
file1.txt内容:
方法:
- 创建一个输入文件流对象并在其中打开 file.txt。
- 创建一个输出文件流对象并在其中打开 file2.txt。
- 从文件中读取每一行并将其写入 file2。
下面是从一个文件中读取内容并将其写入另一个文件的 C++ 程序:
// C++ program to read contents from
// one file and write it to another file
#include<bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
// Input file stream object to
// read from file.txt
ifstream in("file1.txt");
// Output file stream object to
// write to file2.txt
ofstream f("file2.txt");
// Reading file.txt completely using
// END OF FILE eof() me