Solved

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

Posted on 2006-06-29
11
1,066 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now