Is it possible by using sscanf to get the first token then skip some tokens and then get the last one?
For example, the output of /bin/ps -fu1000
cm 2249 1548 0 0:00.00 ttys001 0:00.01 man sscanf
I have tried:
sscanf(line, "%s %[^\n]", user, cmd);
The result should be:
user = "cm";
cmd = "man sscanf":
But it does not work.
Yes, but it’s ugly and can’t be error checked properly.
You can replace some of the
%*swith%*d. The*means that the item is parsed but not assigned anywhere (it is ignored).In the statement, there are 6 ignored items corresponding to the items between
"cm"and"man sscanf"in your example.Also note I limited the input to 41 characters in the scanf itself. Make sure you do not write outside the objects.
EDIT: I added a space before the last conversion because, unlike
%sor%dconversions, the%[conversion does not skip leading whitespace.