This assignment uses functions, files, and strings. Enough flexibility is provid
ID: 3695326 • Letter: T
Question
This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic 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 first.txt and write your first name 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 your first name 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 last.txt and write your last name 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 your last name 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 flast.txt and write your first name from the file first.txt, and your last name from the file last.txt. 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:
first.txt your first name as written in the file
last.txt your last name as written in the file
flast.txt yourfirstname yourlastname
Note that there is a single space between yourfirstname and yourlastname when you display the contents of the file flast.txt.
You will use a user-defined function to display all the files at once or one at a time.
Close all 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.
The programs in the book and sample programs show you how to read file names as strings and how to convert the strings to the C string, if needed.
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main(){
char firstFileName[20], lastFileName[20], flastFileName[20];
string first, last;
// READING FILE NAME TO STORE FIRST NAME
cout<<"Enter file name to store first name: ";
cin>>firstFileName;
// opening file
ofstream firstOpen(firstFileName);
// reading from keyboard and writing to file
cout<<"Enter your first name: ";
cin>>first;
firstOpen<<first;
firstOpen.close();
// Reading fileName to store last name
cout<<"Enter file name to store last name: ";
cin>>lastFileName;
// opening file
ofstream lastOpen(lastFileName);
// reading from keyboard and writing to file
cout<<"Enter your last name: ";
cin>>last;
lastOpen<<last;
lastOpen.close();
// READING FILE NAME TO STORE FIRST and last NAME
cout<<"Enter file name to store first and last name: ";
cin>>flastFileName;
// opening file
ofstream flastOpen(flastFileName); // to write
ifstream fnameOpen(firstFileName); // to read first name
ifstream lnameOpen(lastFileName); // to read last name
// reading first name
first = "";
last = "";
fnameOpen>>first;
lnameOpen>>last;
flastOpen<<first<<" "<<last;
flastOpen.close();
fnameOpen.close();
lnameOpen.close();
return 0;
}
/*
sample run:
Enter file name to store first name: first.txt
Enter your first name: Pravesh
Enter file name to store last name: laast.txt
Enter your last name: Kumar
Enter file name to store first and last name: flast.txt
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.