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

This assignment uses functins, files, and strings. Enough flexibility is provide

ID: 3774956 • Letter: T

Question

This assignment uses functins, files, and strings. Enough flexibility is provided for you to apply your knowledge of C++ programing to develop your solution.

Develop a functional flowchart and then write a C++ program to solve the following problem.

Create a text file named course.txt and write the ELET in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the course from the keyboard. The process of the file creation (name of the file, mode for opening the file, and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function).

Create a text file named number.txt and write the course number 2300 in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the course number from the keyboard. The process of the file creation (name of the file, mode for opening the file and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function)

Create a text file named e2300.txt and write the course from the file course.txt, and the course number from the file number.txt into the newly created file. You will be reading the name of the file from the keyboard as a string, using the string class. The process of the file creation (name of the file, mode for opening the file and testing the open success is done in a user-defined function. The function communicates the status of file open to the calling function)

At this time, each file will have one line of text in it.

Prepare the files for reading.

Display the results of the three files in the order shown below:

course.txt                      the course as written in the file (ELET)

number.txt                    the course number as written in the file (2300)

cnumber.txt                   the course and number as written in the file (ELET 2300)

Note that there is a single space between the course and the course number when you display the contents of the file cnumber.txt. There is a blank line between each line of output.

You will use a user-defined function to display all the files at once or one at a time.

Close all the files. You will use a user-defined function that closes each file separately or all at once.

Run the program again and use your choice of the filenames, and different names, different than what you used above and repeat the process.

Explanation / Answer

Here you can get an idea of how to use the code:

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

int main()
{
string coursefilename, coursecontents, numberfilename, numbercontents;
string direcpath= ""; //specify the directory path where the file is placed
string numberdirecpath= ""; //specify the directory path where the file is placed

string writefilename;
string writefilepath; //specify the directory path where the file is placed

//user defined function
coursefilename = readcoursefilename(direcpath);
coursecontents = readcoursefileContents(direcpath, coursefilename);

numberfilename = readcoursefilename(direcpath);
numbercontents = readcoursefileContents(numberdirecpath, numberfilename);

fstream file; // fstream is used (for reading and writing)
string text;

ofstream outFile(writefilepath+writefilename); // opens file for writing

ouFile << coursefilename + ("\t);
ouFile << coursecontents + (" );

ouFile << numberfilename + ("\t);
ouFile << numbercontents + (" );

outFile.close()

  
}

public string readcoursefilename(string direcpath)
{
string filename;
struct dirent *ent;
if ((dir = opendir (direcpath)) != NULL) {
/* store the course file name in the directory */
while ((ent = readdir (direcpath)) != NULL && ent->d_name.equals(.txt)) {
filename= ent->d_name;
}
closedir (direcpath);
}
return filename;
}


public string readcoursefileContents(string direcpath,string filename)
{
string filecontents;

std::ifstream ifs(direcpath + filename);
std::string filecontents( (std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()) );

return filecontents;
}

public string readnumberfilename(string numberdirecpath)
{
string filename;
struct dirent *ent;
if ((dir = opendir (numberdirecpath)) != NULL) {
/* store the course file name in the directory */
while ((ent = readdir (numberdirecpath)) != NULL && ent->d_name.equals(.txt)) {
filename= ent->d_name;
}
closedir (numberdirecpath);
}
return filename;
}


public string readcoursefileContents(string numberdirecpath,string numberfilename)
{
string filecontents;

std::ifstream ifs(numberdirecpath + numberfilename);
std::string filecontents( (std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()) );

return filecontents;
}

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