From deb02231af58c1618a55324ad15d2b6cbfab71aa Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 22 Jul 2024 21:05:09 +1200 Subject: [PATCH] add sl_write_file --- slibs.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/slibs.h b/slibs.h index f8e4159..7da37b2 100644 --- a/slibs.h +++ b/slibs.h @@ -199,6 +199,7 @@ void sl_append_c_str(sl_string *sl_str, const char *c_str) }); void sl_read_file(const char *filename, sl_string *buffer); +void sl_write_file(const char *filename, const sl_string buffer); #ifdef SL_IMPLEMENTATION void sl_read_file(const char *filename, sl_string *buffer) @@ -221,6 +222,23 @@ void sl_read_file(const char *filename, sl_string *buffer) fclose(file); } + +void sl_write_file(const char *filename, const sl_string buffer) +{ + FILE *file = fopen(filename, "w"); + if (!file) + { + fprintf(stderr, "Error: could not open file %s\n", filename); + exit(1); + } + + for (sl_vec_it(c, buffer)) + { + fputc(*c, file); + } + + fclose(file); +} #endif #endif // SLIBS_H