This is the C problem, and here is the Overview: This program takes three comman
ID: 3806363 • Letter: T
Question
This is the C problem, and here is the Overview: This program takes three command-line arguments, a logfile of text messages and two standard 10-digit
phone numbers, and prints a formatted display of all the conversations that took place between those two individuals. A sample execution is: ./a.out datafile 2055551111 2055552222
Pages two and three contain a sample logfile of text messages. Pages four and five contains sample executions of this program using this logfile. Look at the sample logfile and program executions before reading any further.
The logfile consists of a series of lines of the format show below:
<POSIX-time> <phone-num-1> <phone-num-2> <words-in-message> <the text message>
A POSIX-time is an integer representing the number of seconds since January 1, 1970. 1487952000 translates to Friday,
February 24, 2016 at 10:00am (Central). To convert this integer to a readable date, use the two-line function shown below. char *readableTime(int sec) {
// this function takes an integer representing a time in seconds // and 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 ) );
}
When dealing with phone numbers, you can either treat them as a string or an integer. If you treat them as an integer, please realize that most ten-digit phone numbers are bigger than the largest possible legal integer. If you use integers, make sure you declare them as long and not int. You can convert a command-line argument to a long using the function atol
Your program identifies, and prints, conversations between the two phone numbers listed on the command line. The required format for your output is:
The first 25 characters on a line are a readable version of the POSIX time, followed by five spaces. Make sure you remove the trailing newline character that the function shown above inserts into the character string.
Text messages from the first phone number start in column 31 and are displayed in a 25-character width, longer messages should wrap to multiple lines.
Text messages from the second phone number start in column 61 and are displayed in a 25-character width, longer messages should wrap to multiple lines.
What You Need To Do
Create a directory called project4 on your machine. In that directory, create a file named text.c
In text.c, write the code needed to solve the problem stated above. Make sure that your program:
o Has a header block of comments that includes your name and a brief overview of the program.
o Use at least two functions and has a brief comment describing the purpose of each function. The function readableTime( ), shown above, does not count as one of your two functions.
o Uses the data file and the two phone numbers specified on the command line.
Generates a formatted output of the conversation.
You can assume the user will always enter the proper number of command-line arguments, and that both phone numbers entered will be valid 10-digit positive integers.
If one (or both) of the phone numbers is not found in the data, then simply print the output header and nothing else.
If the specified log file does not exist, print an appropriate error message and exit the program.
If the log file exists, you can assume that it will always contain valid data.
Thank you so much
Explanation / Answer
text.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"scanner.h"
#include<time.h>
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 ) );
}
//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
test1.txt
1457737200 10 This should be first and then there should be others.
1457737205 7 This should be second and before third.
1457737210 6 This should be third after second.
1457737230 8 This should be fifth glad you finally responded.
1457737250 4 This should be seventh.
1457737270 4 This should be tenth.
1457737275 4 This should be eleventh.
1457737290 4 This should be thirteenth.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.