4

Does anyone know how I can print and count the number of characters that I printed?

Say I have a number I am printing via printf or cout. How could I count the actual number of digits I have printed out?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Dave Powell
  • 671
  • 5
  • 14
  • 26

5 Answers5

9

According to the printf man page, printf returns the number of characters printed.

int count = printf("%d", 1000);

If an output error is encountered, a negative value is returned.

jschmier
  • 15,458
  • 6
  • 54
  • 72
  • 1
    does that work for a string in hex which need to be printed in decimal? for example 1b = 27 but printf count is 3? – Dave Powell Jan 14 '10 at 17:40
  • Perhaps I misunderstand your question, but the following returned a count of 2 for me. int count = printf("%d", 0x1b); – jschmier Jan 14 '10 at 17:55
  • hmm i am trying to output a byte array which has a hex string. so a string that happens to be 1b. does that make more sense now? – Dave Powell Jan 14 '10 at 18:05
  • You will first need to convert the ascii values in the byte array (hex string) into an actual number. – jschmier Jan 14 '10 at 18:08
7

printf returns the number of characters it printed

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
3

Here is another creative way of abusing the Locale to do the counting behind the senes.
This allows you to use the stream normally and the local will record the output for you.

This is not production ready code just a proof of concept to show how it could be done:

#include <locale>
#include <iostream>
#include <fstream>

class Counter: public std::codecvt<char,char,std::char_traits<char>::state_type>
{
    public:
    Counter(int& count)
        :m_count(&count)
    {}
    private:
    typedef std::codecvt<char,char,std::char_traits<char>::state_type> MyType;
    typedef MyType::state_type          state_type;
    typedef MyType::result              result;

    virtual bool   do_always_noconv() const throw()
    {
        return false;
    }
    virtual result do_out ( state_type& state,
                    const char* fr, const char* fe, const char*& fn,
                    char*       to, char*       te, char*&       tn ) const
    {
        // Count the number of characters that will be out on the stream
        (*m_count) += (fe - fr);

        // Use the default do_out (which just copies when internal and external are char)
        return MyType::do_out(state,fr,fe,fn,to,te,tn);
    }
    private:
        int*    m_count;

};




int main()
{
    // The variable to store the count in
    int             count   = 0;
    // A local object that contains the counting facet.
    // The counting facet will record output into count.
    std::locale     countingLocale(std::cout.getloc(), new Counter(count));

    std::ofstream   data;
    data.imbue(countingLocale);   // Impue the stream before opening.
    data.open("Plop");
    data << "Stop" << std::endl;

    std::cout << "Count: " << count  << "\n";

    // This should also work with std::cout
    std::cout.imbue(countingLocale)
    // Unfortunately there is a bug in the locale code for me that stops this working.

}
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

If you want to use an ostream, pcount() returns the number of characters put.

Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
0

Use a stringstream to convert beforehand, then poll that for the length.

Puppy
  • 144,682
  • 38
  • 256
  • 465