How to use "strtok" on the string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arf
    New Member
    • Feb 2007
    • 8

    How to use "strtok" on the string?

    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;

    Code:
    string x;
    char delims[]="\n";
    char *result=NULL;
    
                   while(getline(infile, x ,'\n'))
    	{
    		result =strtok(x,delims);
    		istringstream ss(x);
                                   {
                                      ..........
                                   }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Originally posted by arf
      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;

      Code:
      string x;
      char delims[]="\n";
      char *result=NULL;
      
                     while(getline(infile, x ,'\n'))
      	{
      		result =strtok(x,delims);
      		istringstream ss(x);
                                     {
                                        ..........
                                     }
      Use x.c_str() to give strtok the char* representation of the string.

      Comment

      • arf
        New Member
        • Feb 2007
        • 8

        #4
        Originally posted by Ganon11
        Use x.c_str() to give strtok the char* representation of the string.
        How to do that ? Can you show me?

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by arf
          How to do that ? Can you show me?
          have a look at thread
          http://www.thescripts. com/forum/thread607870.ht ml

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            arf:

            In strtok, instead of using x, use x.c_str() and your program should work.

            Comment

            Working...