Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using SpanExcluding

Posted on 2002-05-24
16
Medium Priority
?
1,328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +4
16 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7032725
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
ID: 7032804
'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
ID: 7032808
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:jkr
ID: 7032832
So, try my suggestion :o)
0
 

Author Comment

by:adatd
ID: 7032842
I need to do it using SpanExcluding in MFC
0
 

Author Comment

by:adatd
ID: 7032852
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
ID: 7032855
Why would you restrict yourself on using 'SpanExcluding()'?
0
 

Author Comment

by:adatd
ID: 7032875
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
 
LVL 30

Expert Comment

by:Axter
ID: 7032899
Is this homework?
0
 

Author Comment

by:adatd
ID: 7032908
No I am trying to learn MFC
0
 
LVL 30

Expert Comment

by:Axter
ID: 7032930
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 200 total points
ID: 7032934
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
ID: 7033531
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
ID: 7178821
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
ID: 7181101
pls split it up....
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7199825
Force accepted

** Mindphaser - Community Support Moderator **

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

715 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