aguess

hi

i have some problem in extracting some data from a database.in this database there in field that contain some information that i need to decompose .

the information is numbers between "/" that :

Code Block
102/232/232

i tryed to used vector and to use it like a table but eatch element of the vector take all the string and i can't decompose it (Vect[0]==102/232/232).

is there any way to solve that problem

N.B: i am using MFC



Re: Visual C++ Language decomposing data

Pintu Shukla

Just try this with CString or any char type string or pointer hope this will work for you.after that if you wann you can extract the string on the basis of the token

Thanx






Re: Visual C++ Language decomposing data

UnitUniverse

What the 'decompose' means To 3 strings To 3 numbers Or to exactly what






Re: Visual C++ Language decomposing data

aguess

i would like to decompose it to 03 strings,but u have to know this string is CStringW type. because i am receiveing that data from the database in that form(SCtringW).





Re: Visual C++ Language decomposing data

UnitUniverse

Copy the string to other 3 new string buffer based on the '/' character.






Re: Visual C++ Language decomposing data

aguess

can you give me an exemple how work the string buffer





Re: Visual C++ Language decomposing data

UnitUniverse

A simple exp is here, you can modify it for your own:

Assume your data is a normal unsigned integer bit-width (you can develop another without output-length-limit, however the new strings have to be created in heap, the allocation may fail. on the other side, a very long number string looks meanless to you).

Code Block

#define _STR_BUFF_LEN_FOR(_Type_o_Var_Name_) \

((sizeof(_Type_o_Var_Name_) * 240824L + 99999L) / 100000L + 1L)


#define BUFF_LENGTH_STRNUM _STR_BUFF_LEN_FOR(unsigned int)

BOOL SeperateNmGrp(LPCTSTR lpszNmGrp, LPTSTR alpszNm [3])

{

UINT cNmStr, cLng;

assert(lpszNmGrp);

assert(alpszNm);

for(cNmStr = 0; cNmStr < 3; cNmStr++, alpszNm++)

{

for(cLng = 0; cLng < BUFF_LENGTH_STRNUM; cLng++)

{

if(*(lpszNmGrp + cLng) >= _T('0') && *(lpszNmGrp + cLng) <= _T('9'))

*(alpszNm + cLng) = *(lpszNmGrp + cLng);

else

break;

}

*(alpszNm + cLng) = _T('\0'), lpszNmGrp += cLng;

if(*lpszNmGrp != _T('/'))

{

if(*lpszNmGrp != _T('\0') || cNmStr < 3 - 1)

break;

}

lpszNmGrp++;

}

return(cNmStr < 3 FALSE : TRUE);

}

// Remarks: the length of each *alpszNm must be a BUFF_LENGTH_STRNUM at least.

Usage:

TCHAR szNm1[BUFF_LENGTH_STRNUM];

TCHAR szNm2[BUFF_LENGTH_STRNUM];

TCHAR szNm3[BUFF_LENGTH_STRNUM];

LPTSTR alpszNm[3] = {szNm1, szNm2, szNm3};

SeperateNmGrp(_T("123/21/3154"), alpszNm);






Re: Visual C++ Language decomposing data

Viorel.

aguess wrote:

hi

i have some problem in extracting some data from a database.in this database there in field that contain some information that i need to decompose .

the information is numbers between "/" that :

Code Block
102/232/232

i tryed to used vector and to use it like a table but eatch element of the vector take all the string and i can't decompose it (Vect[0]==102/232/232).

is there any way to solve that problem

N.B: i am using MFC

If your source string always contains three valid numbers, and since you are using MFC, I think you can rely of things already done in MFC:

Code Block

CString example = _T("100/200/300");

int i1 = example.Find('/');

int i2 = example.Find('/', i1 + 1);

CString part1 = example.Left(i1);

CString part2 = example.Mid(i1 + 1, i2 - i1 - 1);

CString part3 = example.Mid(i2 + 1);

int number1 = _ttoi(part1);

int number2 = _ttoi(part2);

int number3 = _ttoi(part3);

Another solution is based on old-fashioned sscanf function, which in my opinion is very helpful here:

Code Block

CString example = _T("100/200/300");

int number1;

int number2;

int number3;

_stscanf_s(example, _T("%d/%d/%d"), &number1, &number2, &number3);

I hope this helps too.





Re: Visual C++ Language decomposing data

aguess

thank you,ur solution is very simple and good,but i my string don' t take always 03 numbers.




Re: Visual C++ Language decomposing data

Bite Qiu - MSFT

aguess wrote:

thank you,ur solution is very simple and good,but i my string don' t take always 03 numbers.

Why not just add some logic to existing code to make it support of any numbers, here is an example that made uppon Viorel's solution:

Code Block

CString example = _T("100/200/300/...more../..and more..."), tmp;
vector<CString> vct;
int i1=-1,i2;

// decomposing strings
while( (i2=example.Find('/', i1 + 1))!=-1 ){
tmp = example.Mid(i1+1, i2-i1-1);
vct.push_back(tmp);
i1=i2;
}

// get the last string
tmp = example.Mid(i1+1);
vct.push_back(tmp); // Now you get a vector that has decomposited strings