scanning till ,

Posted on 2009-04-22
Last Modified: 2012-05-06
say I have the following:

this bottle, is, only, a, test

what would be the best way to extract each string that is separated by a comma?... lets say that this line is read line by line from a file
Question by:kuntilanak
    LVL 86

    Expert Comment

    E.g something like the following:
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    int split_text(string strIn, const char cDelim, vector<string>& vResult) {
       int nPos = 0;
       int nCount = 0;
       int nFound;
       string strToken;
       while(1) {
          nFound = strIn.find(cDelim,nPos);
          if (-1 == nFound)  {
            strToken = strIn.substr(nPos,strIn.length() - nPos);
          strToken = strIn.substr(nPos,nFound - nPos);
          nPos = nFound + 1;
       return nCount;
    int main () {
      vector<string> vResult;
       int n = split_text("this bottle, is, only, a, test",',',vResult);
       for ( int i = 0; i < n; ++i)
          cout << vResult[i] << endl;

    Open in new window

    LVL 86

    Accepted Solution

    BTW, another option would be 'strtok()', e.g.
    // crt_strtok.c
    // compile with: /W1
    // In this program, a loop uses strtok
    // to print all the tokens (separated by commas
    // or blanks) in the string named "string".
    #include <string.h>
    #include <stdio.h>
    char string[] = "this bottle, is, only, a, test";
    char seps[]   = ",";
    char *token;
    int main( void )
       printf( "Tokens:\n" );
       // Establish string and get the first token:
       token = strtok( string, seps ); // C4996
       // Note: strtok is deprecated; consider using strtok_s instead
       while( token != NULL )
          // While there are tokens in "string"
          printf( " %s\n", token );
          // Get next token: 
          token = strtok( NULL, seps ); // C4996

    Open in new window


    Author Comment

    oh why have I forgot strtok... thanks if I remember that exists I would have used it first place.. thank you!
    LVL 86

    Expert Comment

    You're most welcome ;o)

    BTW, don't forget to close your questions once your problem is solved...

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    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.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now