Thread: TCHAR array parsing??

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    2

    Question TCHAR array parsing??

    Hi,

    I've got some problems parsing a string. (actually it's a PTACHAR)

    Here is the format of my string:

    "RED 2132 4365 2341 53 23.0 BLUE 2345 4324 23 35 121 552 432 124"

    RED and BLUE are like keywords, I have to use the values after in
    order to update some field in my application.

    How can I parse this String "word" by "word"? My idea is walk in the
    string until there is a space --> " " and analyse the data. But I
    don't know how to do that??

    Here is my method where I want to parse:
    Code:
    void MyClass::parseMessage(LPCTSTR message)
    {
    	//parse string if not empty
    	if (strlen(message)!=0) {
    
              //do the paring here
    
    	}
    }

    Thanks you for your Help

    LIO
    Last edited by dude1978; 05-21-2003 at 01:45 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    If you are using a TCHAR string, then use the corresponding string lib _tcslen instead of strlen...then _tcscat (conditional version of strcat) to tokenize the string

    All can be found in TCHAR.h

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    2
    Actually I'm receiving my string in a PTCHAR format.

    But my question is how to tokenize that string with _tcscat ?? Sorry this is probably a very silly question but I'm new in c++ dev.

    Thanks in advance for your answer

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    use _tcscat eaxactly as you would use strcat.

    if you need guidance on how to use strcta, then try a board search and you will find all you need

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Try strtok() or _tcstok() something like this.
    Code:
    #include <TCHAR.H>
    #include <stdio.h>
    
    main( void )
    {
       TCHAR string[] = "RED 2132 4365 2341 53 23.0 BLUE 2345 4324 23 35 121 552 432 124";
       char seps[]   = " ";
       char *token;
       int Col = 0;
     
       token = _tcstok( string, seps );
       while( token != NULL )
       {
           if(!strcmp(token, "RED")) 
              Col = 1;
           else if(!strcmp(token, "BLUE"))
              Col = 2;
           else if(Col == 1)
    	      printf("RED  %s\n", token);
           else if(Col == 2)
    	      printf("BLUE %s\n", token);
         	  
          token = _tcstok( NULL, seps );
       }
       return 0;
    }

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Doh...sorry...my bad..I meant strtok...not strcat

  7. #7
    Cat
    Guest
    Originally posted by Scarlet7
    Try strtok() or _tcstok() something like this.

    The code, as written, won't handle Unicode (you'd get compiled errors because of mixing chars and TCHARs. And there were a couple of minor errors in the code. Changes below. The below code works properly in both ANSI and UNICODE.

    Code:
    #include <TCHAR.H>
    #include <stdio.h>
    
    main( void )
    {
         TCHAR string[] = _T("RED 2132 4365 2341 53 23.0 BLUE 2345 4324 23 35 121 552 432 124");
        TCHAR seps[]   = _T(" ");
        TCHAR * token;
        int Col = 0;
    
        token = _tcstok( string, seps );
        while( token != NULL )
        {
            if(!_tcscmp(token, _T("RED")))
                _tprintf(_T("Red token\n"));
            else if (!_tcscmp(token, _T("BLUE")))
                _tprintf(_T("Blue token\n"));
            else
                _tprintf(_T("Token: %s\n"),token);
            token = _tcstok(NULL,seps);
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  2. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  3. parsing words out to pointer array
    By da_fall_guy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2003, 12:21 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Parsing array elements
    By DMaxJ in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2001, 11:28 AM