|
Hi there,
I've got data from the serial port stored in a CString variable,
e.g. 0123;0255;0192 with fixed size and the values are separeted with ';'
Now I need to splitt the values 0123 0255 and so on in other variables in order to make calculations.
My question: Is there an easy way to do this with the advadages of CString or do I need the way I would do in ordinary c-code: for (i=0 ...)
variable[i] = string [i] ... and so on?
|
|
|
|
|
If you are using Visual Studio 2002 or higher, then check out the CString::Tokenize method - you can use this to split the string into sub-strings, and then use _ttoi to convert the string to an int . For example:
CString str = _T("0123;0255;0192");
int nStartPos = 0;
CString strToken = str.Tokenize(_T(";"), nStartPos);
while (!strToken.IsEmpty())
{
int nVal = _ttoi(strToken);
TRACE(_T("Token value: %d\n"), nVal);
strToken = str.Tokenize(_T(";"), nStartPos);
}
HTH.
|
|
|
|
|
Dear Robert,
thanks for good idea. Unfortunately, I'm working under VC++ 6.0 and there is no Tokensize availible.
|
|
|
|
|
OK, well you could use the C strtok function instead, but it's messy (and you'd need to make a copy of the string first), e.g.:
CString str = "0123;0456;0789";
TCHAR* psz = new TCHAR [str.GetLength() + 1];
_tcscpy(psz, str);
TCHAR* pszTok = _tcstok(psz, _T(";"));
while (pszTok != NULL)
{
int nVal = _ttoi(pszTok);
...
pszTok = _tcstok(NULL, _T(";"));
}
delete [] psz;
|
|
|
|
|
I don't think strtok is thread-safe, so be warned!
|
|
|
|
|
Robert Edward Caldecott wrote: I don't think strtok is thread-safe, so be warned!
Is CString itself thread safe ?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
I'm being thick. You can obtain direct access to the CString buffer using CString::GetBuffer if that helps.
|
|
|
|
|
|
Thanks Mike,
this is really working!;)
|
|
|
|
|
I write a MFC app, like Graphic-Suite,only to view Image Infos,you can download my app from here :http://h1.ripway.com/jupiter/JHv2.zip[^]
Problem is when i runned my app,Image Infos Dlg did not appear.
Anyone can help me solute this app....
thanks so much.
|
|
|
|
|
I used MFC appwizard and made a single document interface.
I didn't add anyother codes than this code below in OnDraw function.
I wanted to make a pyramid looking bitmap in size 256*256.
But I had to make the pointer size into 256*256*16 to make it show the size of 256*256.
if i don't, a rectangle with four small boxes of pyramid and a large black box attached to it
appers. why is it?
void CWoosooView::OnDraw(CDC* pDC)
{
CWoosooDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int size = 256;// the size i want to make
int realsize = size*4;// this is the size i had to pump up.
// i made a pointer and added some values to make a rectangle look like a pyramid.
p = (unsigned char*)malloc(realsize*realsize);// i had to use *4 size all the way.
// below is a code for only one pyramid, but four of them appers. why is it?
for(int y = 0; y<realsize; y++){
="" for(int="" x="0;" x<realsize;x++){
="" if="" (((x="">=0) && (x<(realsize/2)))&&((y>=0) && (y<(realsize/2))))
*(p+realsize*y+x) = (int)(x+y)/4;
else if (((x<realsize) &&="" (x="">=(realsize/2)))&&((y>=0) && (y<(realsize/2))))
*(p+realsize*y+x) = (int)(realsize+(y-x))/4;
else if (((x>=0) && (x<(realsize/2)))&&((y<realsize) &&="" (y="">=(realsize/2))))
*(p+realsize*y+x) = (int)(realsize+(x-y))/4;
else if (((x<realsize) &&="" (x="">=(realsize/2)))&&((y<realsize) &&="" (y="">=(realsize/2))))
*(p+realsize*y+x) = (int)((2*realsize-x-y)/4);
}
}
CBitmap bitmap,bitmap2;
CDC memDC;
memDC.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC,realsize,realsize);//realsize = *4 of the wanted size.
bitmap.SetBitmapBits(realsize*realsize,p); // here as well. i used 256*4.
free(p);
memDC.SelectObject(bitmap);
bitmap.DeleteObject();
pDC->BitBlt(0,0,size,size,&memDC,0,0,SRCCOPY);
// so i eventually only displayed the first pyramid of 4 by setting the size of BitBlt to 256*256.
bitmap2.GetBitmapBits(SRCCOPY,bitmap);
memDC.SelectObject(bitmap2);
bitmap2.DeleteObject();
pDC->StretchBlt(256,0,size/2,size/2,&memDC,0,0,size,size,SRCCOPY);
pDC->StretchBlt(384,0,size*2,size*2,&memDC,0,0,size,size,SRCCOPY);
}
woosoo
-- modified at 11:31 Saturday 18th March, 2006
|
|
|
|
|
|
thanks~ but.. what is alpha??
woosoo
-- modified at 9:38 Sunday 19th March, 2006
|
|
|
|
|
Hi,
Wen i open a particular project and try to compile it my msdev crashes and wen i debug it i get the following info:-
Unhandled exception in MSDEV.EXE(MSPDB60.dll)0x0000005 Access Violation
This hapens only for this paricular project .
has anyone of you faced similar problems .
Thanx in Advance
Regards,
FarPointer
|
|
|
|
|
I had a similar problem earlier today. MSDEV crashed when I tried to run my code in the debugger. I tracked the problem down to the initialisation of a basic_string class. For some reason when inisializing the string object the string was too long, even though it was well under the 4294967294 chars specified by MS.
Your project may have a similar problem in that something is being initialised beyond it's memory allocation.
|
|
|
|
|
Well Thanx for showing the interest :-
Actually wat i feel is MSPDB60.dll is for Microsoft-Program-DataBase , something like the linker data overriding the compiler data , etc which gets the msdev enter an infinite loop and leading to stack overflow and crash.
While in your case it was during runing in my case its during compilation this worked fine in another pc .
All i know is iam stuck and need to recreate the whole project(dsz,etc..)
Regards,
FarPointer
|
|
|
|
|
when I run my application , it occurs follow error :
Debug assertion failed
Program E:\Visual C\booth\Debug\booth.exe
File:strex.cpp
line:672
For information on how your program can cause an assertion failure ,
see the Visual C++ documentation on asserts
(Press retry to debug the application )
Abort Retry Ignore
How can i debug it ?
thanks
|
|
|
|
|
Do you test with Debug(key F5)?
I suggestion that you insert breakpoint(key F9) in the any function and use
key F5,Now you can find error(I hope)
maybe it is some helpful to you
|
|
|
|
|
|
how to update the font dialog box in the second tme you open it.
|
|
|
|
|
The first parameter to the CFontDialog c'tor is either a pointer to a LOGFONT structure or a reference to a CHOOSEFONT structure. If use the same structure every time (ie do not create a new one every time) then the CFontDialog will have the same settings as the last time.
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good - Use it!
|
|
|
|
|
I'm trying to obtain the recycle bin folder and have tried the following two ways. Both of them dont work, any idea what is wrong in each of them? I ran them on Windows XP.
<br />
void recycle_bin1()<br />
{<br />
char szPath[4096] = { '\0' };<br />
HRESULT hr = SHGetFolderPath(NULL, <br />
CSIDL_BITBUCKET,<br />
NULL,<br />
SHGFP_TYPE_CURRENT,<br />
szPath);<br />
<br />
const char *str = "SUCCEEDED";<br />
if (hr == S_FALSE) {<br />
str = "S_FALSE";<br />
} <br />
if (hr == E_FAIL) {<br />
str = "E_FAIL";<br />
}<br />
if (hr == E_INVALIDARG) {<br />
str = "E_INVALIDARG";<br />
}<br />
<br />
MessageBox(NULL, szPath, str, NULL);<br />
<br />
}<br />
void recycle_bin2()<br />
{<br />
char szPath[4096] = { '\0' };<br />
<br />
BOOL bRet = SHGetSpecialFolderPath(NULL, <br />
szPath,<br />
CSIDL_BITBUCKET, <br />
FALSE);<br />
<br />
const char *str = "SUCCEEDED";<br />
<br />
if (bRet == FALSE) {<br />
str = "FAILED";<br />
}<br />
<br />
MessageBox(NULL, szPath, str, NULL);<br />
<br />
}<br />
<br />
thanks!
|
|
|
|
|
|
|
The first one returns E_INVALIDARG and the second one returns FALSE.
MSDN doesnt say these APIs set the last error code, so I havent printed them.
I'm away from the computer that has this code, I will get back with the error codes in a couple of hours.
thanks!
|
|
|
|