Question

Imagine the following:

you read in a string with scanf() but you only need a few of the datapoints in the string.

Is there an easy way to throw away the extraneous information, without losing the ability to check if the appropriate data is there so you can still reject malformed strings easily?

example:

const char* store = "Get: 15 beer 30 coke\n";
const char* dealer= "Get: 8 heroine 5 coke\n";
const char* scream= "Get: f* beer 10 coke\n";

I want to accept the first string, but forget about the beer because beer is yuckie. I want to reject the second and third strings because they are clearly not the appropriate lists for the 7/11;

So I was thinking about the following construction:

char* bId = new char[16];
char* cId = new char[16];
int cokes;
sscanf([string here], "Get: %d %s %d %s\n", [don't care], bId, &cokes, cId);

This way I would keep the format checking, but what would I put for [don't care] that doesn't make the compiler whine?

Of course I could just make a variable I don't use later on, but that is not the point of this question. Also checking the left and right side seperately is an obvious solution I am not looking for here.

So, is there a way to not care about but still check the type of a piece of string in scanf and friends?

Was it helpful?

Solution

Use a * as assignment suppression character after %

Example:

  sscanf([string here], "Get: %*d %s %d %s\n", bId, &cokes, cId);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top