Project Overview: Given two input files, containing logs of text messages sent b
ID: 3676763 • 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). A good algorithm for merging two input files into a single file is shown below: Read an initial POSIX-time from each input file While there is data left in both input files (have not reached EOF on either file) Process the line with the smallest POSIX-time Read the next POSIX-time from that file If you have not yet reached EOF on the first input file, process the rest of the lines in that file If you have not yet reached EOF on the second input file, process the rest of the lines in that file
The required output format for the 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
the below written program was displayed with numberins at the margin side, so,whenever you execute the program ,if you encounter any error ,in the output editor window,the error will be displayed along with the line number.so that it will be easier to trace out the error and rectify it and make it get executable.
/*
This program takes two input files and merges them into
one output file in the format of a text message conversation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scanner.h"
#include <time.h>
int chooseFileToPrint(FILE *, FILE *, int, int);
int getNumberOfWords(FILE *);
void printMessage(FILE *, FILE *, int, int);
char *readableTime(int);
void printTime(char *);
int main(int argc, char *argv[]) {
FILE *data1 = fopen(argv[1], "r");
FILE *data2 = fopen(argv[2], "r");
FILE *output = fopen("transcript", "w");
int fileToPrint;
int numberOfWords;
int time1 = readInt(data1);
int time2 = readInt(data2);
int isDataTwo; // variable used to check whether or not to align the text left
char *formattedTime;
while (!feof(data1) && !feof(data2)) {
fileToPrint = chooseFileToPrint(data1, data2, time1, time2);
if (fileToPrint == 1) {
numberOfWords = getNumberOfWords(data1);
isDataTwo = 0;
printMessage(data1, output, numberOfWords, isDataTwo);
formattedTime = readableTime(time1);
fprintf(output, " %s ", formattedTime);
time1 = readInt(data1);
}
else if (fileToPrint == 2) {
numberOfWords = getNumberOfWords(data2);
isDataTwo = 1;
printMessage(data2, output, numberOfWords, isDataTwo);
fprintf(output, "%*c", 35, ' ');
formattedTime = readableTime(time2);
fprintf(output, " %s ", formattedTime);
time2 = readInt(data2);
}
}
while (!feof(data1)) {
numberOfWords = getNumberOfWords(data1);
isDataTwo = 0;
printMessage(data1, output, numberOfWords, isDataTwo);
formattedTime = readableTime(time1);
fprintf(output, " %s ", formattedTime);
time1 = readInt(data1);
}
while (!feof(data2)) {
numberOfWords = getNumberOfWords(data2);
isDataTwo = 1;
printMessage(data2, output, numberOfWords, isDataTwo);
fprintf(output, "%*c", 35, ' ');
formattedTime = readableTime(time2);
fprintf(output, " %s ", formattedTime);
time2 = readInt(data2);
}
return 0;
}
int chooseFileToPrint(FILE *data1, FILE *data2, int time1, int time2) { // find which file has the earliest message to print
if (time1 < time2) {
return 1;
}
else if (time2 < time1) {
return 2;
}
else {
return 0;
}
}
int getNumberOfWords(FILE *data) {
int numberOfWords = readInt(data);
return numberOfWords;
}
void printMessage (FILE *data, FILE *output, int numberOfWords, int isDataTwo) {
char buffer[31];
buffer[0] = '';
char *word;
int bufferLength = 0;
int j;
int i;
buffer[30] = '';
for (j = 0; j < numberOfWords; j++) {
word = readToken(data);
bufferLength += strlen(word);
bufferLength++; // if we don't account for the space between words it will print too many characters
if (bufferLength >= 31) {
if (isDataTwo) {
fprintf(output, "%*c", 35, ' ');
}
fprintf(output, "%s ", buffer);
for (i = 0; i <= strlen(buffer); i++) { // empty the buffer
buffer[i] = 0;
}
buffer[29] = '';
bufferLength = 0;
strcat(buffer, word);
strcat(buffer, " ");
}
else if (bufferLength < 31) {
strcat(buffer, word);
strcat(buffer, " ");
}
}
if (isDataTwo) {
fprintf(output, "%*c", 35, ' ');
}
fprintf(output, "%s ", buffer);
}
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 ) );
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.