I am writing a CS project and I have spent the last week working on my code with
ID: 3830348 • 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;
}
The log:
The otucome.
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-timeExplanation / Answer
So the problem lies with the fact you are using a Windows based Operating System. The CS 100 Project Four assumes an UNIX-ly Operating System, probably the MacOSX. When you compile your program with "gcc -Wall" you get the binary executable in "a.exe" and not as "a.out". In MacOSX or similar operatins systems you would be getting the file "a.out" generated in the build directory (or in the current directory).
In order to run your program in Windows, use "a.exe" instead of "./a.out".
Hope this helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.