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

I am writing a CS project and I have spent the last week working on my code with

ID: 3830393 • Letter: I

Question

I am writing a CS project and I have spent the last week working on my code with a buddy of mine who took the class last year. I need some help getting the right outcome from the code. I have gcc -Wall the code and it works just fine. But I need help writing the C programming language to get the desired outcome with the ./a.out. Can I get help please?

CODE:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <math.h>

#include <string.h>

// convert time

char * readableTime(int sec) {

time_t epoch_time = (time_t) sec;

return asctime(localtime(&epoch_time));

}

int main(void)

{

// for simplicity, i have taken a file name , sender and receiver string

// you can ask user to input these from command line also.

char const* const fileName = "C:\Users\ykgupta\Documents\df\d\input.txt";

char *sender = "123", *receiver = "456";

// open file

FILE* file = fopen(fileName, "r"); /* should check the result */

// temporary variable to store line

char line[1000];

// keep looping till lines are left

while (fgets(line, sizeof(line), file)) {

// if it is a blank line, leave it

if(line[0] == ' ') {

// blank line

continue;

}

// tokenize string

char *p = strtok (line, " ");

char *array[100];

int i=0;

array[0] = p; // time

p = strtok (NULL, " ");

array[1] = p; // phone no 1

p = strtok (NULL, " ");

array[2] = p; // phone no 2

p = strtok (NULL, " ");

array[3] = p; // count of words

// making delimiter blank, to get the whole remaining string

p = strtok (NULL, "");

// now sring p points to the remaining message in that line...

// if this message is a conversation between our required contact nos

if( ((strcmp(array[1], sender) == 0) && (strcmp(array[2], receiver) == 0) ) ||

((strcmp(array[1], receiver) == 0) && (strcmp(array[2], sender) == 0)) ) {

// print information

printf("Time: %s", readableTime(atoi(array[0])));

printf("From: %s ", array[1]);

printf("To: %s ", array[2]);

printf("Message: %s ", p);

}

}

// close file

fclose(file);

return 0;

}

CS 100 Project Four Spring 2017 Project Overview: This program takes three command-line arguments, a lo of text messages and two standard 10-digit gfile phone numbers, and prints a formatted display of all the conversations that took place between those two individuals. A ample 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

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
// convert time
char * readableTime(int sec) {
time_t epoch_time = (time_t) sec;
return asctime(localtime(&epoch_time));
}
int main(int argc, char *argv[]) //to take commandline arguments
{
   //check number of arguments passes
   if( argc == 4 ) {
// printf("The argument supplied is %s ", argv[1]);
       // for simplicity, i have taken a file name , sender and receiver string
       // you can ask user to input these from command line also.
   // sscanf(argv[1], "%d", &m);
       char const* const fileName = argv[1]; // index 0 is name of program. index 1 filename and index 2 and 3 the sender receiver strings
       char *sender = argv[2], *receiver = argv[3];
       // open file
       FILE* file = fopen(fileName, "r"); /* should check the result */
       // temporary variable to store line
       char line[1000];
       // keep looping till lines are left
       while (fgets(line, sizeof(line), file)) {
       // if it is a blank line, leave it
       if(line[0] == ' ') {
       // blank line
       continue;
       }
       // tokenize string
       char *p = strtok (line, " ");
       char *array[100];
       int i=0;
       array[0] = p; // time
       p = strtok (NULL, " ");
       array[1] = p; // phone no 1
       p = strtok (NULL, " ");
       array[2] = p; // phone no 2
       p = strtok (NULL, " ");
       array[3] = p; // count of words
       // making delimiter blank, to get the whole remaining string
       p = strtok (NULL, "");
       // now sring p points to the remaining message in that line...
       // if this message is a conversation between our required contact nos
       if( ((strcmp(array[1], sender) == 0) && (strcmp(array[2], receiver) == 0) ) ||
       ((strcmp(array[1], receiver) == 0) && (strcmp(array[2], sender) == 0)) ) {
       // print information
       printf("Time: %s", readableTime(atoi(array[0])));
       printf("From: %s ", array[1]);
       printf("To: %s ", array[2]);
       printf("Message: %s ", p);
       }
       }
       // close file
       fclose(file);
}
else if( argc > 3 ) {
printf("Too many arguments supplied. ");
}
else {
printf("Three arguments expected. ");
}
return 0;
}

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