Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write C^++ code that takes a text file, and inserts it at the beginning of anoth

ID: 3819046 • Letter: W

Question

Write C^++ code that takes a text file, and inserts it at the beginning of another text file. To 'a We called one.txt contains this: I do not know what to do! and a file called two.txt contains When nothing works, then your code should change two.txt to contain When nothing works, I do not know what to do! You do not have to write a complete program, as long as all necessary logic and computation is clearly illustrated. For example, no need to write #include statements, or the using namespace std statement. However, your code must reflect the true capabilities given by the C^++ file I/O functions. That means, you must demonstrate in your code your knowledge of what C^++ iostreams can and cannot do. Other rules: 1. You can assume file names to be one. txt and two. txt. 2. You must change the second file, not the first. That is, the first file must be added to the beginning of the second file. 3. After the changes, the second file must have the same name (two. txt). 4. All file I/O operations must be shown and must be legal in C^++. 5. FYI, you are not limited to using just two files while solving the problem. Temporary files can be used. 6. Both files might contain any number of lines. The files shown in the example above are for illustrating the problem.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
string file_data1,file_data2,file_data3; // Strings to collect data
ifstream ReadFile1,ReadFile2; // 2 Input Files
ofstream tempFile; // 1 output file
ReadFile1.open("two.txt"); // Open 1 Input File
tempFile.open("temp.txt"); // Open and Create Output File
char output; // Receive Characters
int i=0;
if (ReadFile1.is_open()) // Check File 1 is open or not
{
while (!ReadFile1.eof())
{
ReadFile1 >>std::noskipws>> output; // Read data in output
file_data1=file_data1+output; // String Concat
i++;
}
file_data1[i-1]=''; // put null character at the end of string
}
ReadFile1.close();
tempFile<<file_data1<<" "; // Writing file 1 data into temp file
i=0;
ReadFile2.open("one.txt"); // Open Another Input File
if (ReadFile2.is_open()) // Check File 2 is open or not
{
while(!ReadFile2.eof())
{
ReadFile2>> std::noskipws>>output; // Read data in output
file_data2=file_data2+output; // String Concat
i++;
}
file_data2[i-1]=''; // put null character at the end of string
}
ReadFile2.close(); // Close File 2
tempFile<<file_data2; // Writing file 2 data into temp file
tempFile.close(); // Close Temp File
remove("two.txt"); // revmove two.txt file
rename( "temp.txt" , "two.txt" ); // rename temp.txt to two.txt
return 0;
}

Rules Covered

Covered Rule 1

Covered Rule 2

Covered Rule 3

Covered Rule 4

Covered Rule 5

Covered Rule 6

File one.txt:

I do not know what to do!

File two.txt

When nothing works,

Output File two.txt:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote