From 58970e8a68583e61e93091989af335cdac218085 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 25 Aug 2023 19:19:50 +0100 Subject: (file-handler)+implemented write_to_file First line is the data width, then the sand particles of each cell per row. --- file-handler.c | 17 ++++++++++++++++- main.c | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/file-handler.c b/file-handler.c index 765eec5..0f9e838 100644 --- a/file-handler.c +++ b/file-handler.c @@ -61,4 +61,19 @@ bool load_from_file(state_t *state, const char *filepath) } bool write_to_file(state_t *state, const char *filepath) -{} +{ + FILE *fp = fopen(filepath, "w"); + fprintf(fp, "%lu\n", state->dwidth); + for (size_t i = 0; i < state->dwidth; ++i) + { + for (size_t j = 0; j < state->dwidth; ++j) + { + fprintf(fp, "%d", state->data[(i * state->dwidth) + j]); + if (j != state->dwidth - 1) + fprintf(fp, ","); + } + fprintf(fp, "\n"); + } + fclose(fp); + return true; +} diff --git a/main.c b/main.c index ab11790..a4a36b0 100644 --- a/main.c +++ b/main.c @@ -117,6 +117,8 @@ int main(void) if (state.thread_alive) pthread_cancel(step_thread); + + write_to_file(&state, "data.out"); CloseWindow(); return 0; } -- cgit v1.2.3-13-gbd6f