std::string and strtok

Posted on 2006-07-19
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?

Question by:Unimatrix_001
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 86

Expert Comment

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) {

// ...



Author Comment

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?
LVL 86

Accepted Solution

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.

Author Comment

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


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 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