asdf asdf - 1 year ago 78
C Question

Parsing a 3 word string in C

I have a text with this pattern :

command arg1 arg2
How can I extract
and store them in 2 variables?
I know that SO is not a code writing service, but i'm kind off running on a deadline and the results aren't helping.

Answer Source

You can use sscanf to achieve this

#include <stdio.h>

char command[64];
char arg1[64];
char arg2[64];

sscanf("a b c", "%s %s %s", command, arg1, arg2);

Now command equals "a", arg1 equals "b" and arg2 equals "c"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download