Originally Posted by
^xor
> Probably by writing a custom parser, looking for a regex package, or trying to rework the specification to have a tighter format.
%[...] doesn't support regex does it? From what I can tell, %*[ \t\n]%39[^=]...snip must match at least one tab, blank or newline in the beginning, so if you put KEY=val at the beginning of the line, that scan will fail?
Code:
#include <stdio.h>
int main(void)
{
const char *input = "KEY1=val1";
char key[40] = {0}, value[40] = {0};
int n, items = sscanf(input, "%*[ \t]%39[^=]=%39[^ \t#\n]%n", key, value, &n);
printf("items = %d, key = \"%s\", value = \"%s\", n = %d\n",
items, key, value, n);
return 0;
}
/* my output
items = 2, key = "KEY1", value = "val1", n = 1
*/
Hmm.
n [...]If the conversion specification includes an assignment suppressing character or a field width, the behavior is undefined.
Bad me.