Board index » cppbuilder » strtok

strtok


2007-07-04 10:41:45 PM
cppbuilder114
Hi All
Newbie here
If I use the following routine below, to read a line of text from a file
on disk, and split it on the first comma (which works), how do I split
it if the separator happens to be a "\" rather than a comma.
Replacing the "\" with the "," in the 2bd line as follows
Buf1 = strtok(buffer,"\");
obviously does not work
Thanks
Barry
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char buffer[256];
char *Buf1, *Buf2;
fgets(buffer,200,disk);
Buf1 = strtok(buffer,",");
Buf2 = strtok(NULL,"\"");
float Offset = atof(Buf1);
float Height = atof(Buf2);
 
 

Re:strtok

Because the backslash is used as an escape character, "\" will not work. An
actual backslash character in a string is indicated by putting two
backslashes in a row. For example: "\\"
. Ed
Quote
Barry wrote in message
news: XXXX@XXXXX.COM ...
Hi All

Newbie here

If I use the following routine below, to read a line of text from a file
on disk, and split it on the first comma (which works), how do I split it
if the separator happens to be a "\" rather than a comma.

Replacing the "\" with the "," in the 2bd line as follows

Buf1 = strtok(buffer,"\");

obviously does not work


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char buffer[256];
char *Buf1, *Buf2;

fgets(buffer,200,disk);
Buf1 = strtok(buffer,",");
Buf2 = strtok(NULL,"\"");

float Offset = atof(Buf1);
float Height = atof(Buf2);
 

Re:strtok

In this instance the \\ did not work, because strtok specifically
inserts a NULL character where the seperator was, ie a \0 so that when
you search the same string again, you use NULL.
I actually don't fully unstand how it wrks, but it seems to work like
that. However using a \ or \\ as the search seperator messes up the
system....or so I have found.
Any other C++ commands to search a string for a seperating character?
Barry
Ed Mulroy [TeamB] wrote:
Quote
Because the backslash is used as an escape character, "\" will not work. An
actual backslash character in a string is indicated by putting two
backslashes in a row. For example: "\\"

. Ed

>Barry wrote in message
>news: XXXX@XXXXX.COM ...
>Hi All
>
>Newbie here
>
>If I use the following routine below, to read a line of text from a file
>on disk, and split it on the first comma (which works), how do I split it
>if the separator happens to be a "\" rather than a comma.
>
>Replacing the "\" with the "," in the 2bd line as follows
>
>Buf1 = strtok(buffer,"\");
>
>obviously does not work
>
>
>//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>char buffer[256];
>char *Buf1, *Buf2;
>
>fgets(buffer,200,disk);
>Buf1 = strtok(buffer,",");
>Buf2 = strtok(NULL,"\"");
>
>float Offset = atof(Buf1);
>float Height = atof(Buf2);


 

{smallsort}

Re:strtok

"Barry" < XXXX@XXXXX.COM >wrote in message
Quote
In this instance the \\ did not work
Yes, it will, if you use it properly. Please show your latest code.
Quote
strtok specifically inserts a NULL character where the seperator was
Which is perfectly fine, even when "\\" is used.
Quote
when you search the same string again, you use NULL.
Not true.
Quote
I actually don't fully unstand how it wrks
So it would seem.
Quote
it seems to work like that.
No, it does not.
Quote
However using a \ or \\ as the search seperator messes up the
system...
Then you are probably doing something wrong with it. "\\" is a valid
character to use as a separator.
Gambit
 

Re:strtok

Here is my code
fgets(buffer,200,disk);
Buf1 = strtok(buffer,"\\");
Buf2 = strtok(NULL,"\"");
PartHeader[l]->PartName = Buf1;
PartHeader[l]->PartDescr = Buf2;
checkfile << "Part " << l << " , " << Buf1 << " Descr " << Buf2;
And you were quite correct in that it does work
My problem was that in some instances the string being read is in two
parts with a \ separator, and sometimes there is no 2nd part to the
string and hence no \ separator, and the program was (understandably)
dropping the routine at that point, but not crashing or throwing up any
exception error, so I was misled into thinking the "\\" was merely not
working.
Now to find a way to make it sensitive to the variable input...ie
sometimes a separator and 2nd part, and sometimes not.
Barry
Remy Lebeau (TeamB) wrote:
Quote
"Barry" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>In this instance the \\ did not work

Yes, it will, if you use it properly. Please show your latest code.

>strtok specifically inserts a NULL character where the seperator was

Which is perfectly fine, even when "\\" is used.

>when you search the same string again, you use NULL.

Not true.

>I actually don't fully unstand how it wrks

So it would seem.

>it seems to work like that.

No, it does not.

>However using a \ or \\ as the search seperator messes up the
system...

Then you are probably doing something wrong with it. "\\" is a valid
character to use as a separator.


Gambit


 

Re:strtok

"Barry" < XXXX@XXXXX.COM >wrote in message
Quote
Now to find a way to make it sensitive to the variable input...
ie sometimes a separator and 2nd part, and sometimes not.
That is simple - Buf2 will be NULL if there was no separator.
Gambit
 

Re:strtok

Many thanks for the advice.
Following on from this:
If the first separator is sometimes a "@" and sometimes a "," and
different routines need to follow depending on which is found, how would
one go about that.
One method I have thought of would be to search the initial entire
buffer (string) for any occurance of "@", and then immediately assign to
routine 1, as follows, failing which routine 2:
How does one search the initial buffer (string) for an occurance of "@"?
Example:
fgets(buffer,200,disk);
// Routine 1
Buf1 = strtok(buffer,"@");
Buf2 = strtok(NULL,",");
Buf3 = strtok(NULL,"@");
Buf4 = strtok(NULL,",");
Buf5 = strtok(NULL,",");
Buf6 = strtok(NULL,"\"");
// Routine 2
Buf1 = strtok(NULL,",");
Buf2 = strtok(NULL,",");
Buf3 = strtok(NULL,",");
Buf4 = strtok(NULL,"\"");
Remy Lebeau (TeamB) wrote:
Quote
"Barry" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Now to find a way to make it sensitive to the variable input...
>ie sometimes a separator and 2nd part, and sometimes not.

That is simple - Buf2 will be NULL if there was no separator.


Gambit


 

Re:strtok

"Barry" < XXXX@XXXXX.COM >wrote in message
Quote
If the first separator is sometimes a "@" and sometimes a ","
and different routines need to follow depending on which is
found, how would one go about that.
strtok() supports multiple tokens in a single call, but it does not give you
any information about which token it actually found. If you need that, then
you will have to scan through the string yourself. I suggest you use the
std::string class, as it has a find_first_of() method that can do the scan
for you. For example:
#include <string>
fgets(buffer, 200, disk);
std::string s(buffer);
std::string::size_type pos = s.find_first_of("@,");
if( pos != std::string::npos )
{
if( s[pos] == '@' )
{
// use routine 1
}
else
{
// use routine 2
}
}
else
// no token was found ...
Otherwise, you will have to read each character manually:
fgets(buffer, 200, disk);
char *ptr = buffer;
while( *ptr != '\0' )
{
if( *ptr == '@' )
{
// use routine 1
break;
}
else if( *ptr == ',' )
{
// use routine 2
break;
}
ptr = CharNext(ptr);
}
if( *ptr == '\0' )
// no token was found ...
Gambit
 

Re:strtok

"Barry" < XXXX@XXXXX.COM >wrote in message
Quote
Many thanks for the advice.

Following on from this:

If the first separator is sometimes a "@" and sometimes a "," and
different routines need to follow depending on which is found, how would
one go about that.

One method I have thought of would be to search the initial entire buffer
(string) for any occurance of "@", and then immediately assign to routine
1, as follows, failing which routine 2:

How does one search the initial buffer (string) for an occurance of "@"?
Make buffer a std::string and use the find functions declared
in basic_string.