diff options
| author | akiyamn | 2020-09-04 13:35:49 +1000 |
|---|---|---|
| committer | akiyamn | 2020-09-04 13:35:49 +1000 |
| commit | f36dd4780224648c7abcfa447c5a7a6c7712ec12 (patch) | |
| tree | 6e49d27fe47474856218513758fbe4a7fb14b446 /fileutil.c | |
| parent | 2b7c5811cb77892cdfb83441573613e9e1b45a0a (diff) | |
| download | fit2100_ass1-f36dd4780224648c7abcfa447c5a7a6c7712ec12.tar.gz fit2100_ass1-f36dd4780224648c7abcfa447c5a7a6c7712ec12.zip | |
Working Task 2 functionality
Diffstat (limited to 'fileutil.c')
| -rw-r--r-- | fileutil.c | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -15,6 +15,7 @@ #include <unistd.h> #include <sys/file.h> #include <stdlib.h> +#include <string.h> #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; } |
