write me a c++ code to read from file1 and paste the info of file1 to file2
- User
write me a c++ code to read from file1 and paste the info of file1 to file2
- Assistant
Unfortunately, as an AI language model, I am not able to access files. However, I can provide you with a code template for reading from one file and writing to another in C++:
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line; ifstream fin("file1.txt"); //opening file1 for reading if (fin.is_open()) { //checking if file is opened successfully ofstream fout("file2.txt"); //opening file2 for writing if (fout.is_open()) { //checking if file is opened successfully while (getline(fin, line)) { //reading each line from file1 fout << line << endl; //writing each line to file2 } fout.close(); //closing file2 } else { cout << "Error: Unable to open file for writing." << endl; } fin.close(); //closing file1 } else { cout << "Error: Unable to open file for reading." << endl; } return 0; }