小编chu*_*uck的帖子

在 C++ 中用精确的字节数填充流

const size_t bytes我有一个流,我需要多次用文本 ( )填充精确大小( const char * filler = "Something") ,直到达到字节大小。

像这样:SomethingSomethingSomethingSomethingSome,如果没有更多可用字节,则可以将其截断,但它始终必须是精确的大小。例如字节为 30

    const size_t bytes = 30;
    const char * text = "Something";

    int x = bytes / strlen(text);

    for (int j = 0; j < x; j++)
    {
        for (int i = 0; i < strlen(text); i++)
        {
            stream << text[i];
        }
    }
Run Code Online (Sandbox Code Playgroud)

像上面这样,我只能用未截断的单词填充流,而不是:SomethingSomethingSomethingSom(这正好是 30)

我该如何修改这段代码?谢谢你!

c++ string stream fill

0
推荐指数
1
解决办法
351
查看次数

标签 统计

c++ ×1

fill ×1

stream ×1

string ×1