Take the 2-minute tour ×

I'm looking for something where:

string pearString ("pear");
string bananaString ("banana");

cout << ???? 5 ??? pearString ??????? << "[end]" << endl;
cout << ???? 5 ??? bananaString ??????? << "[end]" << endl;

Will (for some sequence of code characters ???) output:


But I'd like this to work without having to do a substring operation that would copy the string. Unless I'm missing something, there is no formatting specifier for this. (The setw specifier pads shorter strings, but allows longer ones to overflow.)

A "tricky" thing I thought of would be to make a lightweight wrapper class with its own stream output operator. It would take the (non-const) string reference and a limit in its constructure. Then the output operator would test the length, do a standard output if it was less than or equal to the limit. If it was greater, it would temporarily poke a null character in at the length position just long enough to do the output...and then put the original character back.

(UPDATE: actually this idea wouldn't have worked as << ignores embedded nulls.)

Anyway the idea for the way it would look would be:

cout << strlimiter(5, pearString) << "[end]" << endl;
cout << strlimiter(5, bananaString) << "[end]" << endl;

Any cleaner solutions that would work on const strings and wouldn't be fiddling the data out from under you? Something that uses other hooks or tricks of extending iostream? Is this covered by a library somewhere?

UPDATE: I'd overlooked the write method of iostream in lieu of <<, so here's some sample code:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class strlimiter
    friend ostream& operator<< (ostream& os, strlimiter sl);

    const string& data;
    size_t limit;

    strlimiter(const string& data, size_t limit) : data(data), limit(limit) {}
    ~strlimiter() {}

ostream& operator<< (ostream& os, strlimiter sl) {
   return os.write(sl.data.c_str(), min(sl.data.length(), sl.limit));

int main() {
    string pearString ("pear");
    string bananaString ("banana");

    cout << strlimiter(pearString, 5) << "[end]" << endl;
    cout << strlimiter(bananaString, 5) << "[end]" << endl;

    return 0;

Any more trickery that could make it even less of a cost at runtime?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Why not just use the write member function of ostream:


Perhaps surprisingly, you can even put this at the start of an insertion chain. Taking your example as a basis, you can use the following somewhat convoluted chain:

cout.write(bananaString.c_str(),5) << "[end]" << endl;
share|improve this answer
Using write is definitely what I should have thought of, I was stuck on << for some reason. However, the write method ignores embedded nulls and actually puts the number of bytes you say into the stream. So it can't be used to directly solve this problem, without some sort of wrapping to test the length... –  HostileFork Oct 16 '11 at 2:10
@HostileFork: operator<< for std::string also ignores embedded nulls. If you want a different behavior, then testing the length before hand is the way to go. I doubt you can get more efficient than that. –  Benjamin Lindley Oct 16 '11 at 3:05
Right, just saying I can't merely use your second line cout.write(bananaString.c_str(),limit) in the general case of trying to print up to a limit... –  HostileFork Oct 16 '11 at 3:15

For your wrapper class, in the overloaded operator<<, instead of using << on the string, use the ostream::write function.

the_stream.write(the_string.c_str(), the_size);
share|improve this answer
D'oh. Yup. Thanks. Added some code, feel free to throw in any other observations or ideas for improvement... –  HostileFork Oct 16 '11 at 2:13

Your Answer


