shadow_ said:
Hi i m new at C and trying to write a parser and a string class.
Basicly program will read data from file and splits it into lines then
lines to words. i used strtok function for splitting data to lines it
worked quite well but srttok isnot working for multiple blank or
commas. Can strtok do this kind of splitting if it cant what should i
use .
It depends on what you mean by "this kind" of splitting.
One feature of strtok() is that any unbroken sequence of
delimiter characters is combined into a single delimiter.
For example, if the comma is the only delimiter character
the string "a,,b" will be divided into two tokens "a" and
"b", not into three tokens "a", "", and "b". Furthermore,
if comma and semicolon are delimiter characters, then the
string ";a,;,b;" divides into "a" and "b". strtok() returns
no information about the identity or quantity of delimiting
characters it finds, but concentrates entirely on the tokens
that they separate.
Another feature of strtok() is that it "remembers" the
string it is working on: when you pass NULL as the first
argument, it tells strtok() to proceed with the tail end
of the string it examined on the preceding call. This has
an important implication: strtok() can only process one
string at a time, because it can remember only one string --
if it were somehow able to remember twelve strings, how would
it know which one a NULL referred to? So if you have a loop
that divides a buffer into lines with strtok(...,"\n") and
you also have an inner loop that breaks each line into words
with something like strtok(...," \t"), it won't work: as soon
as the inner loop starts work on a line, strtok() will "forget"
its position in the outer loop.
I'm not sure what to suggest as an alternative or as a
remedy, because you haven't explained "this kind" of splitting
precisely enough: you say that strtok() "isnot working," but
you haven't described how it fails to meet your needs. Perhaps
you should use fgets() to read a line at a time (making the
line-splitting strtok() unnecessary) and then use strtok() to
divide each line into words. If you need more information about
the number and kind of delimiters, though, strtok() is not the
answer.