473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

strtok as string replace function problem ?

Hi,

I have selected strtok to be used in my string replacement function.
But I lost the last token, if there is one.

This string would be replaced

select "name", "vorname", "userid", "passwort" from "users" order by
"users"

by this one:

select "name", "vorname", "userid",
"passwort" from "users" order by "users

Thus the last quot is lost.

Is there something missing in the code ?

Thanks, Lothar

lb_I_String& LB_STDCALL lbString::replace(const char* toReplace, const
char* with) {
// Don't worry about my object variables :-)
UAP_REQUEST(getModuleInstance(), lb_I_String, rep)

// the string
char* token = strtok(stringdata, toReplace);

if ((token != NULL) && (token != stringdata)) {
*rep += with;
}

while(token != NULL)
{
*rep += token;
token = strtok(NULL, toReplace);
if (token != NULL) *rep += with;
}

setData(rep->charrep());

return *this;
}

Jun 5 '07 #1
8 4459
In article <11**********************@q69g2000hsb.googlegroups .com>,
Lothar Behrens <lo************@lollisoft.dewrote:
>I have selected strtok to be used in my string replacement function.
But I lost the last token, if there is one.
>This string would be replaced
>select "name", "vorname", "userid", "passwort" from "users" order by
"users"
>by this one:
>select &quot;name&quot;, &quot;vorname&quot;, &quot;userid&quot;,
&quot;passwort&quot; from &quot;users&quot; order by &quot;users
>Thus the last quot is lost.
>Is there something missing in the code ?
>lb_I_String& LB_STDCALL lbString::replace(const char* toReplace, const
char* with) {
Are you sure this isn't C++? The :: is suspicious, and the & cannot
occur in that position in C.
> // Don't worry about my object variables :-)
// the string
char* token = strtok(stringdata, toReplace);
The first call in the sequence searches the string pointed to
by s1 for first character that is not contained in the current
separator string pointed to by s2. If no such character is
found, then there are no tokens in the string pointed to by s1
and the strtok function returns a null pointer."

What this implies is that if the string ends in the delimiter,
then strtok() will *not* return a pointer to the empty line.
srtrok() effectively cannot distinguish between the case of
the string ending in \0 and the string ending in any number
of the given delimiters: you have to do that yourself by
keeping track of where the last token ends and comparing that to
the known position of the end of the string.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jun 5 '07 #2
On 5 Jun., 22:54, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
>
Are you sure this isn't C++? The :: is suspicious, and the & cannot
occur in that position in C.
Yes, sorry. But I put the complete function to indicate C++ code.
Because there
are also + operators used :-)
>
What this implies is that if the string ends in the delimiter,
thenstrtok() will *not* return a pointer to the empty line.
srtrok() effectively cannot distinguish between the case of
the string ending in \0 and the string ending in any number
of the given delimiters: you have to do that yourself by
keeping track of where the last token ends and comparing that to
the known position of the end of the string.
Using a flag and determining a trailing token would then help...

Thanks

Lothar

Jun 5 '07 #3
Lothar Behrens wrote:
On 5 Jun., 22:54, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:

Are you sure this isn't C++? The :: is suspicious, and the & cannot
occur in that position in C.

Yes, sorry. But I put the complete function to indicate C++ code.
Which means you are in the wrong newsgroup. You want comp.lang.c++.
Actually, you also probably want to do the problem quite differently.


Brian

Jun 5 '07 #4
Yes, sorry. But I put the complete function to indicate C++ code.
>
Which means you are in the wrong newsgroup. You want comp.lang.c++.
Actually, you also probably want to do the problem quite differently.

Brian
Hmmm,

didn't you read the subject ?

I have asked for the strtok function. This is a C function and
therefore NOT wrong here.
If I do ask this question in comp.lang.c++, they probably tell me I am
wrong there.

The posting rules here are too strong :-(

Lothar
Jun 5 '07 #5
Lothar Behrens wrote:
Yes, sorry. But I put the complete function to indicate C++ code.
Which means you are in the wrong newsgroup. You want comp.lang.c++.
Actually, you also probably want to do the problem quite
differently.

Brian

Hmmm,

didn't you read the subject ?
Of course.
I have asked for the strtok function. This is a C function and
therefore NOT wrong here.
It's also a C++ function, and that's the language you're working in.
If I do ask this question in comp.lang.c++, they probably tell me I am
wrong there.
No. Well some might, but I'll be there to correct them. Besides, as I
said, you don't want to do it that way. Besides, I see that you DID
post over there, so you can't be too worried.

I've given you a better solution there. If you whinge that it didn't
answer the question, I'll severely killfile you about the head and
shoulders.
The posting rules here are too strong :-(
Not really. Ask questions about C. Lurk and/or read over a few weeks
worth of posts.

Brian
Jun 5 '07 #6
Lothar Behrens wrote, On 05/06/07 22:56:
>>Yes, sorry. But I put the complete function to indicate C++ code.
Which means you are in the wrong newsgroup. You want comp.lang.c++.
Actually, you also probably want to do the problem quite differently.

Brian

Hmmm,

didn't you read the subject ?

I have asked for the strtok function. This is a C function and
therefore NOT wrong here.
If I do ask this question in comp.lang.c++, they probably tell me I am
wrong there.

The posting rules here are too strong :-(
Generally deciding to use a specific function to solve a problem and
then trying to solve it is the wrong approach. Work out the best way to
solve the function with all of the tools available, instead of just one.
If you do have a specific reason for wanting to use strtok them tell us.
--
Flash Gordon
Jun 5 '07 #7
On 6 Jun., 00:33, Flash Gordon <s...@flash-gordon.me.ukwrote:
If you do have a specific reason for wanting to use strtok them tell us.
This is an excerpt from http://www.openwatcom.org/index.php/...L#.3Cstring.3E

OWSTL is only available in Open Watcom v1.4 or later releases.
Although a few STL features do exist in Open Watcom v1.3, STL support
in v1.3 is very minimal. If you wish to experiment with OWSTL you will
need to install or upgrade to v1.4 or later. Naturally we recommend
that you use the latest version of Open Watcom possible. Note also
that copying the OWSTL headers into an installation of a pre-v1.4
release is not likely to work. The current implementation of OWSTL
depends on a number of namespace/template bug fixes that occured
during the development of Open Watcom v1.4.

I still use OW 1.3, because I had problems to move to 1.4. I'll try
the latest if I have time.

Regards, Lothar
Jun 6 '07 #8
Lothar Behrens wrote:
On 6 Jun., 00:33, Flash Gordon <s...@flash-gordon.me.ukwrote:
>If you do have a specific reason for wanting to use strtok them
tell us.

This is an excerpt from http:
//www.openwatcom.org/index.php/Open_Watcom_STL#.3Cstring.3E

OWSTL is only available in Open Watcom v1.4 or later releases.
Although a few STL features do exist in Open Watcom v1.3, STL
support
None of these things are the normal tools available. C has long
since been standardized, including the library. Read the standard
and use those tools. Otherwise you are off-topic on c.l.c.

--
<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 6 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
17
by: bofh1234 | last post by:
I need to delimit a string. The delimiters are a semicolon and comma. When I run the program I get a segmentation fault on the first strtok. I followed the examples of others and from my old C...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
18
by: Robbie Hatley | last post by:
A couple of days ago I dedecided to force myself to really learn exactly what "strtok" does, and how to use it. I figured I'd just look it up in some book and that would be that. I figured...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
26
by: ryampolsky | last post by:
I'm using strtok to break apart a colon-delimited string. It basically works, but it looks like strtok skips over empty sections. In other words, if the string has 2 colons in a row, it doesn't...
5
by: Kelly B | last post by:
I need a function which returns me a "word" from a given string and then sets the pointer to the next one which is then retrieved during further calls to the function. I think strtok( ) is the...
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.