Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse the login name

Status
Not open for further replies.

JohnQK

Programmer
Mar 17, 2006
5
CA
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.
 
> domain\username;
This is what the user would type in

> char ustring[] = "domain\\username";
This is what you must write to embed the same string inside your source code (I assume for testing).

The \\ rule only applies to quoting \ within a string inside your source code. If the user wants to type in a \, then that is all they need to type in - a single \



--
 
if the user type in "domain\username" in login page, the string passed in is "domain\username". The strtok can not parse this string correctly. That is why I need to add one more "\", but I do not know how to do it in the source code.
 
I've no idea what you're seeing.

This works like I expected it would
Code:
#include <stdio.h>
#include <string.h>

int main(void) {
  char buff[BUFSIZ];
  char *domain, *user;
  char *nl;

  printf( "Enter domain\\username\n" );
  fgets( buff, BUFSIZ, stdin );
  nl = strchr( buff, '\n' );
  if ( nl ) { *nl = '\0'; }
  printf( "You typed --%s--\n", buff );

  domain = strtok( buff, "\\" );
  user   = strtok( NULL, "\\" );
  printf( "Domain=%s, user=%s\n", domain, user );
  return 0;
}



$ gcc -W -Wall -ansi -pedantic -O2 foo.c
$ ./a.out
Enter domain\username
hello\world
You typed --hello\world--
Domain=hello, user=world

--
 
Salem, Thanks for the reply. However, login info is entered in user interface no windows console, it is passed in as the parameter. when buff char array got it, it will be like "domain\user". If I changed you code based on this scenario, it will not work,

#include <stdio.h>
#include <string.h>
#include "stdafx.h"

int main(void) {
char buff[BUFSIZ] = "domain\user";
char *domain, *user;
char *nl;

//printf( "Enter domain\\username\n" );
//fgets( buff, BUFSIZ, stdin );
//nl = strchr( buff, '\n' );
//if ( nl ) { *nl = '\0'; }
//printf( "You typed --%s--\n", buff );


domain = strtok( buff, "\\" );
user = strtok( NULL, "\\" );
printf( "Domain=%s, user=%s\n", domain, user );
return 0;
}
 
Code:
char buff[BUFSIZ] = "domain\user";
This code will not produce a string of "domain\user". The compiler treats any \ characters in literal strings as excape characters.
Code:
char buff[BUFSIZ] = "domain\\user";
The code above will produce a string of "domain\user".
 
> If I changed you code based on this scenario, it will not work,
Because you're FAILING to recognise the difference between what a user would be expected to type in at a prompt (whether it's a console prompt or a GUI dialog, it doesn't matter)
[tt]domain\user[/tt]

and what you have to quote if you want to put the string inside your source code.
[tt]char buff[BUFSIZ] = "domain\\user";[/tt]

--
 
that is where my problem is. the login parameter is like "domain\user". So I have to change it to "domain\\user". I tried the following it is not working

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

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

sDomain=sLogin;

}


else{

bDomain == true;

sUser[j]=sLogin ;

}
}
 
OK, simple question time.
Is the string typed in by the user or stored in the program?

--
 
Salem. Thanks for you help on this issue. 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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top