STL strings & printf-type formatting 
Author Message
 STL strings & printf-type formatting

Is there an STL algorithm or other such template that will allow printf-type
formatting of std::string & std::wstring objects.

What I would like to do is some like:-

        std::string strAge;
        strAge.format("%03d", nAgeVar);

or more likely

        std::string strAge;
        format(strAge, "%03d", nAgeVar);

Rod Paterson



Sun, 14 Apr 2002 03:00:00 GMT  
 STL strings & printf-type formatting
    There is nothing printf-like in C++, because printf is hopelessly
limited to C data types.   How would you use printf to display a Employee
object?

    The C++ solution:

    stringstream    s;

    s << nAgeVar;

    All the formatters that work with cout will work with a stringstream.


Quote:
> Is there an STL algorithm or other such template that will allow
printf-type
> formatting of std::string & std::wstring objects.

> What I would like to do is some like:-

>         std::string strAge;
>         strAge.format("%03d", nAgeVar);

> or more likely

>         std::string strAge;
>         format(strAge, "%03d", nAgeVar);

> Rod Paterson




Sun, 14 Apr 2002 03:00:00 GMT  
 STL strings & printf-type formatting
Rod,

Here is a rough example of a global function which takes a std::string, a
format string, and a variable number of arguments and formats the string
appropriately.  This should suit your needs.   You could always write
another one which takes a std::wstring

(On the other hand, if you're really looking for a std::basic_string
derivative which has this CString like functionality, you could also try
something I wrote a while back
http://home.earthlink.net/~jmoleary/stdstring.htm)

Joe O'

    void FormatString(std::string& str, LPCSTR* szFormat, ...)
    {
        va_list argList;
        va_start(argList, szFormat);
        FormatStringV(str, szFormat, argList);
        va_end(argList);
    }

   #define MAX_FMT_TRIES 5  // #of times we try if 2048 chars isn't big
enough
   #define FMT_BLOCK_SIZE 2048 // amount of extra memory we'll add above
2048 each try

   void FormatStringV(std::string& str, LPCSTR szFormat, va_list argList)
   {
       va_list argListSave = argList;

       // We'll use the  _vsntprintf function, assuming FMT_BLOCK_SIZE
characters.

       int nTriesLeft   = MAX_FMT_TRIES;
       int nUsed  = -1;
       char* pBuf;

      int nChars = 0;

       // Keep looping until we succeed or we have exhausted the number of
tries

       do
       {
           nChars += FMT_BLOCK_SIZE;  // number of TCHARS in the string
           pBuf  = reinterpret_cast<LPCSTR>(_alloca(nChars));

           // Now try the actual formatting.

           nUsed = _vsntprintf(pBuf, nChars, szFormat, argListSave);

           if ( nUsed >= 0 )
              str = pBuf;

          } while ( nUsed < 0 && --nTriesLeft > 0);

          va_end(argListSave);
      }
   }


Quote:
> Is there an STL algorithm or other such template that will allow
printf-type
> formatting of std::string & std::wstring objects.

> What I would like to do is some like:-

>         std::string strAge;
>         strAge.format("%03d", nAgeVar);

> or more likely

>         std::string strAge;
>         format(strAge, "%03d", nAgeVar);

> Rod Paterson




Sun, 14 Apr 2002 03:00:00 GMT  
 STL strings & printf-type formatting
printf formatting is a disease, and C++ contains the
cure (iostreams).

Not trying to be confrontational.  A solution which
you may find helpful has already been posted, but I
would submit for your consideration that you try
iostreams (following is ISO standard C++ and works
with VC6):

#include <iomanip>
#include <iostream>b
#include <sstream>

std::stringstream  ss;
int  AgeVar = 7;
ss << std::setw(3) << AgeVar;
std::cout << ss.str() << std::endl;


Quote:
> Is there an STL algorithm or other such template that will allow
printf-type
> formatting of std::string & std::wstring objects.

> What I would like to do is some like:-

>         std::string strAge;
>         strAge.format("%03d", nAgeVar);

> or more likely

>         std::string strAge;
>         format(strAge, "%03d", nAgeVar);

> Rod Paterson




Sun, 14 Apr 2002 03:00:00 GMT  
 STL strings & printf-type formatting
For what it's worth, I prefer iostreams as well.  It's just that in some
projects I've worked on, I've had to integrate with legacy code that used
old, CRT iostreams which tend to conflict with the new C++ ones.  Since he
asked specifically for printf-style printing, that's what I gave him.

But Rod you should defnitely use iostreams if you can..  They'll save you
from many silly errors.

Joe O'


Quote:
> printf formatting is a disease, and C++ contains the
> cure (iostreams).

> Not trying to be confrontational.  A solution which
> you may find helpful has already been posted, but I
> would submit for your consideration that you try
> iostreams (following is ISO standard C++ and works
> with VC6):

> #include <iomanip>
> #include <iostream>b
> #include <sstream>

> std::stringstream  ss;
> int  AgeVar = 7;
> ss << std::setw(3) << AgeVar;
> std::cout << ss.str() << std::endl;



> > Is there an STL algorithm or other such template that will allow
> printf-type
> > formatting of std::string & std::wstring objects.

> > What I would like to do is some like:-

> >         std::string strAge;
> >         strAge.format("%03d", nAgeVar);

> > or more likely

> >         std::string strAge;
> >         format(strAge, "%03d", nAgeVar);

> > Rod Paterson




Sun, 14 Apr 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Format string as a parameter to printf()?

2. printf format string: how bad style is this?

3. printf -*s format string

4. Examples of format strings for scanf/printf ?

5. printf and formatting numeric strings

6. placing output of printf in memory: formatting strings

7. printf and variable length string format (asterisk)

8. Printf format strings

9. printf() format-string parser wanted!

10. printf Format string

11. Source to format strings like printf()

12. STL STRING FORMAT HOW TO

 

 
Powered by phpBB® Forum Software