I have a file that has text inside it (<tab>
is \t
):
display <tab> output_stmt
[ <tab> left_bracket
"Hello World" <tab> string_const
] <tab> right_bracket
~ <tab> term_sym
How can I get the strings after the <tab>
, skipping the first string and <tab>
everyline.
I only know how to get only the first string and ignore the rest of the line by using:
strtok(variablename, "\t");
Any useful answers are much appreciated.
Thank you!
With the strtok()
function you can get also the next token. See the man page of strtok()
:
The
strtok()
function breaks a string into a sequence of zero or more nonempty tokens. On the first call tostrtok()
the string to be parsed should be specified instr
. In each subsequent call that should parse the same string,str
must beNULL
.
So you can call:
char* tokenOne = strtok(variablename, "\t"); /* first token */
char* tokenTwo = strtok(NULL, "\t"); /* second token */
Note that you don't have to free memory because strtok()
works with the input buffer as it changes it e.g.:
"display \t output_stmt\0"
will be:
"display \0 output_stmt\0"
after the call of strtok()
. After that it just returns a pointer to the next token.