Solved

Using SpanExcluding

Posted on 2002-05-24
16
1,224 Views
Last Modified: 2007-11-27
Using MSVC++ 6.0 How would I assign the data in the string  separated by spaces to a variable using SpanExcluing(" ")???  There can be up to 3 spaces between each value.

CString src, str[20];
int x,y;

src = "2026461 37 103 293996800 -894013888 1613 2 -174 -2
       2 20  26 461 100    1 2 -29 195 10";

for(x=0; x< 20; x++)
{
   y = src.Find(" ");
   str[x] = src.SpanExcluding(" ");
}
   

     
     
0
Comment
Question by:adatd
  • 6
  • 3
  • 3
  • +4
16 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Your question is ambiquous.

I don't think SpanExcluding is the right function you need for your required ouput.

Please post an example of what you want the out come to be.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
'strtok()' does what you want to do:

char* token = " ";
char* cstr;
char* p;
CString src, str[20];
int x,y;

src = "2026461 37 103 293996800 -894013888 1613 2 -174 -2
      220 26 461 100 1 2 -29 195 10";

cstr = (LPCTSTR) src;

if ( p = strtok ( cstr, token) {
 str[0] = p;
} else {
 // no blanks
}

for(x=1; x< 20; x++)
{
  if ( p = strtok ( NULL, token)) {
  str[x] = p;
  } else {
    break;
  }
}
 
0
 

Author Comment

by:adatd
Comment Utility
I need the values in src to be assigned as
str[0]= "2026461";
str[1] = "37";
str[2] = "103";
str[3] = "293996800";
str[4] = "-894013888";
str[5] = "1613";
str[6] = "2";
str[7] = "-174";
str[8] = "-2";
str[9] = "220";
str[10] = "26";
str[11] = "461";
str[12] = "100";
str[13] = "1";
str[14] = "2";
str[15} = "-29";
str[16] = "195";
str[17] = "10";

I want it to assign values until it comes to a '\n';
when it comes to a newline than i want it to break out of the loop;
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
So, try my suggestion :o)
0
 

Author Comment

by:adatd
Comment Utility
I need to do it using SpanExcluding in MFC
0
 

Author Comment

by:adatd
Comment Utility
What I need to do is read a CString and assign the data delimited by a space to variables using the SpanExcluding function in MFC.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Why would you restrict yourself on using 'SpanExcluding()'?
0
 

Author Comment

by:adatd
Comment Utility
I need some type of function in MFC I don't care which function it is but it has to be a MFC function.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
Comment Utility
Is this homework?
0
 

Author Comment

by:adatd
Comment Utility
No I am trying to learn MFC
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
MFC by itself does not have the functionality you'r looking for.

Part of learning MFC, is knowning how to mix standard-C/C++ with MFC specific code.
0
 
LVL 4

Accepted Solution

by:
mblat earned 50 total points
Comment Utility
What client wants, client gets.... :-)

AfxExtractSubString is your answer....  It doesn't seems to be very well documented, but from what I see it looks like:

CString szSource = "111 222";
CString cs1,cs2;
AfxExtractSubString(cs1,szSource,1,' ');
AfxExtractSubString(cs2,szSource,2,' ');

or something like that - search msdn.microsoft.com for more samples....

Hope it helps..

0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Here is an unutterably DISGUSTING function that I wrote.  I am simply speechless with contempt for myself that I even THOUGHT of writing it.

The only problem is that it works perfectly for my purposes and it saves me lots of hassle when I need to rip a commaa-delimited string into several strings.

(Note: I'm sure this will look terrible on this page.  Copy it into a C++ source file and everything lines up nicely).

You need to declare the fn like this in a header:

void    BreakOutStrings( LPCSTR szSrc, CString* ps1,    CString* ps2=0,  CString* ps3=0,  CString* ps4=0,  CString* ps5=0,  CString* ps6=0,  CString* ps7=0,  CString* ps8=0,  CString* ps9=0,  CString* ps10=0,
                                                   CString* ps11=0, CString* ps12=0, CString* ps13=0, CString* ps14=0, CString* ps15=0, CString* ps16=0, CString* ps17=0, CString* ps18=0, CString* ps19=0, CString* ps20=0,
                                                   CString* ps21=0, CString* ps22=0, CString* ps23=0, CString* ps24=0, CString* ps25=0, CString* ps26=0, CString* ps27=0, CString* ps28=0, CString* ps29=0, CString* ps30=0
                                    );


-=-=-=-=-=-=-=-=-=-=-==--=-=-=-=-=-=-=-=-=-=-=-=
Here's the function:
-=-=-=-=-=-=-=-=-=-=-==--=-=-=-=-=-=-=-=-=-=-=-=

void BreakOutStrings( LPCSTR szSrc, CString* ps01,      CString* ps02/*=0*/, CString* ps03/*=0*/, CString* ps04/*=0*/, CString* ps05/*=0*/,
                                              CString* ps06/*=0*/,CString* ps07/*=0*/, CString* ps08/*=0*/, CString* ps09/*=0*/, CString* ps10/*=0*/,
                                                      CString* ps11/*=0*/,CString* ps12/*=0*/, CString* ps13/*=0*/, CString* ps14/*=0*/, CString* ps15/*=0*/,
                                                      CString* ps16/*=0*/,CString* ps17/*=0*/, CString* ps18/*=0*/, CString* ps19/*=0*/, CString* ps20/*=0*/,
                                                      CString* ps21/*=0*/,CString* ps22/*=0*/, CString* ps23/*=0*/, CString* ps24/*=0*/, CString* ps25/*=0*/,
                                                      CString* ps26/*=0*/,CString* ps27/*=0*/, CString* ps28/*=0*/, CString* ps29/*=0*/, CString* ps30/*=0*/
                                                      )
{
      CString s= szSrc; s+= ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
      CString sTmp;
      
                          if (ps02) *ps02=""; if (ps03) *ps03=""; if (ps04) *ps04="";      if (ps05) *ps05="";      
      if (ps06) *ps06="";      if (ps07) *ps07=""; if (ps08) *ps08=""; if (ps09) *ps09="";      if (ps10) *ps10="";      
      if (ps11) *ps11="";      if (ps12) *ps12=""; if (ps13) *ps13=""; if (ps14) *ps14="";      if (ps15) *ps15="";      
      if (ps16) *ps16="";      if (ps17) *ps17=""; if (ps18) *ps18=""; if (ps19) *ps19="";      if (ps20) *ps20="";      
      if (ps21) *ps21="";      if (ps22) *ps22=""; if (ps23) *ps23=""; if (ps24) *ps24="";      if (ps25) *ps25="";
      if (ps26) *ps26="";      if (ps27) *ps27=""; if (ps28) *ps28=""; if (ps29) *ps29="";      if (ps30) *ps30="";

      int nPos=0;

      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos);           *ps01= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps02) *ps02= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps03) *ps03= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps04) *ps04= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps05) *ps05= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps06) *ps06= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps07) *ps07= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps08) *ps08= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps09) *ps09= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps10) *ps10= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps11) *ps11= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps12) *ps12= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps13) *ps13= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps14) *ps14= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps15) *ps15= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps16) *ps16= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps17) *ps17= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps18) *ps18= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps19) *ps19= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps20) *ps20= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps21) *ps21= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps22) *ps22= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps23) *ps23= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps24) *ps24= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps25) *ps25= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps26) *ps26= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps27) *ps27= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps28) *ps28= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps29) *ps29= sTmp;
      sTmp= s.Mid(nPos).SpanExcluding( "," ); nPos= 1+s.Find(',', nPos); if (ps30) *ps30= sTmp;
}

-=-=-=-=-=-=-=-=-=-=-==--=-=-=-=-=-=-=-=-=-=-=-=
Use it like this:
CString sSrc="Hello,17, 2002-05-19, foo";
CString sGreeting, sAge, sDate, sWhoKnows;
BreakOutStrings( sSrc, &sGreeting, &sAge, &sDate, &sWhoKnows );

--==--=-=-==-=-=-
You will need to change it so that it does not use commas.  I'm too weak with vile self-loathing to even look at it.

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
Dear adatd

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to split between

     "mblat and DanRollins"

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 

Author Comment

by:adatd
Comment Utility
pls split it up....
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **

Dan, there will be a separate question with points for your help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

16 Experts available now in Live!

Get 1:1 Help Now