I'm using scanf to extract very regular tabular data. I need to go over it in two passes and can safely ignore some of the arguments some of the time. I want to allocate space for only the strings I care about and a single "discard" string for the others. If I pass this argument multiple times as output, is the behavior defined?
A silly minimal example:
char label[MAX_SIZE], discard[MAX_SIZE];
sscanf(input, "%s %s %s %s", discard, label, discard, discard);
Back to the example: scanf takes at least two arguments. The first one is a string that can consist of format specifiers.
The scanf() function reads data from the standard input stream stdin into the locations given by each entry in argument-list . Each argument must be a pointer to a variable with a type that corresponds to a type specifier in format-string .
scanf() blocks until data is available on the stdin file descriptor. This means that the thread executing this function is not able to perform any other work until scanf() returns.
I cannot find any language in the C11 standard that makes your intended usage invalid.
There seems to be a better solution, though. If you place a *
(“assignment suppression character”) after the %
in the format string, then the scanf
function will parse but not store the input item. Hence, you don't have to (must not, actually) provide a destination argument for it. Seems to be exactly what you need here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With