how to truncate a string based on buffer size.
-
Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }
-
Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }
Nandu_77b wrote:
// how to truncate the string based on given buffersize
How about the
substr()
method?"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Nandu_77b wrote:
// how to truncate the string based on given buffersize
How about the
substr()
method?"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Nandu_77b wrote: // how to truncate the string based on given buffersize How about the substr() method? substr() this fun will not take buffer size, it takes length of characters. I need to truncate based on buffer size. Please see my code which will give you clear idea. -Nandu
-
Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }
What about
strncpy
[^]? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What about
strncpy
[^]? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Are you aware that, for
ANSI
strings, the char length and the buffer size (bytes) are basically the same? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Nandu_77b wrote: // how to truncate the string based on given buffersize How about the substr() method? substr() this fun will not take buffer size, it takes length of characters. I need to truncate based on buffer size. Please see my code which will give you clear idea. -Nandu
If the size of your buffer is 50, then you would extract characters 0-49.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Are you aware that, for
ANSI
strings, the char length and the buffer size (bytes) are basically the same? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }
lstrcpynA(errmsg, bufsize);
If using TCHAR, do:lstrcpyn(errmsg, bufsize / sizeof(TCHAR));
(lstrcpyn is a tiny bit more efficient than strncpy since it doesn't fill the rest of the buffer with zeros after completing the copy. With large buffers, this is generally a waste of cycles.)Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke