In: Computer Science
Write a program in C++ that Copies Story.txt to DuplicateStory.txt. Each line in Story.txt will be written three times in DuplicateStory.txt.
Answer:
Note: To run program your Story.txt file need to be in same folder where program file is stored.
Code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string str;
ifstream infile{"Story.txt"}; //creating input file stream
object
ofstream outfile{"DuplicateStory.txt"};//creating output file
stream object
if(infile && outfile)// validate both
files
{
while(getline(infile,str))//read line from input file upto the
end
{
outfile << str << "\n";//writing line 1st time
outfile <<
str << "\n";//writing line 2nd time
outfile <<
str << "\n";//writing line 3rd time
}
cout << "File copied successfully \n";
} else //if error with any file
{
printf("Cannot read File");
}
infile.close();//Closing file Story.txt
outfile.close();//Closing file DuplicateStory.txt
return 0;
}
Screenshot: