Write a program in c++ to copy contents of ABC.txt to XYZ.txt
Answers
Answered by
1
Answer:
contests append (line)
file-handle.close ()
file handle2=openXYZ.text.a
Please mark me as brainliest
Answered by
0
Here is a program in C++ that can be used to copy the contents of the file "ABC.txt" to a new file "XYZ.txt".
- This program uses the fstream library to handle input and output from/to files.
- #include <iostream>
- #include <fstream>
- using namespace std;
- int main() {
- // Open the input file "ABC.txt" for reading
- ifstream inputFile("ABC.txt");
- // Check if the input file was successfully opened
- if (!inputFile.is_open()) {
- cerr << "Error opening input file ABC.txt" << endl;
- return 1;
- }
- // Open the output file "XYZ.txt" for writing
- ofstream outputFile("XYZ.txt");
- // Check if the output file was successfully opened
- if (!outputFile.is_open()) {
- cerr << "Error opening output file XYZ.txt" << endl;
- return 1;
- }
- // Copy the contents of the input file to the output file
- string line;
- while (getline(inputFile, line)) {
- outputFile << line << endl;
- }
- // Close the input and output files
- inputFile.close();
- outputFile.close();
- // Confirm that the copy was successful
- cout << "File copied successfully" << endl;
- return 0;
- }
- This program first opens the input file "ABC.txt" using an ifstream object and checks if it was successfully opened. If the file was not successfully opened, an error message is displayed and the program exits with a non-zero status code.
- Next, the output file "XYZ.txt" is opened using an ofstream object and similarly checked if it was successfully opened.
- The contents of the input file are then copied to the output file line by line using a while loop and the getline function. The getline function reads a line of text from the input file and stores it in the line string. The contents of the line string are then written to the output file using the << operator.
- Finally, the input and output files are closed and a confirmation message is displayed to indicate that the copy was successful.
#SPJ3
Similar questions