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

[C] Given two input files, containing logs of text messages sent between two ind

ID: 3676788 • Letter: #

Question

[C] Given two input files, containing logs of text messages sent between two individuals, 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 ) ); }

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.

In [C] please

Explanation / Answer

GIVEN BELLOW ARE THE CODE BY COMMENTING MAKE YOU EASY UNDERSTAND.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "scanner.h"

#include <time.h>

// variable declaration//

int Message(TXT *, TXT *, int, int);

int Words(TXT *);

void GetMessage(TXT *, TXT *, int, int);

char *Time(int);

void GetTime(char *);

//main method//

int main(int argc, char *argv[]) {

               TXT *person1= fopen(argv[1], "r");

               TXT *person2 = fopen(argv[2], "r");

               TXT *merge = fopen("transcript", "w");

               int output;

               int Wordssize;

               int time1 = readInt(person1);

               int time2 = readInt(person2);

               int msg;                                                                                          

               char *formatTime;          

//check for message of person 1 and person 2//

               while (!feof(person1) && !feof(person2)) {

                              output = Message(person1, person2, time1, time2);

                              if (output == 1) {

                                             Wordssize = Words(person1);

                                            

                                             msg = 0;

                                             GetMessage(person1, *merge, Wordssize, msg);

                                             formatTime = Time(time1);

                                             fprintf(*merge, " %s ", formatTime);

                                             time1 = readInt(person1);

                              }

                              else if (output == 2) {

                                             Wordssize = Words(person2);

                                             msg = 1;

                                             GetMessage(person2, *merge, Wordssize, msg);

                                             fprintf(*merge, "%*c", 35, ' ');

                                             formatTime = Time(time2);

                                             fprintf(*merge, " %s ", formatTime);

                                             time2 = readInt(person2);

                              }

               }

              

               while (!feof(person1)) {

                              Wordssize = Words(person1);

                                                            msg = 0;

                              GetMessage(person1, *merge, Wordssize, msg);

                                 formatTime = Time(time1);

                              fprintf(*merge, " %s ", formatTime);

                              time1 = readInt(person1);

               }

               while (!feof(person2)) {

                              Wordssize = Words(person2);

                              msg = 1;

                              GetMessage(person2, *merge, Wordssize, msg);

                              fprintf(*merge, "%*c", 35, ' ');

                              formatTime = Time(time2);

                              fprintf(*merge, " %s ", formatTime);

                              time2 = readInt(person2);

               }

                              return 0;

}

// check messages by time for person1 and person2 to send which message send first//

int Message(TXT *person1, TXT *person2, int time1, int time2)

{              if (time1 < time2)

{

return 1;

               }

               else if (time2 < time1)

{             return 2;

               }

               else {

                              return 0;

               }

}

int Words(TXT *data) {

               int Wordssize = readInt(data);

               return Wordssize;

}

void GetMessage (TXT *data, TXT *merge, int Wordssize, int msg) {

               char buf[31];

               buf[0] = '';

               char *word;

               int bufLength = 0;

               int j;

               int i;

               buf[30] = '';

               for (j = 0; j < Wordssize; j++) {

                              word = readToken(data);

                              bufLength += strlen(word);

                              bufLength++;                                                                                               

// count the character for space//

                              if (bufLength >= 31) {

                                             if (msg) {

                                            

                                                            fprintf(*merge, "%*c", 35, ' ');

                                             }

                                             fprintf(*merge, "%s ", buf);

                                             for (i = 0; i <= strlen(buf); i++) {                                  

                                                            buf[i] = 0; // buf empty //

                                             }                                                                                                       

                                             buf[29] = '';

                                             bufLength = 0;

                                             strcat(buf, word);

                                             strcat(buf, " ");

                              }

                              else if (bufLength < 31) {

                                             strcat(buf, word);

                                             strcat(buf, " ");

                              }

               }

               if (msg) {

                              fprintf(*merge, "%*c", 35, ' ');

               }

               fprintf(*merge, "%s ", buf);

}

char *Time(int sec) {

time_t epoch_time = (time_t) sec;

return asctime( localtime( &epoch_time ) );

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote