By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Parametrized stream manipulator

P: n/a
Hi,

Does anybody know how to implement parametrized stream operator (such as
setw, setfill)? I need to put into the stream variable of type char* without
terminating NULL. It would be great if I had the manipulator which allows to
determine maximum number of characters ( let's assume maxw( char* str, int
maxlen ) ).

example:
cout << maxw("Crocodile", 5 );
Croco

thanx in advance
ralfer

ps. prefer templates to macros... :)
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"rwawryk" <ra****@gazeta.pl> wrote...
Does anybody know how to implement parametrized stream operator (such as
setw, setfill)? I need to put into the stream variable of type char* without terminating NULL. It would be great if I had the manipulator which allows to determine maximum number of characters ( let's assume maxw( char* str, int
maxlen ) ).

example:
cout << maxw("Crocodile", 5 );
Croco

thanx in advance
ralfer

ps. prefer templates to macros... :)


(a) If you need to put several chars into the stream, just put
them into the stream. No "terminating NULL" is copied into the
stream. cout << string("Crocodile").substr(5); should do it.

(b) Usually parameterised manipulators are written either as
functions that return special objects or just as classes [that
you construct right there], which, when output to a stream, do
something to the stream:

struct maxw {
maxw(const char*, int); // constructor
ostream& print(ostream&) const; // output
// some data
};

ostream& operator <<(ostream& os, const maxw& m) {
return m.print(os);
}

maxw::maxw(const char* s, int n) : ....

ostream& maxw::print(ostream& os) const {
// do something to the stream to output what you need
// in a special way
}

HTH

Victor
Jul 19 '05 #2

P: n/a
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<vj************@corp.supernews.com>...
example:
cout << maxw("Crocodile", 5 );
Croco

thanx in advance
ralfer

ps. prefer templates to macros... :)


(a) If you need to put several chars into the stream, just put
them into the stream. No "terminating NULL" is copied into the
stream. cout << string("Crocodile").substr(5); should do it.


That's "odile". You meant string("Crocodile").substr(0, 5). :)

- Shane
Jul 19 '05 #3

P: n/a
"Shane Beasley" <sb******@cs.uic.edu> wrote...
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<vj************@corp.supernews.com>...
example:
cout << maxw("Crocodile", 5 );
Croco

thanx in advance
ralfer

ps. prefer templates to macros... :)


(a) If you need to put several chars into the stream, just put
them into the stream. No "terminating NULL" is copied into the
stream. cout << string("Crocodile").substr(5); should do it.


That's "odile". You meant string("Crocodile").substr(0, 5). :)


Right. Should have probably said

string("Crocodile", 5)

(without substr).

Victor
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.