Read a whole file into a Str, and tell a read error from out of memory.
#include "mem/mem.c"
#include "str/str.c"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
const char *path = argc > 1 ? argv[1] : "data/pg2554.txt";
FILE *f = fopen(path, "rb");
if (f == NULL) {
perror(path);
return EXIT_FAILURE;
}
bool read_error = ferror(f);
fclose(f);
if (!ok) {
fprintf(stderr, "%s: %s\n", path,
read_error ? "read error" : "out of memory");
return EXIT_FAILURE;
}
printf(
"%s: %zu bytes\n", path, text.
len);
return EXIT_SUCCESS;
}
size_t len
Bytes of content, excluding the terminator.
void str_free(Str *s)
Free the buffer and reset s to the zero state.
bool str_append_file(Str *s, FILE *f)
Append everything left to read from f.
Owned, growable byte string.