Object Oriented Programming Practical 25
Question 1
Write a C++ Program to Copy the Contents of One File Into Another File.
copy.cpp
C
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
std::ifstream inFile;
std::ofstream outFile;
inFile.open("abc.txt");
if (!inFile)
{
std::cout << "Error: Unable to open input file!" << endl;
return 1;
}
outFile.open("xyz.txt");
if (!outFile) {
std::cout << "Error: Unable to open output file!" << endl;
return 1;
}
std::string line;
while (std::getline(inFile, line))
{
outFile << line << endl;
}
inFile.close();
outFile.close();
std::cout << "Content copied successfully!" << endl;
return 0;
}
Output
