From f36dd4780224648c7abcfa447c5a7a6c7712ec12 Mon Sep 17 00:00:00 2001 From: akiyamn Date: Fri, 4 Sep 2020 13:35:49 +1000 Subject: Working Task 2 functionality --- fileutil.c | 14 ++++++++++---- fileutil.h | 2 +- logfile.txt | 2 -- 3 files changed, 11 insertions(+), 7 deletions(-) delete mode 100644 logfile.txt diff --git a/fileutil.c b/fileutil.c index 266569f..7ae3f6c 100644 --- a/fileutil.c +++ b/fileutil.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "fileutil.h" #define FILE_BUF_SIZE 1024 @@ -22,14 +23,14 @@ * Prints a given string to stdout. Returns nothing. */ void to_stdout(char *string) { - write(1, string, sizeof(string)); + write(1, string, strlen(string)); } /* * Prints a given string to stderr. Returns nothing. */ void to_stderr(char *string) { - write(2, string, sizeof(string)); + write(2, string, strlen(string)); } /* @@ -42,6 +43,7 @@ void print_file(char *read_path) { // Read the input file if ((infile = open(read_path, O_RDONLY)) <= 2){ to_stderr(read_path); + to_stderr(" could not be opened for reading.\n"); exit(1); // Exit if an error occurs } @@ -54,7 +56,11 @@ void print_file(char *read_path) { close(infile); } -int main(int argc, char const *argv[]) { - print_file("logfile.txt"); +int main(int argc, char *argv[]) { + if (argc >= 2) { + print_file(argv[1]); + } else { + print_file("logfile.txt"); + } return 0; } diff --git a/fileutil.h b/fileutil.h index 88e59ae..06b43f8 100644 --- a/fileutil.h +++ b/fileutil.h @@ -4,6 +4,6 @@ void to_stdout(char *string); void to_stderr(char *string); void print_file(char *read_path); -int main(int argc, char const *argv[]); +int main(int argc, char *argv[]); #endif \ No newline at end of file diff --git a/logfile.txt b/logfile.txt deleted file mode 100644 index 05aae31..0000000 --- a/logfile.txt +++ /dev/null @@ -1,2 +0,0 @@ -12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ -nsdfggdfdgfd dfgdfgd dfgdfg dfgdgfdfg dgf dgf dfg dfg \ No newline at end of file -- cgit v1.2.3