Solved

How to tokenize a comma delimited _bstr_t typed srting into a string array?

Posted on 2006-06-29
11
1,080 Views
Last Modified: 2013-11-20
I would like someone to show me what are the "best practices" for parsing a _bstr_t string into a string array. My program works well up to the point of getting the "responseText" into a _bstr_t typed string. it shows in the MessageBox as "1,1,3,Processed OK!,00002,,,,,Succeeded,,,Qwwfdd1A3A,,,". I tried various way to parse this data into a string array but I am very clumsy at this. Can some one please help me out?

Up to the lines, below, my C++ program works just fines.
  .
  .
   _bstr_t tempStr = pXMLHTTPReq->responseText;
   MessageBox(NULL, tempStr, "readystate", MB_OK);

After the_bstr_t string is tokenized, it should look like this :

aString[0]= "1"
aString[1]= "1"
aString[2]= "3"
aString[3]= "Processed OK!"
aString[4]= "00002"
aString[5]= ""
 .
 .
aString[10]="Succeeded"
 .
 .
0
Comment
Question by:ablazso
  • 4
  • 4
  • 3
11 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17015037
What about just using

#include <string.h>
#include <malloc.h>


//...

   _bstr_t tempStr = pXMLHTTPReq->responseText;

   char seps[] = ",";
   char* token;
   CStringArray aString;
   char* string = strdup ( tempStr );

   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      aString.Add ( token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }

   free ( string );

?
0
 

Author Comment

by:ablazso
ID: 17015906
First of all, thanks for your suggestion.
I added the above lines of code, however I also added #include <afxcoll.h> (otherwise CStringArray would not compile), but now I have another fatal error when I try to rebuild the solution:

c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxv_w32.h(18): fatal error C1189: #error :  WINDOWS.H already included.  MFC apps must not #include <windows.h>

What am I doing wrong?  I do not have #include <windows.h> in my code!
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17016009
have you include stdafx.h ? its having generally files

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h>            // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>                  // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

-MAHESH
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ablazso
ID: 17018211
Given the suggested tokenizer code above, what .h file should be included so that CStringArray would not com up as undfinded during comilation? Thank you for help!
0
 
LVL 86

Expert Comment

by:jkr
ID: 17018241
Just place

#include <afxcoll.h>

in your 'stdafx.h' and that should work.
0
 

Author Comment

by:ablazso
ID: 17018257
Currently I have:
#include <afxwin.h>
#include <string.h>
#include <malloc.h>
these includes in my program, so where is windows.h commng form?
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17018261
include standard stdafx.h file having above include files.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17018298
ONLY include stdafx.h having above files
0
 
LVL 86

Expert Comment

by:jkr
ID: 17018356
Is that a fixed/max count of elements in the string you want to split?
0
 

Author Comment

by:ablazso
ID: 17018855
No, I just want to tokenize the tempStr string received via:
  _bstr_t tempStr = pXMLHTTPReq->responseText;
into a string array.

Incidentally I do not have stdafx.h on my system.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17018898
>>Incidentally I do not have stdafx.h on my system.

You should have one in your project directory, this is one generated when you create the project. If not, there's something going seriously wrong.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question