parse the login name

I have login name like domain\username, I tried to use strtok to parse it. However, strtok can not recognize "\". For example,
char ustring[] = "domain\username";
sLogin = strtok(ustring, "\\" );
sDomain = sLogin;
while( sLogin != NULL )
{
sUser = sLogin;
sLogin = strtok( NULL, "\\" );
}

Output both sDomain and sUser are domainusername

However, if I change ustring to the following by adding one more "\"
char ustring[] = "domain\\username";

it will parse the login correctly like
sDomain: domain
sUser: username

So any idea how to add one more backslash into login name

Thanks in advance.


Answer this question

parse the login name

  • RBrady

    seems there is some problem to post the source code, here it is

    for (i=0; i<strlen(sLogin); i++){

    if(sLoginIdea != char(92)&& bDomain == false ){

    sDomain=sLoginIdea;

    }


    else{

    bDomain == true;

    sUser[j]=sLoginIdea;

    }
    }


  • vinothr

    oops, again

    for (i=0; i<strlen(sLogin); i++){

    if(sLogin[ i ] != char(92)&& bDomain == false ){

    sDomain[ i ]=sLogin[ i ];

    }


    else{

    bDomain == true;

    sUser[ j ]=sLogin[ i ] ;

    }
    }


  • Lexite

    Oshah, Thanks for the reply. login info is entered in user interface and it is passed in as the parameter. when login parameter char array gets it, it will be like "domain\user". so I have to change it to "domain\\user". But I do not know how to do it. I even think about the following, but it is not working

    for (i=0; i<strlen(sLogin)+1; i++){

    if(sLoginIdea != char(92)&& bDomain == false ){

    sDomainIdea=sLoginIdea;

    }

    else{

    bDomain == true;

    sUser[j]=sLoginIdea;

    }


  • Genja_Eugeny

    The problem is solved. The cause of the problem is related to the data type of passed-in login name. It is wide character. So I have to use wcstok not strtok. Thanks
  • A2324_1

    I think you didn't get what OShah meant. \\ translates to \. Your input string needs to have two slashes - which get treated as a single slash.

    SamTran wrote:

    oops, again

    for (i=0; i<strlen(sLogin); i++){

    if(sLogin[ i ] != char(92)&& bDomain == false ){

    sDomain[ i ]=sLogin[ i ];

    }


    else{

    bDomain == true;

    sUser[ j ]=sLogin[ i ] ;

    }
    }



  • KimberlyL

    As you know "domain\\username"; translates to domain\username once compiled.

    Your logic is correct... it's just the input string that's wrong. When you change ustring so that it comes from the user or external source, your code will parse it correctly.



  • parse the login name