CString을 이용한 Token처리

다음은 CString을 이용한 Token처리 방법 예제입니다.

 Example

 
 
CString str(_T("%First Second#Third")); 
CString resToken; 
int curPos= 0; 
resToken= str.Tokenize("% #",curPos); 

while (resToken != "")
 { 
        printf("Resulting token: %s\n", resToken); 
        resToken= str.Tokenize("% #",curPos); 
}; 
 

 Output

 Resulting Token: First
 Resulting Token: Second
 Resulting Token: Third

Posted by chungki

2009/09/02 10:22 2009/09/02 10:22
,
Response
No Trackback , No Comment
RSS :
http://www.chungki.net/tc/rss/response/285

Token 사용예제

"1, 2, 3, 5, 6"같은 문자열로부터 숫자값을 꺼내오기 위해서는 다음과 같은 방법을 사용할 수 있다.

char* str="1,2,3,5,6";

int a[5];
sscanf(str,"%d,%d,%d,%d,%d",&a[0],&a[1],&a[2],&a[3],&a[4]);


하지만 문자열부터 꺼내오는 데이터개수가 고정적이지가 않을때는 sscanf사용하는게 불편하다. 그리고 그래서 Token을 이용하면 개수가 정해지지 않은 데이터를 가져올 수 있다. 다음은 token의 사용예제이다.

 
char string[]= "A string of ,,tokens and some more tokens"; 
char seps[]=" ,w"; 
char *token;

void main(void) 
{     
     printf("%s Tokens:", string);     
     token=strtok(string,seps);     
     while (token!=NULL)     
     {     
            printf(" %s",token);        
            token=strtok(NULL,seps);     
      } 
}

 Output

 A string of ,,tokens
and some more tokens

 Tokens:
A
String
of
tokens
and
some
more
tokens

Posted by chungki

2009/09/02 10:12 2009/09/02 10:12
Response
No Trackback , No Comment
RSS :
http://www.chungki.net/tc/rss/response/283

Token의 사용예제


char string[]= "A string of ,,tokens and some more tokens"; 
char seps[]=" ,w"; 
char *token; 

void main(void) 
{     
     printf("%s Tokens:", string);     
     token=strtok(string,seps);     
     while (token!=NULL)     
     {     
            printf(" %s",token);        
            token=strtok(NULL,seps);     
      } 
}


Output

A string of ,,tokens
and some more tokens

Tokens:
A
String
of
tokens
and
some
more
tokens

Posted by chungki

2005/04/12 13:14 2005/04/12 13:14
Response
No Trackback , No Comment
RSS :
http://www.chungki.net/tc/rss/response/109


블로그 이미지

Nice !!!!!!!!

- chungki

Notices

Archives

Authors

  1. chungki

Calendar

«   2012/05   »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Site Stats

Total hits:
73376
Today:
7
Yesterday:
20