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

C programming Your program reads from two files and prints out the text message

ID: 3725313 • Letter: C

Question

C programming

Your program reads from two files and prints out the text message conversations in chronological order (from oldest to newest message sent/received). 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 below) 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 ‘’ character).

The next field in the output lines is the phone number that texted you (or you texted).

The test 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.

Use at least three functions, the readableTime function does not count as one of the three functions.

You can assume that no two text messages will ever have the same time stamp.

Char *readableTime(int sec){

                time_t epoch_time = (time_t) sec;

                return asctime( localtime( &epoch_time));

}

Explanation / Answer

main.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"scanner.h"
#include<time.h>


char *readableTime(int sec) {
time_t epoch_time = (time_t) sec;
return asctime( localtime( &epoch_time ) );
}

//Take the string and find the length and subtract that length from
//the total width in order to find how many spaces you need to indent.
void indent(int dateLen, FILE *fw){
   int a, spaces;
   spaces = 30 - dateLen;
   for (a=0; a<=spaces; a++){
       fprintf(fw," ");
   }
}

//This function indents the distance for the second person.
void indentResp(FILE *fw) {
   int k = 0;
   for (k = 0; k < 35; k++) {
       fprintf(fw, " ");
   }
}

//This function closes all of the files
void close(FILE *fw, FILE *f1, FILE *f2) {
   fclose(fw);
   fclose(f1);
   fclose(f2);
}

int main(int argc, char **argv){
   //Opens all of the files necessary for this program
   FILE *f1 = fopen(argv[1], "r");
   FILE *f2 = fopen(argv[2], "r");
   FILE *fw = fopen("transcript", "w");

   //Creates a variable for each individual word to be stored in
   //before it is printed
   char *word;

   //Reads in the first date stamp for each file
   int num1 = readInt(f1);
   int num2 = readInt(f2);
   int words1 = 0, words2 = 0;
   int i=0;
   int length;

   while (!feof(f2) && !feof(f1)){
       //The if statement decides which text was sent first by taking the
       //smaller of the numbers and printing that line first
       if (num1>num2){
           indentResp(fw);
           //Reads the amount of words that are present in the message
           words2 = readInt(f2);
           //Reads the first word of the text and finds the initial value
           //for the length
           i = 0;

           word = readToken(f2);
           length = strlen(word);
           //Itterates the number of times a word appears
           while (i < words2 - 1) {
               if (length <= 30) {
                   fprintf(fw, "%s ", word);
                   //Reads in the next word and adds its length
                   word = readToken(f2);
                   length = length + strlen(word) + 1;
                   i++;
               }
               if (length > 30) {
                   //Prints a new line and assigns and sets length back
                   //equal to zero
                   fprintf(fw, " ");
                   indentResp(fw);
                   length = strlen(word);
               }
           }
           fprintf(fw, "%s", word);
           //Print a new line and indent for the date
           fprintf(fw, " ");
           indentResp(fw);
           indent(strlen(readableTime(num2)), fw);
           //Prints the time stamp at the end of the message
           fprintf(fw, "%s ", readableTime(num2));
           //Gets the next time stamp for num1 to compare with num1 to see
           //which should be printed next
           num2 = readInt(f2);
       }
       if (num2>num1){
           //Reads the amount of words that are present in the message
           words1 = readInt(f1);
           //Reads the first word of the text and finds the initial value
           //for the length
           i = 0;

           word = readToken(f1);
           length = strlen(word);
           //Itterates the number of times a word appears
           while (i < words1 - 1) {
               if (length <= 30) {
                   fprintf(fw, "%s ", word);
                   //Reads in the next word and adds its length
                   word = readToken(f1);
                   length = length + strlen(word) + 1;
                   i++;
               }
               if (length > 30) {
                   //Prints a new line and assigns and sets length back
                   //equal to zero
                   fprintf(fw, " ");
                   length = strlen(word);
               }
           }
           fprintf(fw, "%s", word);
           //Print a new line and indent for the date
           fprintf(fw, " ");
           indent(strlen(readableTime(num1)), fw);
           //Prints the time stamp at the end of the message
           fprintf(fw, "%s ", readableTime(num1));
           //Gets the next time stamp for num1 to compare with num1 to see
           //which should be printed next
           num1 = readInt(f1);
       }
   }

   while (!feof(f2)) {
       indentResp(fw);
       //Reads the amount of words that are present in the message
       words2 = readInt(f2);
       //Reads the first word of the text and finds the initial value
       //for the length
       i = 0;

       word = readToken(f2);
       length = strlen(word);
       //Itterates the number of times a word appears
       while (i < words2 - 1) {
           if (length <= 30) {
               fprintf(fw, "%s ", word);
               //Reads in the next word and adds its length
               word = readToken(f2);
               length = length + strlen(word) + 1;
               i++;
           }
           if (length > 30) {
               //Prints a new line and assigns and sets length back
               //equal to zero
               fprintf(fw, " ");
               indentResp(fw);
               length = strlen(word);
           }
       }
       fprintf(fw, "%s", word);
       //Print a new line and indent for the date
       fprintf(fw, " ");
       indentResp(fw);
       indent(strlen(readableTime(num2)), fw);
       //Prints the time stamp at the end of the message
       fprintf(fw, "%s ", readableTime(num2));
       //Gets the next time stamp for num1 to compare with num1 to see
       //which should be printed next
       num2 = readInt(f2);
   }

   while (!feof(f1)) {
       //Reads the amount of words that are present in the message
       words1 = readInt(f1);
       //Reads the first word of the text and finds the initial value
       //for the length
       i = 0;

       word = readToken(f1);
       length = strlen(word);
       //Itterates the number of times a word appears
       while (i < words1 - 1) {
           if (length <= 30) {
               fprintf(fw, "%s ", word);
               //Reads in the next word and adds its length
               word = readToken(f1);
               length = length + strlen(word) + 1;
               i++;
           }
           if (length > 30) {
               //Prints a new line and assigns and sets length back
               //equal to zero
               fprintf(fw, " ");
               length = strlen(word);
           }
       }
       fprintf(fw, "%s", word);
       //Print a new line and indent for the date
       fprintf(fw, " ");
       indent(strlen(readableTime(num1)), fw);
       //Prints the time stamp at the end of the message
       fprintf(fw, "%s ", readableTime(num1));
       //Gets the next time stamp for num1 to compare with num1 to see
       //which should be printed next
       num1 = readInt(f1);
   }

   close(fw, f1, f2);

   return 0;
}


scanner.c

#include <stdio.h>
#include <stdlib.h> /* for exit, malloc, realloc */
#include <ctype.h>   /* for isspace */
#include <string.h> /* for strdup */

#include "scanner.h"


static void skipWhiteSpace(FILE *);
static char convertEscapedChar(int);
static void *allocateMsg(size_t,char *);
static void *reallocateMsg(void *,size_t,char *);

/********** public functions **********************/

int
readInt(FILE *fp)
    {
    int x,result;
    result = fscanf(fp,"%d",&x);
    if (result == EOF)
        {
        return 0;
        }
    if (result == 0)
        {
        fprintf(stderr,"SCAN ERROR: attempt to read an integer failed ");
        fprintf(stderr,"offending character was <%c> ",fgetc(fp));
        exit(1);
        }
    return x;
    }

double
readReal(FILE *fp)
    {
    int result;
    double x;
    result = fscanf(fp," %lf",&x);
    if (result == EOF)
        {
        return 0.0;
        }
    if (result == 0)
        {
        fprintf(stderr,"SCAN ERROR: attempt to read a real number failed ");
        fprintf(stderr,"offending character was <%c> ",fgetc(fp));
        exit(2);
        }
    return x;
    }

char
readChar(FILE *fp)
    {
    int result;
    char x;
    result = fscanf(fp," %c",&x);
    if (result == EOF)
        {
        return -1;
        }
    if (result == 0)
        {
        fprintf(stderr,"SCAN ERROR: attempt to read a non-whitespace character failed ");
        fprintf(stderr,"offending character was <%c> ",fgetc(fp));
        exit(2);
        }
    return x;
    }

char
readRawChar(FILE *fp)
    {
    int result;
    char x;
    result = fscanf(fp,"%c",&x);
    if (result == EOF)
        {
        return -1;
        }
    if (result == 0)
        {
        fprintf(stderr,"SCAN ERROR: attempt to read a raw character failed ");
        fprintf(stderr,"offending character was <%c> ",fgetc(fp));
        exit(2);
        }
    return x;
    }

char *
readString(FILE *fp)
    {
    int ch,index;
    char *buffer;
    int size = 512;

    /* advance to the double quote */

    skipWhiteSpace(fp);

    ch = fgetc(fp);
    if (ch == EOF) return 0;

    /* allocate the buffer */

    buffer = allocateMsg(size,"readString");

    if (ch != '"')
        {
        fprintf(stderr,"SCAN ERROR: attempt to read a string failed ");
        fprintf(stderr,"first character was <%c> ",ch);
        exit(4);
        }

    /* toss the double quote, skip to the next character */

    ch = fgetc(fp);

    /* initialize the buffer index */

    index = 0;

    /* collect characters until the closing double quote */

    while (ch != '"')
        {
        if (ch == EOF)
            {
            fprintf(stderr,"SCAN ERROR: attempt to read a string failed ");
            fprintf(stderr,"no closing double quote ");
            exit(6);
            }
        if (index > size - 2)
            {
            ++size;
            buffer = reallocateMsg(buffer,size,"readString");
            }

        if (ch == '\')
            {
            ch = fgetc(fp);
            if (ch == EOF)
                {
                fprintf(stderr,"SCAN ERROR: attempt to read a string failed ");
                fprintf(stderr,"escaped character missing ");
                exit(6);
                }
            buffer[index] = convertEscapedChar(ch);
            }
        else
            buffer[index] = ch;
        ++index;
        ch = fgetc(fp);
        }

    buffer[index] = '';

    return buffer;
    }

char *
readToken(FILE *fp)
    {
    int ch,index;
    char *buffer;
    int size = 80;

    skipWhiteSpace(fp);

    ch = fgetc(fp);
    if (ch == EOF) return 0;

    buffer = allocateMsg(size,"readToken");

    index = 0;
    while (!isspace(ch))
        {
        if (ch == EOF) break;
        if (index > size - 2)
            {
            ++size;
            buffer = reallocateMsg(buffer,size,"readToken");
            }
        buffer[index] = ch;
        ++index;
        ch = fgetc(fp);
        }

    /* push back the character that got us out of this loop */

    ungetc(ch,fp);

    if (index > 0)              //there is something in the buffer
        clearerr(fp);           //so force the read to be good

    buffer[index] = '';

    return buffer;
    }

char *
readLine(FILE *fp)
    {
    int ch,index;
    char *buffer;
    int size = 512;
  
    ch = fgetc(fp);
    if (ch == EOF) return 0;

    buffer = allocateMsg(size,"readLine");

    index = 0;
    while (ch != ' ')
        {
        if (ch == EOF) break;
        if (index > size - 2)
            {
            ++size;
            buffer = reallocateMsg(buffer,size,"readLine");
            }
        buffer[index] = ch;
        ++index;
        ch = fgetc(fp);
        }


    if (index > 0)              //there is something in the buffer
        clearerr(fp);           //so force the read to be good

    buffer[index] = '';

    return buffer;
    }

/********** private functions **********************/

static void
skipWhiteSpace(FILE *fp)
    {
    int ch;

    /* read chars until a non-whitespace character is encountered */

    while ((ch = fgetc(fp)) != EOF && isspace(ch))
        continue;

    /* a non-space character got us out of the loop, so push it back */

    ungetc(ch,fp);
    }

static char
convertEscapedChar(int ch)
    {
    switch (ch)
        {
        case 'n': return ' ';
        case 't': return ' ';
        case '"': return '"';
        case '\': return '\';
        }
    return ch;
    }

void *
allocate(size_t size)
    {
    char *s;
    s = malloc(size);
    if (s == 0)
        {
        fprintf(stderr,"could not allocate string, out of memory ");
        exit(3);
        }

    return s;
    }

void *
reallocate(void *s,size_t size)
    {
    char *t;
    t = realloc(s,size);
    if (t == 0)
        {
        fprintf(stderr,"could not reallocate string, out of memory ");
        exit(3);
        }

    return t;
    }


void *
allocateMsg(size_t size,char *where)
    {
    char *s;
    s = malloc(size);
    if (s == 0)
        {
        fprintf(stderr,"%s: could not allocate string, out of memory ",
            where);
        exit(3);
        }

    return s;
    }

static void *
reallocateMsg(void *s,size_t size,char *where)
    {
    char *t;
    t = realloc(s,size);
    if (t == 0)
        {
        fprintf(stderr,"%s: could not reallocate string, out of memory ",
            where);
        exit(3);
        }

    return t;
    }


scanner.h

#ifndef SCANNER_H
#define SCANNER_H

extern int readInt(FILE *);
extern double readReal(FILE *);
extern char readChar(FILE *);
extern char readRawChar(FILE *);
extern char *readString(FILE *);
extern char *readToken(FILE *);
extern char *readLine(FILE *);
extern void *allocate(size_t);
extern void *reallocate(void *,size_t);
#endif