Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: why char* is passed with value "0"?

  1. #11
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: why char* is passed with value "0"?

    Quote Originally Posted by tneva82 View Post
    I use what I'm most comfortable with. Never been comfortable with string class, even less with their ioclasses, haven't seen anything that does the job of strtok(which my program depends heavily. No strtok equilavent, no go. Is there equilavent of fscanf either? Another function that is absolutly _essential_ and used more than I dare to count. If there's not equilavent in c++ then it's just ugly situation when I have to redesign design from the scratch).



    I use tools that help me. C++ has tools I find useful so I use them. But if they don't have tools I need(like strtok. Point me to c++ function that does same job? I have tried to look but haven't found so far one) I use them. Simple as that. I use whatever allows me to get the job done most easily.
    With Boost, a tokenizer class exists that allow one to reap the tokens from a string.

    If you don't want to use boost, then here's strtok()... my version:
    Code:
    std::vector<std::string> strtok(const std::string& str, const std::string& delim)
    {
      std::vector<std::string> tokens;
    
      size_t pos = 0;
      size_t loc = str.find_first_of(delim, pos);
    
      if (loc == std::string::npos)
      {
        tokens.push_back(str);
      }
      else
      {
        while (loc != std::string::npos)
        {
          tokens.push_back(str.substr(pos, loc - pos));
          pos = loc + 1;
          loc = str.find_first_of(delim, pos);
        }
      }
    
      return tokens;
    }
    Example usage:
    Code:
    void displayString(const std::string& str)
    {
      std::cout << str << std::endl;
    }
    
    int main()
    {
      const std::string        line   = "abc*def*ghi*jkl*";
      std::vector<std::string> tokens = strtok(line, "*");
    
      std::for_each(tokens.begin(), tokens.end(), displayString);
    }
    Last edited by dwhitney67; March 5th, 2009 at 09:28 PM.

  2. #12
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: why char* is passed with value "0"?

    Quote Originally Posted by dwhitney67 View Post
    Get over the C standard library if you plan to program in C++. C++ features a rich library which solves most of the issues you write about, but since it seems that you don't want to progress your knowledge, then damn, program in C.
    Funny, I thought that was one of C++'s strengths. (Flamebait!)

    As for the OP:
    http://www.linuxforums.org/forum/lin...c-strings.html

    And I imagine you could write easily a home-made function that could split into the desired tokens. (Edit: lol, dwhitney already did)
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  3. #13
    Join Date
    May 2007
    Beans
    251

    Re: why char* is passed with value "0"?

    Quote Originally Posted by Can+~ View Post
    Good to see my post helping cross-forum users
    The Unforgiven

  4. #14
    Join Date
    Jun 2006
    Location
    Canada
    Beans
    133

    Re: why char* is passed with value "0"?

    Quote Originally Posted by dwhitney67 View Post
    If you don't want to use boost, then here's strtok()... my version:
    Code:
    std::vector<std::string> strtok(const std::string& str, const std::string& delim)
    {
      std::vector<std::string> tokens;
    
      size_t pos = 0;
      size_t loc = str.find_first_of(delim, pos);
    
      if (loc == std::string::npos)
      {
        tokens.push_back(str);
      }
      else
      {
        while (loc != std::string::npos)
        {
          tokens.push_back(str.substr(pos, loc - pos));
          pos = loc + 1;
          loc = str.find_first_of(delim, pos);
        }
    //ZNOTE: Need to add the final piece to vector!
      }
    
      return tokens;
    }
    Thanks for this code, dwhitney67! This is the way to do it in C++, folks. It's a clear and tidy solution to the problem of tokenizing or splitting a string using a delimiter in C++. The WWW is an unsemantic mess so I am adding a few search keywords to this thread. (o;

    One modification needs to be made: where you see my NOTE in the quoted code, we must add the remainder of the string to the vector.

    Suggestion:
    if (pos <= str.size()-1)
    tokens.push_back( str.substr(pos, str.size()-pos) );
    FOSS is the smart choice.

  5. #15
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: why char* is passed with value "0"?

    Quote Originally Posted by cogitordi View Post
    Suggestion:
    if (pos <= str.size()-1)
    tokens.push_back( str.substr(pos, str.size()-pos) );
    Sorry about that; you are correct.

    When I tested, I assumed the string was delimited by one of the visible chars in the delim string. I forgot to consider the null-byte.

    If you want to shorten the code a bit, use string's substr() default parameter for the second arg.
    Code:
    if (pos < str.size())
    {
       tokens.push_back(str.substr(pos));
    }

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •