Solved

std::string and strtok

Posted on 2006-07-19
4
359 Views
Last Modified: 2008-02-26
Hi there,

I'm attempting to use strtok with std::string, but unfortunately I can't convert std::string to (char *). I can do (const char *), but strtok ain't accepting that. Any ideas anybody?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17141028
You even should not use 'c_str()', since 'strtok()' inserts null bytes where it encounters a separator, thus altering the string's buffer, which is not a good idea. Make a copy of the char buffer before you use 'strtok()', e.g.

#include <string>
#include <string.h>

//..

std::string str = "Just some text";

char* buf = strdup(str.c_str());

char* token = strtok(buf," ");

while (token) {

// ...

}

free(buf);
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 17141112
Many thanks! :) Although one question, how come you're using string and string.h together? I thought .h was the older version (and obselete) compared with string?
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 17141141
No, not in this case. Even though you are right about e.g. 'iostream' and 'iostream.h', 'string.h' is a plain C header file that - among others - has e.g. 'strtok()', 'strdup()' and 'strstr()'. This file is not related to STL's 'string' header.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 17141165
Ah, okay I understand. Enjoy your points. (-:

Uni
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now