473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Parametrized stream manipulator

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
3 3325
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: ...
8
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello(...
2
by: Peteroid | last post by:
In the same spirit as setw( ) and setprecision( ), is there a stream manipulator that will fill in closing-zeroes on those fractions with a given setprecision( )? For example, if setprecision(3)...
6
by: jack | last post by:
I have a class which overloads the insertion operator '<<' for every type that ostream handles. I do this so that I can use my class a direct replacement for cout and cerr where the insertion...
5
by: ax | last post by:
my book says it is ok define ostream& operator<<(ostream& (*p)(ostream )) { return (*p)( *this); } ostream& endl(ostream& o) {o.put('\n'); o.flush();} but seems compiler says it is...
4
by: John Friedland | last post by:
'printf' has a '%a' conversion for floating-point output: For example, printing '123456' with "|%13.4a|" produces | 0x1.e240p+16| I've looked through Josuttis and the header files, but I...
2
Colloid Snake
by: Colloid Snake | last post by:
Hello, I was attempting to configure log4cpp to monitor some logs on my *nix box, and when I run 'make' I get this error message: # make g++ -DHAVE_CONFIG_H -I. -I. -I../include...
0
by: Halimaji Nijazi | last post by:
Hi everybody I've read a lot about parametrized services and now I want to test it. My problem is, how should I create a simple parametrized service? How starting this service whithin .NET? ...
6
by: Dan Smithers | last post by:
I want to write my own class derived from the ostream class. I have been getting errors with my templates: First, I get an error writing a nested template. If I leave the function definition...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.