string class manipulation

S

ssylee

This is what I want to do with the line:

fullmsg += ": " + GetLastError();

fullmsg is a string object. I'm trying to concatenated GetLastError(),
which is type int, to the string. Would an integer be printed or a
character with the code that is equal to GetLastError() that would be
printed?

Thanks in advance.
 
A

Alp Mestan

You probably want to do

    std::eek:stringstream os;
    os << ": " << GetLastError();
    fullmsg += os.str();

It's certainly what he would like to do, yep.

To the author (ssylee) > you can't concatenate that simply strings
with integers. You first have to create an ostringstream, then put the
integers, chars and anything else you want in it, using the <<
operator. Then os.str() returns the finally built string and clears
the ostringstream's buffer.
 
S

ssylee

It's certainly what he would like to do, yep.

To the author (ssylee) > you can't concatenate that simply strings
with integers. You first have to create an ostringstream, then put the
integers, chars and anything else you want in it, using the <<
operator. Then os.str() returns the finally built string and clears
the ostringstream's buffer.

Thank you for your replies. I guess I was trying to hard to make up a
shortcut like that. But I've managed to find another method to achieve
what I initially wanted to do when I've made the first post.
 
J

James Kanze

Thank you for your replies. I guess I was trying to hard to
make up a shortcut like that. But I've managed to find another
method to achieve what I initially wanted to do when I've made
the first post.

Typically, you'll have a function somewhere along the lines of:

std::string
getSystemErrorMessage(
SystemErrorCodeType errorCode )
{
LPVOID msgBuf ;
FormatMessage(
( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS ),
NULL,
errorCode,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &msgBuf,
0,
NULL ) ;
std::string result( static_cast< char const*
( msgBuf ) ) ;
LocalFree( msgBuf ) ;
return result ;
}

(with a corresponding version for Unix, of course). And since
it returns a string, you can easily concatenate its results.
 
R

Roland Pibinger

Typically, you'll have a function somewhere along the lines of:
std::string getSystemErrorMessage(SystemErrorCodeType errorCode)
{
LPVOID msgBuf ;
FormatMessage(
( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS ),
NULL,
errorCode,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &msgBuf,
0,
NULL ) ;
std::string result( static_cast< char const*(msgBuf));
LocalFree( msgBuf ) ;
return result ;
}

(with a corresponding version for Unix, of course). And since
it returns a string, you can easily concatenate its results.

By converting the code from C to C++ it lost exception safety. (Ok,
that's just nitpicking since out-of-memory isn't really an exception
but a fatal error). The return value of FormatMessage should be
checked, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top