Write a program to copy an existing text file from your hard disk to another file that you will call: Your Last Name.txt, e.g. if your last name was Smith, the output file name would be Smith.txt. You can create a text file and add two or three lines of text to it. You may use the attached program as your program or write your own. Please note the inclusion of at the top of the program. Also pay attention to the open and close statements in the program. // File: CopyFile.cpp // Copies file InData.txt to file OutData.txt #include // for the definition of EXIT_FAILURE #include // required for external file streams #include using namespace std; // Associate stream objects with external file names #define inFile "InData.txt" #define outFile "OutData.txt" // Functions used ... // Copies one line of text int copyLine(ifstream&, ofstream&); int main() { // Local data ... int lineCount; // output: number of lines processed ifstream ins; // ins is as an input stream ofstream outs; // outs is an output stream // Open input and output file, exit on any error. ins.open(inFile); // connects ins to file inFile if (ins.fail ()) { cerr << "*** ERROR: Cannot open " << inFile << " for input." << endl; return EXIT_FAILURE; // failure return } // end if outs.open(outFile); // connect outs to file outFile if (outs.fail()) { cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl; return EXIT_FAILURE; // failure return } // end if // Copy each character from inData to outData. lineCount = 0; do { if (copyLine(ins, outs) != 0) lineCount++; } while (!ins.eof()); // Display a message on the screen. cout << "Input file copied to output file." << endl; cout << lineCount << " lines copied." << endl; ins.close(); // close input file stream outs.close(); // close output file stream return 0; // successful return } // Copy one line of text from one file to another // Pre: ins is opened for input and outs for output. // Post: Next line of ins is written to outs. // The last character processed from ins is ; // the last character written to outs is . // Returns: The number of characters copied. int copyLine (ifstream& ins, // IN: ins stream ofstream& outs) // OUT: outs stream { // Local data ... const char NWLN = '\n'; // newline character char nextCh; // inout: character buffer int charCount = 0; // number of characters copied // Copy all data characters from stream ins to // stream outs. ins.get(nextCh); while ((nextCh != NWLN) && !ins.eof()) { outs.put(nextCh); charCount++; ins.get (nextCh); } // end while // If last character read was NWLN write it to outs. if (!ins.eof()) { outs.put(NWLN); charCount++; } return charCount; } // end copyLine

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Write a program to copy an existing text file from your hard disk to another file that you will call: Your Last Name.txt, e.g. if your last name was Smith, the output file name would be Smith.txt. You can create a text file and add two or three lines of text to it. You may use the attached program as your program or write your own. Please note the inclusion of at the top of the program. Also pay attention to the open and close statements in the program. // File: CopyFile.cpp // Copies file InData.txt to file OutData.txt #include // for the definition of EXIT_FAILURE #include // required for external file streams #include using namespace std; // Associate stream objects with external file names #define inFile "InData.txt" #define outFile "OutData.txt" // Functions used ... // Copies one line of text int copyLine(ifstream&, ofstream&); int main() { // Local data ... int lineCount; // output: number of lines processed ifstream ins; // ins is as an input stream ofstream outs; // outs is an output stream // Open input and output file, exit on any error. ins.open(inFile); // connects ins to file inFile if (ins.fail ()) { cerr << "*** ERROR: Cannot open " << inFile << " for input." << endl; return EXIT_FAILURE; // failure return } // end if outs.open(outFile); // connect outs to file outFile if (outs.fail()) { cerr << "*** ERROR: Cannot open " << outFile << " for output." << endl; return EXIT_FAILURE; // failure return } // end if // Copy each character from inData to outData. lineCount = 0; do { if (copyLine(ins, outs) != 0) lineCount++; } while (!ins.eof()); // Display a message on the screen. cout << "Input file copied to output file." << endl; cout << lineCount << " lines copied." << endl; ins.close(); // close input file stream outs.close(); // close output file stream return 0; // successful return } // Copy one line of text from one file to another // Pre: ins is opened for input and outs for output. // Post: Next line of ins is written to outs. // The last character processed from ins is ; // the last character written to outs is . // Returns: The number of characters copied. int copyLine (ifstream& ins, // IN: ins stream ofstream& outs) // OUT: outs stream { // Local data ... const char NWLN = '\n'; // newline character char nextCh; // inout: character buffer int charCount = 0; // number of characters copied // Copy all data characters from stream ins to // stream outs. ins.get(nextCh); while ((nextCh != NWLN) && !ins.eof()) { outs.put(nextCh); charCount++; ins.get (nextCh); } // end while // If last character read was NWLN write it to outs. if (!ins.eof()) { outs.put(NWLN); charCount++; } return charCount; } // end copyLine
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education