Question: C++ coding- Follow the instructions /*=========================================================== Program Name- Reading from and writing to file Program Description- This program demonstrates how to read from and write
C++ coding- Follow the instructions
/*===========================================================
Program Name- Reading from and writing to file Program Description- This program demonstrates how to read from and write to file. It reads a name list from a file, sort the list and then writes the sorted list to another file.
Instructions- 1. Imitate the output sample, its prompts and output, as closely as you can. 2. You must define the declared function writeNamesToFile(..). 3. When you complete #2, test your code in the main(). 4. Add your code where it says "Write your code below...". Do NOT change or remove any existing statements.
Note- For sorting, you are required to use the library function. =========================================================== */ #include
using namespace std;
/** For every name in vector string A, put last name, first name. */ void lastNameFirstPlusComma(vector
// Prints out vector string A void printStringVector(const vector
// Reads names into vector "names" from the file "in". void readNamesFromFile(vector
/* =========================================== You must define the following function after main(). <<< Write your code below... >>> =========================================== */
// Writes names in vector "names" to the file "out" void writeNamesToFile(const vector
int main() { cout << "=================================================== "; cout << "This program reads a name list from a file, sorts the list and "; cout << "then writes the sorted list to another file. "; cout << "=================================================== ";
string inFile, outFile; // for input/output file name. ifstream inF; // for input file stream. ofstream outF; // for output file stream.
cout << "Enter the file name of the name list ==> "; cin >> inFile; inF.open(inFile.c_str()); if (inF.fail()) { cout << "Error opening " << inFile << "! "; return 1; }
vector
cout << " This is the original name list in the file..."; cout << " ___________________ "; printStringVector(nameList);
cout << " ==> Put the last name first..."; lastNameFirstPlusComma(nameList);
cout << " ___________________ "; printStringVector(nameList);
/* ============================================ You must sort the above nameList and then write it to a file, whose name should be input from the user. <<<<<< Write your code below... >>>>> ============================================ */ cout << endl;
return 0; }
//======================================= void lastNameFirstPlusComma(vector
for (size_t i = 0; i < A.size(); i++) { istringstream input(A[i]); input >> s1 >> s2; A[i] = s2 + ", " + s1; } }
void readNamesFromFile(vector
getline(in, name); while (!in.fail()) { names.push_back(name); getline(in, name); }; }
void printStringVector(const vector
/* Output sample
===================================================== This program reads a name list from a file, sorts the list and then writes the sorted list to another file. =====================================================
Enter the file name of the name list ==> 5_names.txt
The original name list in the file... ___________________ Jane Doe Maggie Walter John Stone Carol Adams Kevin Engles
==> Put last name, first name... ___________________ Doe, Jane Walter, Maggie Stone, John Adams, Carol Engles, Kevin
==> Sorting the name list...
Enter the file name for the sorted list ==> output.txt Check the sorted name list in the file output.txt!
*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
