By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,146 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,146 IT Pros & Developers. It's quick & easy.

How to use "strtok" on the string?

P: 8
I face a big problem to use 'strtok' on the string. Message from the output is "error C2664: 'strtok' : cannot convert parameter 1 from 'std::string' to 'char *'". Can anybody give some suggessions about this matter?or may be for other approaches. My objective is to search "\n" in the string.

this is my code;

Expand|Select|Wrap|Line Numbers
  1. string x;
  2. char delims[]="\n";
  3. char *result=NULL;
  4.  
  5.                while(getline(infile, x ,'\n'))
  6.     {
  7.         result =strtok(x,delims);
  8.         istringstream ss(x);
  9.                                {
  10.                                   ..........
  11.                                }
Feb 23 '07 #1
Share this Question
Share on Google+
5 Replies


DeMan
100+
P: 1,806
I'm not familiar with strtok (nor am I particularly familiar with Strings in c/c++) but I think..... a string is a char array. a char array can be accessed by a pointer to the FIRST char.
thus, it appears strtok wants a pointer to a string (as in a pointer to a pointer to a char), so you either need to declare x as
string *x
OR (if you want it to be modified) to pass in its' reference ie
doThis(&x) -> the & means something along the lines of "i give you a pointer to this variable so that I can use this variable later" ....so if you need to use the variable at x (and you aren't allocating the memory for it) you pass a 'reference to the value' x.
Feb 23 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
I face a big problem to use 'strtok' on the string. Message from the output is "error C2664: 'strtok' : cannot convert parameter 1 from 'std::string' to 'char *'". Can anybody give some suggessions about this matter?or may be for other approaches. My objective is to search "\n" in the string.

this is my code;

Expand|Select|Wrap|Line Numbers
  1. string x;
  2. char delims[]="\n";
  3. char *result=NULL;
  4.  
  5.                while(getline(infile, x ,'\n'))
  6.     {
  7.         result =strtok(x,delims);
  8.         istringstream ss(x);
  9.                                {
  10.                                   ..........
  11.                                }
Use x.c_str() to give strtok the char* representation of the string.
Feb 23 '07 #3

P: 8
arf
Use x.c_str() to give strtok the char* representation of the string.
How to do that ? Can you show me?
Feb 27 '07 #4

Expert 100+
P: 1,510
How to do that ? Can you show me?
have a look at thread
http://www.thescripts.com/forum/thread607870.html
Feb 27 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
arf:

In strtok, instead of using x, use x.c_str() and your program should work.
Feb 27 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.