【问题标题】:Is there a simple way to get the number of characters printed in C++?有没有一种简单的方法来获取用 C++ 打印的字符数?
【发布时间】:2017-05-13 14:55:37
【问题描述】:

printf(...) 返回输出到控制台的字符数,我觉得这对设计某些程序很有帮助。所以,我想知道 C++ 中是否有类似的功能,因为 cout

【问题讨论】:

  • 我认为你最好的办法是输出到内存缓冲区(使用ostringstream),计算它,然后将该缓冲区输出到控制台
  • 我总是发现使用老式 C 函数更容易进行复杂的格式化。您是否有任何具体原因要避免使用 printf?
  • 糟糕,抱歉。我什至不知道 printf 在 C++ 中工作,认为它必须是 cout
  • 所有在 c 中工作的东西也可以在 c++ 中工作
  • @Angelica 不是真的。例如 C 复数、可变长度数组... C 和 C++ 是不同的语言,而不是彼此的子集/超集

标签: c++ printf console-application iostream cout


【解决方案1】:

您可以将自己的streambuf 关联到cout 以计算字符数。

这是封装所有内容的类:

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;
};

你可以这样使用它:

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

当对象实例存在时,它会计算 cout 输出的所有字符。

请记住,这只会计算通过 cout 输出的字符,而不是使用 printf 打印的字符。

【讨论】:

    【解决方案2】:

    您可以创建一个过滤流缓冲区来报告写入的字符数。例如:

    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; }
    };
    

    您只需将此流缓冲区用作过滤器:

    int main() {
        countbuf sbuf;
        std::streambuf* orig = std::cout.rdbuf(&sbuf);
        std::cout << "hello: ";
        std::cout << sbuf.count() << "\n";
        std::cout.rdbuf(orig);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 2023-03-09
      • 2011-01-07
      相关资源
      最近更新 更多