C++ OPEN AND COPY FILES
Your task is to create a simple C++ program that prompts the user for two file names, opens both files, reads the data from one file and copies that data to the other file. After this is done, the files are both closed and the program ends.
1. Your program, opencopy.cpp, needs to use both an ifstream object and an ofstream object.
2. You only need to use #include for these but make sure that you also have using std::ifstream and using std::ofstream.
3. You will use string objects to get the filenames from the user. That should make it a lot easier .
4. If one or both of the files cannot be opened, you need to provide the user with an error message about that fact, including the offending name(s). It then needs to prompt the user for two more names and continue until the program can open both the input and output files.
5. If the program can only open one file, it needs to close() the other file before trying to open any new ones. You may also need to clear() the stream that failed to open, as in myInputFile.clear()
6. The program should use the getline() function to read data from the input file a line at a time and you can write it to the output file with the stream insertion (<<) operator.
7. The getline() function reads until it encounters a newline character. It takes the newline out of the stream and discards it. That means that you have to put the newlines back in to duplicate the original. This turns out to be easy. Let’s say that the string that you are reading data into is called big buffer and that your ofstream object is named output file. Then you can do something as simple as this output_file << (big_buffer + "\n");
8. Make sure that the program closes all open files before main executes the return statement.
Please find the code below:::
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main( )
{
ifstream infile;
string line;//for read line
char fileName[20];
while(true){
cout<<"Enter file name :
";
cin>>fileName;
infile.open (fileName); //name of
file here. plz mention Complete path if file is not at root
if (infile.is_open()) //if file
opened
{
cout<<"Enter out file name : ";
cin>>fileName;
ofstream writer
(fileName); //printing data to out file
cout<<"File scan start........"<<endl;
cout<<"Copying data ........"<<endl;
while(
getline(infile, line,'\n') ) { //get row from text file
writer<<line<<endl;
}
infile.close();
//close file
writer.close();
//close file
cout<<"File scan done ........"<<endl;
break;
}
else //if file not found show the
below message
{
cout <<
"Sorry, we could not find the file.Please try again!!!!" <<
endl;
}
}
return 0;
}
words.txt
This is a test string
HelloWorld
copy me if you can
hahah
ahhah
ahahhhahhahahah
output:

copyHere.txt now
This is a test string
HelloWorld
copy me if you can
hahah
ahhah
ahahhhahhahahah
C++ OPEN AND COPY FILES Your task is to create a simple C++ program that prompts...
I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...
Reading and Writing Complete Files in C: The first part of the lab is to write a program to read the complete contents of a file to a string. This code will be used in subsequent coding problems. You will need 3 functions: main(), read_file() and write_file(). The main function contains the driver code. The read_file() function reads the complete contents of a file to a string. The write_file() writes the complete contents of a string to a file. The...
In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....
Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...
Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...
Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...
// Retire.cpp : Defines the entry point for the console application. // program to convery years to retire // sumin Kang // 9/26/17 #include "stdafx.h" #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; int main() { //declare files ifstream inFile; ofstream outFile; // declare constants and variables string name; int age; int ageInRetire; // open files inFile.open("D:\\retire"); outFile.open("D:\\retire"); // get input from user cout << "What is your name?"; getline(inFile, name); cout << "How old are you?";...
nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.// Function prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream jokeFile;ifstream punchlineFile;// Explain the program to the user.cout << "This program will print a joke "<< "and its punch line.\n\n";// Get the joke file name.cout << "Enter the name of...
Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...
Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes, etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...