From 6cb22fd7f2c20be2cf268d6bcd236252d7847763 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Wed, 21 Jan 2026 17:53:40 +0100 Subject: Engage! --- examples/files.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/files.c (limited to 'examples/files.c') diff --git a/examples/files.c b/examples/files.c new file mode 100644 index 0000000..663bbf9 --- /dev/null +++ b/examples/files.c @@ -0,0 +1,52 @@ +#define NONSTD_IMPLEMENTATION +#include "../nonstd.h" + +#include + +int main(void) { + // 1. Basic usage + const char *msg = "Hello, World!\n"; + if (write_entire_file("test_basic.txt", msg, strlen(msg))) { + printf("Written test_basic.txt\n"); + } + + size_t sz; + char *content = read_entire_file("test_basic.txt", &sz); + if (content) { + printf("Read: %.*s", (int)sz, content); + FREE(content); + } + + // 2. usage with string view + stringv sv = sv_from_cstr("Hello from String View!\n"); + if (write_file_sv("test_sv.txt", sv)) { + printf("Written test_sv.txt\n"); + } + + stringv sv_read = read_entire_file_sv("test_sv.txt"); + if (sv_read.data) { + printf("Read sv: %.*s", (int)sv_read.length, sv_read.data); + free((char *)sv_read.data); + } + + // 3. usage with string builder + stringb sb; + sb_init(&sb, 0); + sb_append_cstr(&sb, "Hello from "); + sb_append_cstr(&sb, "String Builder!\n"); + + if (write_file_sb("test_sb.txt", &sb)) { + printf("Written test_sb.txt\n"); + } + + // Read into stringb + stringb sb2 = read_entire_file_sb("test_sb.txt"); + if (sb2.data) { + printf("Read into sb: %s", sb2.data); + sb_free(&sb2); + } + + sb_free(&sb); + + return 0; +} -- cgit v1.2.3