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

CS 100 Project Four - Spring 2018 This program assumes that the text message log

ID: 3726495 • Letter: C

Question

CS 100 Project Four - Spring 2018 This program assumes that the text message logs maintained in your phone are stored in two separate files. The first file is all the incoming text messages that you received, the second is all the outgoing text messages that you sent. This program reads these two files and prints your text message history in chronological order (oldest to newest). takes two command-line arguments that represent the log files for all i ncoming text messages a nd all outgoing text messages. A sample execution is: /a.out datafilel datafile2 Page two contains a sample execution of the program. The two log files capture a conversation from the first day of school last August with your mother. Look at the sample log files and the program execution before reading any further The format for these two log files is shown below: An EPOCH-time is an integer representing the number of seconds since January 1, 1970. 1519408800 translates to noon on Friday, February 23, 2018 (Central). To convert an EPOCH-time integer to a readable date, use the function shown below. /1 this function takes an integer representing a time in seconds and // returns a formatted string (ending with a newline) containing the date readableTime (int sec) time t epoch time (time t) sec ; return asctime localtime( &epoch;_time The website associated with any EPOCH-time. allows you to generate EPOCH-times for any date, and see the date files and prints out the text message conve ersations in chronological order (from the oldest message sent/received to the newest). The required format for your output is: The first 25 characters on a line are a readable version of the EPOCH time. The function readableTime (shown above) has a newline character as the very last character of the string it generates. Make sure you remove this trailing newline character (simply replace it with a '10' character). The next field in the output lines is the phone number that texted you (or you texted). The text message itself is printed in a block that does not exceed 40 characters. If there are more than 40 characters in a text message, wrap the message to the next line. The basic algorithm for merging two files into a single output is shown below. For this project, you can assume that no two text messages will ever have the same timestam Read timel from filel Read time2 from file2 While not end-of-file-for-filel AND not end-of-file-for-file2) If timel

Explanation / Answer

#include<iostream>
#include <fstream>
#include<time.h>
using namespace std;

// Function to convert second to day, month, date, hour, minute, second
char *readableTime(long int sec)
{
// Converts seconds to time format
time_t epoch_time = (time_t)sec;
// Convert to character format
return asctime(localtime(&epoch_time));
}// End of function

// Function to read file contents
void readFile()
{
// ifstream objects created to read data from files
ifstream rFile1, rFile2;
// Open the files for reading
rFile1.open("Data1_in.txt");
rFile2.open("Data1_out.txt");

string data1, data2;
long int time1, time2;
string message1, message2;
string phoneNo1, phoneNo2;

// Reads the data and stores it in variables
// Reads time from both the files
rFile1>>time1;
rFile2>>time2;
// Reads phone number from both the files
rFile1>>phoneNo1;
rFile2>>phoneNo2;
// Calls the function to convert second to date and time format
data1 = readableTime(time1);
data2 = readableTime(time2);
// Reads the message from both the files
getline(rFile1, message1);
getline(rFile2, message2);

// Loops till end of the file for both the files
while(!rFile1.eof() && !rFile2.eof())
{
// Checks the date
if(data1.compare(data2) < 0)
{
// Displays the time, phone number and message for Data1_in.txt
cout<<" "<<data1<<" From "<<phoneNo1.substr(0, 3)<<"-"<<phoneNo1.substr(3, 3)<<"-"<<phoneNo1.substr(6)<<" | "<<message1.substr(3);
// Reads next data from Data1_in.txt
rFile1>>time1;
rFile1>>phoneNo1;
data1 = readableTime(time1);
getline(rFile1, message1);
}// End of if condition
// Otherwise
else
{
// Displays the time, phone number and message for Data1_out.txt
cout<<" "<<data2<<" To "<<phoneNo2.substr(0, 3)<<"-"<<phoneNo2.substr(3, 3)<<"-"<<phoneNo2.substr(6)<<" | "<<message2.substr(2);
// Reads next data from Data1_out.txt
rFile2>>time2;
rFile2>>phoneNo2;
data2 = readableTime(time2);
getline(rFile2, message2);
}// End of else
}//End of while loop

// Loops till end of remaining part of the Data1_in.txt file
while(!rFile1.eof())
{
// Displays the time, phone number and message for Data1_in.txt
cout<<" "<<data1<<" From "<<phoneNo1.substr(0, 3)<<"-"<<phoneNo1.substr(3, 3)<<"-"<<phoneNo1.substr(6)<<" | "<<message1.substr(3);
// Reads next data from Data1_out.txt
rFile1>>time1;
rFile1>>phoneNo1;
data1 = readableTime(time1);
getline(rFile1, message1);
}// End of while loop

// Loops till end of remaining part of the Data1_out.txt file
while(!rFile2.eof())
{
// Displays the time, phone number and message for Data1_out.txt
cout<<" "<<data2<<" To "<<phoneNo2.substr(0, 3)<<"-"<<phoneNo2.substr(3, 3)<<"-"<<phoneNo2.substr(6)<<" | "<<message2.substr(2);
// Reads next data from Data1_out.txt
rFile2>>time2;
rFile2>>phoneNo2;
data2 = readableTime(time2);
getline(rFile2, message2);
}// End of while loop
//Close file
rFile1.close();
rFile2.close();
}//End of function

// main function definition
int main()
{
// Calls the function to read both the file contents
readFile();
}// End of main function

Sample Output:

Wed Aug 23 17:30:00 2017
From 205-555-1234 | Hello. This is your mother. Make sure to get to class early today.
Wed Aug 23 17:30:10 2017
From 205-555-1234 | And make sure you get a gook breakfast.
Wed Aug 23 17:30:20 2017
From 205-555-1234 | Don't sit in the back. Talk to the people around you.
Wed Aug 23 17:30:30 2017
From 205-555-1234 | Please call your grandother and tell her about your day later on.
Wed Aug 23 17:30:40 2017
From 205-555-1234 | And let me know about your classes. Do you like your teachers? Does this feel like the right major for you? Have you signed up for co-op yet? Don't stay up too late.
Wed Aug 23 18:29:50 2017
To 205-555-1234 | k
Wed Aug 23 18:30:00 2017
From 205-555-1234 | I hope you are not texting in class. Sit up front and take notes.
Thu Aug 24 07:30:10 2017
From 205-555-1234 | WHAT?
Wed Aug 23 18:30:10 2017
To 205-555-1234 | k
Thu Aug 24 07:30:00 2017
To 205-555-1234 | classes were fine, going out with friends now.
Thu Aug 24 10:29:00 2017
To 205-555-1234 | back home, met some new friends, had fun.