Here's a pretty neat API that was introduced as part of the POSIX.1-2008 spec. What it does is allows you to use the `fopen` family of APIs to interact with strings. The closest thing I can compare it to is Python's StringIO class.
The really nice thing about it is it takes away all the hassles of seeking and a whole bunch of "whoops, I just wrote off the end of the buffer" problems. It comes in two forms: `fmemopen` and `open_memstream` (the later dynamically allocating memory as needed).
Downside is it's POSIX.1-2008, so not widely implemented on platforms other than those using glibc.
(Also fun is `fopencookie` which allows you to make your own custom FILE objects - see https://www.gnu.org/software/libc/manual/html_node/Streams-and-Cookies.html )
The really nice thing about it is it takes away all the hassles of seeking and a whole bunch of "whoops, I just wrote off the end of the buffer" problems. It comes in two forms: `fmemopen` and `open_memstream` (the later dynamically allocating memory as needed).
Downside is it's POSIX.1-2008, so not widely implemented on platforms other than those using glibc.
(Also fun is `fopencookie` which allows you to make your own custom FILE objects - see https://www.gnu.org/software/libc/manual/html_node/Streams-and-Cookies.html )