From c1db1b967b6370a2adb6bb14c575fa9c2586e599 Mon Sep 17 00:00:00 2001 From: "dario.genga" <dario.genga@etu.hesge.ch> Date: Sat, 18 Jun 2022 16:46:19 +0200 Subject: [PATCH] Add option to define output path --- .gitignore | 1 + kmeans.c | 1 - kmeans.h | 2 -- main.c | 31 ++++++++++++++++++++++--------- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 7769823..b97ac13 100644 --- a/.gitignore +++ b/.gitignore @@ -79,5 +79,6 @@ dkms.conf # Custom gitignore for project main +output_data.txt cmake-build-debug .idea \ No newline at end of file diff --git a/kmeans.c b/kmeans.c index 9bb1d3b..f900603 100644 --- a/kmeans.c +++ b/kmeans.c @@ -4,7 +4,6 @@ #include "kmeans.h" - /// Create empty clusters for the universe /// \param universe The universe who contains the cluster to create. void initialize_clusters_array(kmeans* universe) { diff --git a/kmeans.h b/kmeans.h index 7947c15..b6518c8 100644 --- a/kmeans.h +++ b/kmeans.h @@ -11,8 +11,6 @@ #include "files_utils.h" #include "values_utils.h" -#define REQUIRED_FULL_ARGS 4 -#define ONLY_SOURCE_PATH_ARG 2 #define LINE_INDEX_DIMENSIONS 0 #define LINE_INDEX_CLUSTER 1 #define LINE_INDEX_CONTENT 2 diff --git a/main.c b/main.c index 7493511..12ef374 100644 --- a/main.c +++ b/main.c @@ -6,15 +6,17 @@ #include <time.h> #include "kmeans.h" -#define REQUIRED_FULL_ARGS 4 +#define REQUIRED_FULL_ARGS 5 #define ONLY_SOURCE_PATH_ARG 2 -#define FILE_OUTPUT "./output_data.txt" +#define ONLY_SOURCE_AND_OUTPUT_PATH_ARGS 3 +#define DEFAULT_FILE_OUTPUT "./output.txt" -kmeans *init_frm_cmd_arguments(int argc, char *argv[]) { +kmeans *init_frm_cmd_arguments(int argc, char *argv[], char *output_file) { kmeans *universe; int k; int dimensions; char data_path[256]; + char output[256]; int *data_length = malloc(sizeof(int)); point** data; @@ -26,18 +28,25 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) { scanf("%d", &dimensions); printf("Path to data (comma separator) : "); scanf("%s", data_path); + printf("Output : "); + scanf("%s", output); } // Create the universe with only our file - else if (argc == ONLY_SOURCE_PATH_ARG) { + else if (argc == ONLY_SOURCE_PATH_ARG || argc == ONLY_SOURCE_AND_OUTPUT_PATH_ARGS) { strcpy(data_path, argv[1]); universe = kmeans_create_empty(); read_custom_data_source(universe, data_path); + if (argc == ONLY_SOURCE_PATH_ARG) + strcpy(output, DEFAULT_FILE_OUTPUT); + else + strcpy(output, argv[2]); } // Set the number of dimensions, clusters and path to data else if (argc == REQUIRED_FULL_ARGS) { dimensions = atoi(argv[1]); k = atoi(argv[2]); strcpy(data_path, argv[3]); + strcpy(output, argv[4]); } // Bad number of arguments else { @@ -48,15 +57,17 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) { // Display instructions printf("you must start the program without arguments or with one of the following format :\n"); - printf("main <number of dimensions> <number of cluster> <path_to_data_source>\n"); - printf("or\n"); + printf("main <number of dimensions> <number of cluster> <path_to_data_source> <output_path>\n"); + printf("main <path_to_custom_data_source> <output_path>\n"); printf("main <path_to_custom_data_source>\n"); exit(EXIT_SUCCESS); } + // Save the output file path + strcpy(output_file, output); // Create the universe - if (argc != ONLY_SOURCE_PATH_ARG) { + if (argc != ONLY_SOURCE_PATH_ARG && argc != ONLY_SOURCE_AND_OUTPUT_PATH_ARGS) { data = read_data(data_path, dimensions, data_length); universe = kmeans_create(k, dimensions, data, *data_length); } @@ -67,15 +78,17 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) { int main(int argc, char *argv[]) { srand(time(NULL)); + char output_file[256]; // Initialize the universe - kmeans *universe = init_frm_cmd_arguments(argc, argv); + kmeans *universe = init_frm_cmd_arguments(argc, argv, output_file); + printf("%s", output_file); // Start the clustering start_clustering(universe); // Output the result to a file - write_data_output(universe, FILE_OUTPUT); + write_data_output(universe, output_file); // Free the universe and exit the program destroy_universe(universe); -- GitLab