Search code examples
c++streamtruncateiomanipsetw

Truncating with setw


Is there a way that I can force setw to truncate?

Say that I want to get the output:

blah blah blee le
bu blah blah blee

Is there a way to make this work:

string foo{"bu blah blah blee le"};

cout << setw(foo.size() - 3) << foo.data() + 3 << setw(foo.size() - 3) << foo << endl;

Solution

  • No, not really.

    You can switch to unformatted output for this example, though:

    assert(foo.size() > 3);
    cout.write(&foo[3], foo.size() - 3);
    cout.write(&foo[0], foo.size() - 3);