In: Computer Science
Write a C++ pgm which
1.reads an input file named 'input.txt'
2.content of input.txt are not known to you
3.copies content of input file into an output file named 'output.txt'
4.contents of output.txt should be exactly the same as contents of input.txt
5.the user should be given a choice to select input and output file in any folder
6.remember to check that input file opened successfully
SOURCE CODE:
#include <iostream>
#include<string>
#include <fstream> //including c++ standard libray fstream
for file operations
using namespace std;
int main()
{
string s;
string Str="";
string InFileName="";
string OutFileName="";
cout<<"Enter input file name: ";
getline(cin,InFileName); //reading input file name from user
ifstream myfile(InFileName.c_str()); //ifstream is used to read
information from file
if(myfile.is_open()) //checking whether the file is opened or
not
{
while(getline(myfile,s)) //while loop for reading file line by line
to a string 's'
{
Str=Str+s+"\n"; //adding s to Str for further processing
}
myfile.close(); //closing ifstream
}
else //if file is not opened then printing message
{
cout<<"File opening not successful"<<endl;
return 0;
}
cout<<"Enter ouput file name: ";
getline(cin,OutFileName); //reading output file name from
user
ofstream file(OutFileName.c_str()); //ofstream is used to write
information to file
file<<Str<<endl; //writing information to file that is
stored in the string 'Str'
file.close(); //closing ofstream
cout<<"Information from "<<InFileName<<" to
"<<OutFileName<<" copied
successfully"<<endl;
return 0;
}
CONSOLE OUTPUT:
input.txt
Output.txt