|
Go the end of the string, start backing up character-by-character while whitespace (or whatever you want to trim) is found. When you find the first non-matching character, place a NUL into that location to terminate the string at that point.
You do not have to do something crazy like copying the string or moving it into and out of a string object.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
What about beginning of string?
Any sufficiently gross incompetence is nearly indistinguishable from malice.
|
|
|
|
|
Just reverse the algorithm, being sure not to walk past the end of the string.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
tanarnelinistit wrote: how can i remove any new line or carriage return from the...begining of it?
Something like the following comes to mind:
void TrimLeft( char *str, const char ch )
{
char *pStr = str;
while (ch == *pStr)
pStr++;
strcpy(str, pStr);
} Fine tune it to fit your needs.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Method using CString:
char buffer[17] = {0};
InitializeBuffer(buffer);
CString strBuffer(buffer);
strBuffer.TrimLeft();
strBuffer.TrimRight();
memset(buffer, 0, 17);
strncpy(buffer, strBuffer, 16);
Method using std::string:
char buffer[17] = {0};
InitializeBuffer(buffer);
std::string sBuffer(buffer);
int start_pos = sBuffer.find_first_not_of("\t\n\r ");
int end_pos = sBuffer.find_last_not_of("\t\n\r ");
if (start_pos == npos)
{
start_pos = 0;
}
if (end_pos = npos)
{
end_pos = strlen(buffer) - 1;
}
std::string sTemp = sBuffer.substr(start_pos, end_pos - start_pos + 1);
memset(buffer, 0, 17);
strncpy(buffer, sTemp.c_str(), 16);
Method using char:
int FindFirstNotOf(const char* chars, size_t chars_size, const char* str, size_t str_size)
{
int ret = -1;
for (int i = 0; i < str_size; ++i)
{
bool bFound = false;
for (int j = 0; j < chars_size; ++j)
{
if (str[i] == chars[j])
{
bFound = true;
break;
}
}
if (bFound == false)
{
ret = i;
break;
}
}
return ret;
}
int FindLastNotOf(const char* chars, size_t chars_size, const char* str, size_t str_size)
{
int ret = -1;
for (int i = str_size - 1; i >= 0; --i)
{
bool bFound = false;
for (int j = 0; j < chars_size; ++j)
{
if (str[i] == chars[j])
{
bFound = true;
break;
}
}
if (bFound == false)
{
ret = i;
break;
}
}
return ret;
}
char buffer[17] = {0};
InitializeBuffer(buffer);
char chars[5] = "\t\r\n ";
int start_pos = FindFirstNotOf(chars, 5, buffer, 17);
int end_pos = FindLastNotOf(chars, 5, buffer, 17);
if (start_pos == -1)
{
start_pos = 0;
}
if (end_pos == -1)
{
end_pos = strlen(buffer) - 1;
}
char newBuffer[17] = {0};
strncpy(newBuffer, &buffer[start_pos], end_pos - start_pos);
memset(buffer, 0, 17);
strncpy(buffer, newBuffer, 16);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
OK - perhaps a simple example of trimming a string in-place, because you did not mention the word "copy" in your post (this code comes slightly modified from my TStaticString class):
void TrimRight( LPTSTR cpString,
LPCTSTR cpTrimWhat = _T( " " ) )
{
size_t stStrLen = ::_tcslen( cpString );
if( !stStrLen )
{
return;
}
LPTSTR cpCursor = ( cpString + ( stStrLen - 1 ) );
size_t stCount = 0;
while( ( stCount < stStrLen) &&
( ::_tcschr( cpTrimWhat, *cpCursor ) ) )
{
++stCount;
--cpCursor;
}
if( stCount )
{
++cpCursor;
*cpCursor = _T( '\0' );
}
return;
} See how that works for you. Specify the characters to Example of use:
TCHAR caTest[ 32 ];
strcpy( caTest, _T( "abc\t " ) );
TrimRight( caTest );
strcpy( caTest, _T( "abc\t " ) );
TrimRight( caTest, "\t " );
The first call will trim up to the tab character, the second will also trim the tab character.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
plz send me da code how vc++ can connect to access database either DAO/ODBC
janan
|
|
|
|
|
See here[^]
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
See the semi-related article here.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hello
i want to display the content of edit control in one form to another edit box content in new from
janan
|
|
|
|
|
Say if you have an edit box in the first dialog then in the second dialog you can place a pointer to the parent class like
CMain* pParent=(CMain*)GetParent();
pParent->m_edit.GetWindowText() etc..//... to get the value from the other edit box
// Put it into the edit box in the second dialog using
m_editOnSecondDlg.SetWindowText()...
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Whats application type?
if its dialog you can use
<br />
CMain* m_Main=(CMain*)GetParent();<br />
CString str;<br />
m_Main->m_Edit1.GetWindowText(str);<br />
in another dialog use
<br />
m_Edit2.SetWindowText(str);
|
|
|
|
|
What is the relationship between the two dialogs (i.e., parent/child, siblings)?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hello everyone,
I always see the two words -- asynchronized I/O and I/O multiplexing. Are they the same thing -- like select/poll?
I noticed that in some situations, people say they are different and asynchronized I/O is better than I/O multiplexing.
Could anyone help to clarify the differences between the two words?
thanks in advance,
George
|
|
|
|
|
Multiplexed I/O queries multiple I/O channels, and returns if an communication occurs on one channel. The typical use is a Server process that has to respond to multiple connections. The call is blocking, so you can't do anything else (in this thread).
Asynchronous I/O is more versatile, but can be used in the same scenario.
You can request the I/O operation to start, then do your own things. At any time, you can query if the I/O operation has completed It's also possible to set an event or call a callback function when the operation completes.
|
|
|
|
|
Thank you very much peterchen!
Your reply makes senses.
peterchen wrote: Multiplexed I/O queries multiple I/O channels, and returns if an communication occurs on one channel. The typical use is a Server process that has to respond to multiple connections. The call is blocking, so you can't do anything else (in this thread).
I can not imagine why in the situation (server process handles multiple client connections) the call is blocking (other than asynchronous) -- I think the server can process other things even if no new data is received from client connection, which is an asynchronized mechanism other than a blocked mechanism. Could you show me more details please?
regards,
George
-- modified at 8:06 Tuesday 25th July, 2006
|
|
|
|
|
Hello comunity,
how to set an focus to button and select him when user enter a CEdit field, so when the user press enter on keyboard the additional funktion begins, example show an MessageBox with this text from
this CEdit field, and not like now to close my dialog???
Thanx in advance!
break;
|
|
|
|
|
setting the focus is one thing, and select him so that when user presses enter key in an edit box is an onther one.
to set focus :
(GetDlgItem(IDC_MY_BUTTON))->SetFocus();
to set as the default button :
this->SetDefID(IDC_MY_BUTTON);
where IDC_MY_BUTTON is the button you want to act on, and this, is the dialog box which the button is layed on...
|
|
|
|
|
Hi,
thanx a lot you solve my problem!
That what im looking for is
this->SetDefID(IDC_MY_BUTTON);
regards
break;
|
|
|
|
|
break; wrote
CEdit field, so when the user press enter on
Didn't get your question clearly. But I feel that you want to capture the enter key on the dialog so that when enter key is pressed you don't close the dialog.
Right?
If yes you can use PreTranslateMessage() and detect when Enter is pressed
Something like
if(pMsg->message == WM_KEYDOWN && (VK_ESCAPE == pMsg->wParam || VK_RETURN == pMsg->wParam))
pMsg->wParam=VK_SEPARATOR;
would do
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Hello,
thanx for answer, i solve the problem like this:
SetDefID(IDC_ADD);
regards
break;
|
|
|
|
|
Hi all,
How do I detect that the mouse was double clicked on a text in any window. eg.(edit box, static text box,hyperlink,MS-Outlook etc).I need the text to be read and stored in the database(only single word on which the mouse is clicked).
Some link to articles that could help me build on this.
My initial ideas are that I can develop a system wide hook for mouse and check for its double click event.When ever it does a double click I would read the text(and I don't know this right now how this would be achieved-Reading the text).
Thanks in advancee
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
|
See if this helps.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I'm trying to make it clear this time. I have a function called Read that reads from the COM. Then i'm using it in the following piece of code to print the result to the screen and to write it in one file. The output to the screen it's ok (FFFF00000000028C) but the one to the file is : "ൃFFF " or smth like that. Note: all the output from my device that is connected to COM is preceded by a Line feed <lf-0x0a> and terminated by a Carriage return <cr-0x0d>
I use the following code:
DWORD dwBytesRead = 0;<br />
char szBuffer[101];<br />
FILE *out;<br />
serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);<br />
if (dwBytesRead > 0)<br />
{<br />
szBuffer[dwBytesRead] = '\0';<br />
out = fopen( "output.txt", "w" );<br />
fprintf( out, "%s", szBuffer ); <br />
printf("%s", szBuffer);<br />
fclose(out);<br />
<br />
if (strchr(szBuffer,EOF_Char))<br />
fContinue = false;<br />
<br />
}
|
|
|
|