diff options
| -rw-r--r-- | fileutil.c | 14 | ||||
| -rw-r--r-- | fileutil.h | 2 | ||||
| -rw-r--r-- | logfile.txt | 2 |
3 files changed, 11 insertions, 7 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; } @@ -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 |
