Is there a simple way to get the number of characters printed in C++?
Asked Answered
A

2

10

printf(...) returns the number of characters output to the console, which I find very helpful in designing certain programs. So, I was wondering if there is a similar feature in C++, since the cout<< is an operator without a return type (at least from what I understand of it).

Astrea answered 29/12, 2016 at 9:45 Comment(7)
I think your best bet is to output to a memory buffer (with ostringstream), count it, and then output that buffer to the consoleRavenous
I always found complex formatting to be easier with the old school C functions. Is there any specific reason you want to avoid printf?Yajairayajurveda
Oops, sorry. I was not even aware that printf works in C++, thought it has to be cout<<.Astrea
Every thing that works in c works in c++ tooCornered
@Cornered not really. For example C complex numbers, variable length arrays... C and C++ are different languages and not subset/superset of each otherErubescence
I think you can just use printfimmediately in your cases.Halfblood
Firstly I dont know who fooled him that printf doesnt work in cpp. Next, avoiding the syntax, all c static and dynamic library should work in c++ as long you have the declarations and libs.Azucenaazure
B
6

You can associate your own streambuf to cout to count the characters.

This is the class that wraps it all:

class CCountChars {
public:
    CCountChars(ostream &s1) : m_s1(s1), m_buf(s1.rdbuf()), m_s1OrigBuf(s1.rdbuf(&m_buf)) {}
    ~CCountChars() { m_s1.rdbuf(m_s1OrigBuf); m_s1 << endl << "output " << m_buf.GetCount() << " chars" << endl; }

private:
    CCountChars &operator =(CCountChars &rhs) = delete;

    class CCountCharsBuf : public streambuf {
    public:
        CCountCharsBuf(streambuf* sb1) : m_sb1(sb1) {}
        size_t GetCount() const { return m_count; }

    protected:
        virtual int_type overflow(int_type c) {
            if (streambuf::traits_type::eq_int_type(c, streambuf::traits_type::eof()))
                return c;
            else {
                ++m_count;
                return m_sb1->sputc((streambuf::char_type)c);
            }
        }
        virtual int sync() {
            return m_sb1->pubsync();
        }

        streambuf *m_sb1;
        size_t m_count = 0;
    };

    ostream &m_s1;
    CCountCharsBuf m_buf;
    streambuf * const m_s1OrigBuf;
};

And you use it like this:

{
    CCountChars c(cout);
    cout << "bla" << 3 << endl;
}

While the object instance exists it counts all characters output by cout.

Keep in mind that this will only count characters output via cout, not characters printed with printf.

Bicycle answered 29/12, 2016 at 10:18 Comment(0)
C
2

You could create a filtering stream buffer which reports the number of characters written. For example:

class countbuf
    : std::streambuf {
    std::streambuf* sbuf;
    std::streamsize size;
public:
    countbuf(std::streambuf* sbuf): sbuf(sbuf), size() {}
    int overflow(int c) {
        if (traits_type::eof() != c) {
            ++this->size;
        }
        return this->sbuf.sputc(c);
    }
    int sync() { return this->sbuf->pubsync(); }
    std::streamsize count() { this->size; }
};

You'd just use this stream buffer as a filter:

int main() {
    countbuf sbuf;
    std::streambuf* orig = std::cout.rdbuf(&sbuf);
    std::cout << "hello: ";
    std::cout << sbuf.count() << "\n";
    std::cout.rdbuf(orig);
}
Controller answered 29/12, 2016 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.