Project Overview: Given two input files, containing logs of text messages sent b
ID: 3676653 • Letter: P
Question
Project Overview: Given two input files, containing logs of text messages sent between two individuals, you should merge them into a single file that contains a formatted transcript of their conversation. On the next page you can see two sample input files and the output file that you must generate from this input. Look at this example before reading any further. The first input file is the messages sent from Person #1 to Person #2. The second input file is the messages sent from Person #2 to Person #1. Each file consists of one or more lines, where each line contains a single text message. The format is: A sample input line would be: 1457737200 9 Toto, I’ve a feeling we’re not in Kansas anymore. The POSIX-time is an integer value that represents the number of seconds since January 1, 1970. 1457737200 translates to Friday, March 11, 2016 at 5:00pm (Central). To convert this integer to a readable date, use the function given below. char *readableTime(int sec) { // this function takes an integer representing a time in seconds // it returns a formatted string that contains the date //the formatted string includes a newline character at the end time_t epoch_time = (time_t) sec; return asctime( localtime( &epoch_time ) ); } You should merge the two files together in chronological order (from smallest POSIX-time to largest). transcript file is specified below: • Text messages for Person #1 start at the left margin and are displayed in a 30-character width, longer messages should wrap to multiple lines. After each message, display the date (right justified, in a readable format) on a separate line. • A 5-character blank space (buffer) exists between Person #1 and Person #2’s text messages. With this buffer, a text message for Person #2 would start 35 spaces from the left margin. •A 30-character width for is also used for Person #2’s text messages, with each message followed by the date.Explanation / Answer
Comments added
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
ofstream out_data("output.txt");
std::ifstream readfile("input1.txt");//reding message from text file
std::ifstream readfile1("input2.txt");//reding message from text file
std::string eachline;
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
string line;
while (iss >> line)//reading each value from file
{
string words[100];
int count = 0;
stringstream ssin(line);
while (ssin.good(){
ssin >> words[i];
if(words[i] == ""){
break;
}
++count;
}
//outputting message to file
//spaces adding
for(int i=0;i<30;i++){
out_data<<" ";
}
//writing words
for(int i=0;i<count;i++){
out_data<<words[i]<<" ";
}
out_data<<" ";
}
}
while (std::getline(readfile1, eachline))
{
std::istringstream iss(eachline);
string line;
//printing extra empty 5 lines
for(iny i=0;i<5;i++){
out_data<<" ";
}
while (iss >> line)//reading each value from file
{
string words[100];
int count = 0;
stringstream ssin(line);
while (ssin.good(){
ssin >> words[i];
if(words[i] == ""){
break;
}
++count;
}
//outputting message to file
//spaces adding
for(int i=0;i<30;i++){
out_data<<" ";
}
//writing words
for(int i=0;i<count;i++){
out_data<<words[i]<<" ";
}
out_data<<" ";
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.