string class manipulation

  • Follow


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.
0
Reply stanigator (149) 8/14/2008 7:31:01 PM

ssylee wrote:
> 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?

No, this wouldn't work as you expected (it shouldn't even compile).

You probably want to do

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

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
0
Reply v.Abazarov (13255) 8/14/2008 8:21:56 PM


On 14 ao=FBt, 22:21, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> You probably want to do
>
> =A0 =A0 std::ostringstream os;
> =A0 =A0 os << ": " << GetLastError();
> =A0 =A0 fullmsg +=3D 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.
0
Reply alpmestan (6) 8/14/2008 9:13:22 PM

On Aug 14, 2:13=A0pm, Alp Mestan <alpmes...@gmail.com> wrote:
> On 14 ao=FBt, 22:21, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
> > You probably want to do
>
> > =A0 =A0 std::ostringstream os;
> > =A0 =A0 os << ": " << GetLastError();
> > =A0 =A0 fullmsg +=3D 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.

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.
0
Reply stanigator (149) 8/14/2008 10:08:50 PM

On Aug 15, 12:08 am, ssylee <staniga...@gmail.com> wrote:
> On Aug 14, 2:13 pm, Alp Mestan <alpmes...@gmail.com> wrote:

> > On 14 ao=FBt, 22:21, Victor Bazarov <v.Abaza...@comAcast.net> wrote:

> > > You probably want to do

> > >     std::ostringstream os;
> > >     os << ": " << GetLastError();
> > >     fullmsg +=3D 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.

> 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.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
0
Reply james.kanze (9594) 8/15/2008 8:32:52 AM

On Fri, 15 Aug 2008 01:32:52 -0700 (PDT), James Kanze rote:
>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. 

  

-- 
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
0
Reply rpbg123 (870) 8/16/2008 4:35:24 PM

5 Replies
34 Views

(page loaded in 0.071 seconds)

Similiar Articles:













7/12/2012 12:44:53 PM


Reply: