Skip to content
Snippets Groups Projects
Commit c1db1b96 authored by dario.genga's avatar dario.genga
Browse files

Add option to define output path

parent f3cb8734
No related branches found
No related tags found
No related merge requests found
...@@ -79,5 +79,6 @@ dkms.conf ...@@ -79,5 +79,6 @@ dkms.conf
# Custom gitignore for project # Custom gitignore for project
main main
output_data.txt
cmake-build-debug cmake-build-debug
.idea .idea
\ No newline at end of file
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "kmeans.h" #include "kmeans.h"
/// Create empty clusters for the universe /// Create empty clusters for the universe
/// \param universe The universe who contains the cluster to create. /// \param universe The universe who contains the cluster to create.
void initialize_clusters_array(kmeans* universe) { void initialize_clusters_array(kmeans* universe) {
......
...@@ -11,8 +11,6 @@ ...@@ -11,8 +11,6 @@
#include "files_utils.h" #include "files_utils.h"
#include "values_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_DIMENSIONS 0
#define LINE_INDEX_CLUSTER 1 #define LINE_INDEX_CLUSTER 1
#define LINE_INDEX_CONTENT 2 #define LINE_INDEX_CONTENT 2
......
...@@ -6,15 +6,17 @@ ...@@ -6,15 +6,17 @@
#include <time.h> #include <time.h>
#include "kmeans.h" #include "kmeans.h"
#define REQUIRED_FULL_ARGS 4 #define REQUIRED_FULL_ARGS 5
#define ONLY_SOURCE_PATH_ARG 2 #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; kmeans *universe;
int k; int k;
int dimensions; int dimensions;
char data_path[256]; char data_path[256];
char output[256];
int *data_length = malloc(sizeof(int)); int *data_length = malloc(sizeof(int));
point** data; point** data;
...@@ -26,18 +28,25 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) { ...@@ -26,18 +28,25 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) {
scanf("%d", &dimensions); scanf("%d", &dimensions);
printf("Path to data (comma separator) : "); printf("Path to data (comma separator) : ");
scanf("%s", data_path); scanf("%s", data_path);
printf("Output : ");
scanf("%s", output);
} }
// Create the universe with only our file // 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]); strcpy(data_path, argv[1]);
universe = kmeans_create_empty(); universe = kmeans_create_empty();
read_custom_data_source(universe, data_path); 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 // Set the number of dimensions, clusters and path to data
else if (argc == REQUIRED_FULL_ARGS) { else if (argc == REQUIRED_FULL_ARGS) {
dimensions = atoi(argv[1]); dimensions = atoi(argv[1]);
k = atoi(argv[2]); k = atoi(argv[2]);
strcpy(data_path, argv[3]); strcpy(data_path, argv[3]);
strcpy(output, argv[4]);
} }
// Bad number of arguments // Bad number of arguments
else { else {
...@@ -48,15 +57,17 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) { ...@@ -48,15 +57,17 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) {
// Display instructions // Display instructions
printf("you must start the program without arguments or with one of the following format :\n"); 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("main <number of dimensions> <number of cluster> <path_to_data_source> <output_path>\n");
printf("or\n"); printf("main <path_to_custom_data_source> <output_path>\n");
printf("main <path_to_custom_data_source>\n"); printf("main <path_to_custom_data_source>\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
// Save the output file path
strcpy(output_file, output);
// Create the universe // 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); data = read_data(data_path, dimensions, data_length);
universe = kmeans_create(k, dimensions, data, *data_length); universe = kmeans_create(k, dimensions, data, *data_length);
} }
...@@ -67,15 +78,17 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) { ...@@ -67,15 +78,17 @@ kmeans *init_frm_cmd_arguments(int argc, char *argv[]) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
srand(time(NULL)); srand(time(NULL));
char output_file[256];
// Initialize the universe // 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 the clustering
start_clustering(universe); start_clustering(universe);
// Output the result to a file // 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 // Free the universe and exit the program
destroy_universe(universe); destroy_universe(universe);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment