c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

commit 0720a53642433e2da606d9528f485eeef44d811a
parent 4af61b5b35326d4b6cd134d4535217c76f887115
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 18 Oct 2026 12:09:09 -0500

Better error handling, support cli filepath specification

Diffstat:
Mperf-aware-programming/haversine/Makefile | 2+-
Mperf-aware-programming/haversine/json.c | 10++++++++--
Mperf-aware-programming/haversine/main.c | 9++++++++-
3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/perf-aware-programming/haversine/Makefile b/perf-aware-programming/haversine/Makefile @@ -8,4 +8,4 @@ build: gcc main.c run: build - ./a.out + ./a.out artifacts/out.json diff --git a/perf-aware-programming/haversine/json.c b/perf-aware-programming/haversine/json.c @@ -14,8 +14,11 @@ struct Json { }; char* loadFileToString(char* filename) { - printf("Loading %s\n" , filename); FILE* fp = fopen(filename, "r"); + if(!fp) { + printf("Unable to open %s", filename); + return NULL; + } char *line = NULL; size_t len = 0; ssize_t read; @@ -130,11 +133,14 @@ struct Coordinates* parseCoordinates(char* jsonData, int* sz) { struct Coordinates* loadJson(char* filename, int* size) { char* line = loadFileToString(filename); + if(line == NULL) { + return NULL; + } + int sz = 0; struct Coordinates* res = parseCoordinates(line, &sz); size[0] = sz; free(line); - printf("%d", sz); return res; } diff --git a/perf-aware-programming/haversine/main.c b/perf-aware-programming/haversine/main.c @@ -2,8 +2,15 @@ int main(int argc, char** argv) { + if(argc != 2) { + printf("Usage: hav {json-filepath}\n"); + return -1; + } int size = 0; - struct Coordinates* coords = loadJson("artifacts/out.json", &size); + struct Coordinates* coords = loadJson(argv[1], &size); + if(coords == NULL) { + return -1; + } for(int i = 0; i < size; ++i) { struct Coordinates* current = &coords[i];