Solved

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

Posted on 2006-06-29
11
1,076 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

810 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