1

i am using c++ win32 Api.

i want to split the character using delimiter.

that character like "CN=USERS,OU=Marketing,DC=RAM,DC=COM".

i want to split the charcter into after the first comma(,).that means i need only

OU=Marketing,DC=RAM,DC=COM.

i already tried strtok function,but it split CN=USERS only.

How can i achieve this?

Arjun babu
  • 607
  • 2
  • 13
  • 42

2 Answers2

3

Try below code, you should be able to get each item(separated by ',') easily: strtok version:

char domain[] = "CN=USERS,OU=Marketing,DC=RAM,DC=COM";
  char *token = std::strtok(domain, ",");
  while (token != NULL) {
      std::cout << token << '\n';
      token = std::strtok(NULL, ",");
  }

std::stringstream version:

std::stringstream ss("CN=USERS,OU=Marketing,DC=RAM,DC=COM");
std::string item;
while(std::getline(ss, item, ',')) 
{
  cout << item << endl;
}

Have a look at std::getline() http://en.cppreference.com/w/cpp/string/basic_string/getline

billz
  • 44,644
  • 9
  • 83
  • 100
  • @ billz :thanks for ur reply,but i cant use std::stringstream and etc.can u give other windows function like strtok,strcmp etc. – Arjun babu Nov 19 '12 at 05:35
  • 1
    @sanju: strtok, strcmp are not windows functions. it's part of standard CRT (C runtime library) – pagra Nov 19 '12 at 09:57
1

Using strchr makes it quite easy:

char domain[] = "CN=USERS,OU=Marketing,DC=RAM,DC=COM";
char *p = strchr(domain, ',');
if (p == NULL)
{
    // error, no comma in the string
}
++p; // point to the character after the comma
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351