summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakiyamn2020-09-04 13:35:49 +1000
committerakiyamn2020-09-04 13:35:49 +1000
commitf36dd4780224648c7abcfa447c5a7a6c7712ec12 (patch)
tree6e49d27fe47474856218513758fbe4a7fb14b446
parent2b7c5811cb77892cdfb83441573613e9e1b45a0a (diff)
downloadfit2100_ass1-f36dd4780224648c7abcfa447c5a7a6c7712ec12.tar.gz
fit2100_ass1-f36dd4780224648c7abcfa447c5a7a6c7712ec12.zip
Working Task 2 functionality
-rw-r--r--fileutil.c14
-rw-r--r--fileutil.h2
-rw-r--r--logfile.txt2
3 files changed, 11 insertions, 7 deletions
diff --git a/fileutil.c b/fileutil.c
index 266569f..7ae3f6c 100644
--- a/fileutil.c
+++ b/fileutil.c
@@ -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;
}
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