Fixed segfault when no arguments given

This commit is contained in:
Rory Healy 2021-09-08 22:22:48 +10:00
parent 5198405eb4
commit f7b25ecb92
6 changed files with 5 additions and 10 deletions

View file

@ -17,7 +17,6 @@
#define STAGE_2_ARG_COUNT 5 #define STAGE_2_ARG_COUNT 5
#define STAGE_3_ARG_COUNT 5 #define STAGE_3_ARG_COUNT 5
#define STAGE_4_ARG_COUNT 5 #define STAGE_4_ARG_COUNT 5
#define OPEN_FILE_ERROR "Error: Unable to open file %s\n"
enum stages getStage(char *stage) { enum stages getStage(char *stage) {
if (strcmp(stage, "1") == 0){ if (strcmp(stage, "1") == 0){
@ -95,7 +94,7 @@ int getExpectedArgs(enum stages stage) {
FILE *safeFileOpen(FILE **f, char *fileName, char *mode) { FILE *safeFileOpen(FILE **f, char *fileName, char *mode) {
*f = fopen(fileName, mode); *f = fopen(fileName, mode);
if (*f == NULL) { if (*f == NULL) {
fputs(OPEN_FILE_ERROR, stderr); fprintf(stderr, "Error: Unable to open file %s\n", fileName);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return *f; return *f;

BIN
input.o

Binary file not shown.

12
main.c
View file

@ -8,14 +8,7 @@
* The polygon can be split using a pair of integers, which represent * The polygon can be split using a pair of integers, which represent
* the edge numbers that should be connected. This comes from stdin. * the edge numbers that should be connected. This comes from stdin.
* *
* To run the program type: * To see valid input arguments, run ./voronoi2
* ./voronoi1 dataset_file polygon_file output_file < splits
*
* Options:
* dataset_file required Path to the dataset file (CSV)
* polygon_file required Path to the polygon file (txt)
* output_file required Output solution file
* splits optional Pairs of integers to split the edges
* *
*/ */
@ -69,6 +62,9 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
/* Ensure input arguments are correct */ /* Ensure input arguments are correct */
if (argc == 1) {
printArgError(STAGE_ERROR, "Incorrect usage.");
}
enum stages stage = getStage(argv[1]); enum stages stage = getStage(argv[1]);
int expectedArgs = getExpectedArgs(stage); int expectedArgs = getExpectedArgs(stage);
if (expectedArgs != argc) { if (expectedArgs != argc) {

BIN
main.o

Binary file not shown.

BIN
voronoi1

Binary file not shown.

BIN
voronoi2

Binary file not shown.