In: Computer Science
// Program readline.cpp demonstrates how to read a line of text from a file #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string str1, str2, str3, str4; // declares 4 variables ifstream inData; // declares input stream ofstream outData; // declares output stream inData.open("e2_input.txt"); // binds program variable inData to the input file "input.txt" outData.open("e2_output.txt"); // binds program variable outData to the output file "output.txt" // input 4 lines getline(inData,str1); getline(inData,str2); getline(inData,str3); getline(inData,str4); // output 4 lines outData << str4 << endl; outData << str3 << endl; outData << str2 << endl; outData << str1 << endl; // outputs 4 lines inData.close(); outData.close(); return 0; }
Allan Smith John Cooper Zhang Hua Yao Ming
You are required to deliver the following:
How to add two more names with it?
How to add two more names with it?
How to add two more names with it?
How to add two more names with it?
#include<iostream>
#include<fstream>
#include <string>
using namespace std;
int main()
{
string str1, str2, str3, str4; // declares 4 variables
ifstream inData; // declares input stream
ofstream outData; // declares output stream
inData.open("e2_input.txt");
// binds program variable inData to the input file "input.txt"
outData.open("e2_output.txt");
// binds program variable outData to the output file "output.txt"
// input 4 lines
getline(inData, str1);
getline(inData, str2);
getline(inData, str3);
getline(inData, str4);
// output 4 lines
outData << str4 << endl;
outData << str3 << endl;
outData << str2 << endl;
outData << str1 << endl; // outputs 4 lines
// add 2 more names to the output file
string str5 = "John Smith";
string str6 = "Alicia Vaughn";
outData << str5 << endl << str6 << endl;
inData.close();
outData.close();
return 0;
}
********************************************************* SCREENSHOT *******************************************************
CODE SCREENSHOT :
INPUT FILE (e2_input.txt) - This file needs to be created before running the code and this file should be created within the same directory.
OUTPU T FILE (e2_output.txt)