read comma-separated input with `scanf()`

The comma is not considered a whitespace character so the format specifier "%s" will consume the , and everything else on the line writing beyond the bounds of the array sem causing undefined behaviour. To correct this you need to use a scanset:

while (scanf("%4[^,],%4[^,],%79[^,],%d", sem, type, title, &value) == 4)

where:

  • %4[^,] means read at most four characters or until a comma is encountered.

Specifying the width prevents buffer overrun.

Leave a Comment