?
Solved

std::string and strtok

Posted on 2006-07-19
4
Medium Priority
?
391 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
[X]
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
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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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