CString instead of LPCSTR

A simple routine to allow CString formatted strings to be used wherever a regular LPCSTR would be used, without needing to define a CString (ie, dynamically created and formatted LPCSTR strings)

.

Environment: VC6 SP4

Well, I've seen other people asking for a class like this, and I sure wanted one, so I finally wrote one.

Suppose you want to call a routine with a dynamically constructed LPCSTR. You would call it like this:

SampleRoutine( (LPCSTR) csFMT(
                  "Sample dynamic string count(%d) name(%s)",
                  nAnInteger,
                  szAZeroTerminatedString));

The Microsoft CString class provides a Format member routine which provides sprintf-type formatting, without the need to allocate a buffer. The class CString automatically calculates the size of the buffer required, and allocates it. A CString variable, however, must first be declared. This routine allows the CString Format member routine to be used, without needing to declare a CString.

Here is the sample code without using the "csFmt" routine:

  CString csTemp;
  CsTemp.Format("Sample dynamic string count(%d) name(%s)",
                nAnInteger, szAZeroTerminatedString);
  SampleRoutine((LPCSTR) csTemp);

This method doesn't require any subclassing, and is only a short < 10 line routine.

CString csFMT(LPCSTR lpszFormat, ...)
{
    CString csStr;

    va_list argList;
    va_start(argList, lpszFormat);
    csStr.FormatV(lpszFormat, argList);
    return csStr;
}

Downloads

You can cut & paste code from above.



Comments

  • If only Format whould ??

    Posted by Legacy on 06/04/2002 12:00am

    Originally posted by: Boaz Harrosh

    If CString::Format was defined as
    CString& AFX_CDECL Format(LPCTSTR lpszFormat, ...);
    and not:
    void AFX_CDECL Format(LPCTSTR lpszFormat, ...);

    than we could just do:
    cout << (LPCSTR)CString().Format("%s" ,"yes it works this way") ;


    A constructor is callable in C++. It is just like a type cast.

    Reply
  • Another way

    Posted by Legacy on 04/17/2002 12:00am

    Originally posted by: Mathieu Fr.

    You can also do such a thing. No cstring. no alloc.
    
    Of course there are restrictions due to the static buffer, but SPEED matters sometimes (and mem alloc too) !

    LPCSTR poil(LPCSTR lpszFormat, ...)
    {
    const char buff[1000];
    va_list argList;
    va_start(argList, lpszFormat);
    vsprintf( buff, lpszFormat, argList );
    return buff;
    }

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • The exponential growth of data, along with virtualization, is bringing a disruptive level of complexity to your IT infrastructure. Having multiple point solutions for data protection is not the answer, as it adds to the chaos and impedes on your ability to deliver consistent SLAs. Read this white paper to learn how a more holistic view of the infrastructure can help you to unify the data protection schemas by properly evaluating your business needs in order to gain a thorough understanding of the applications …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds