file-handling · intermediate · ~15 min
Stateful line-by-line config parsing.
int ini_get(const char *ini, const char *section, const char *key, char *out, size_t cap);
ini is newline-separated config text with [section] headers and key=value
lines. Return the value for key inside section in out, or -1 if not
found / overflow / NULL.
[name] lines.key= when you're inside the requested section.= into out.INI/conf files drive countless tools. Looking up [section] key=value teaches stateful line parsing.
#include <stddef.h>
int ini_get(const char *ini, const char *section, const char *key, char *out, size_t cap) {
/* TODO */
(void)ini; (void)section; (void)key; (void)out; (void)cap;
return -1;
}
Matching a key in the wrong section. Prefix-matching keys (use exact length). Forgetting the overflow guard.
Same key in two sections. Missing key. Section with no such key.
O(text length).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.