By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Splitting function

P: n/a
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 .

Unal

Jun 8 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
shadow_ wrote:
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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 8 '07 #2

P: n/a
Eric Sosman wrote:
>
.... snip ...
>
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.
The OP can go back a few days and search for my publication of
toksplit (since renamed to tknsplit for standardization reasons).
This might fill his requirements.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.