473,785 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3346
"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 "terminatin g NULL" is copied into the
stream. cout << string("Crocodi le").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(cons t char* s, int n) : ....

ostream& maxw::print(ost ream& 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.********@att Abi.com> wrote in message news:<vj******* *****@corp.supe rnews.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 "terminatin g NULL" is copied into the
stream. cout << string("Crocodi le").substr(5 ); should do it.


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

- Shane
Jul 19 '05 #3
"Shane Beasley" <sb******@cs.ui c.edu> wrote...
"Victor Bazarov" <v.********@att Abi.com> wrote in message news:<vj******* *****@corp.supe rnews.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 "terminatin g NULL" is copied into the
stream. cout << string("Crocodi le").substr(5 ); should do it.


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


Right. Should have probably said

string("Crocodi le", 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
2178
by: Riku Jarvinen | last post by:
Hi everyone, I asked another question regarding this same subject about a week ago. See thread: http://groups.google.fi/group/comp.lang.c++/browse_frm/thread/cc57b579d6832732/530bae667a479add#530bae667a479add So, the basic idea is to have a compact logger class which can be used like the std::cout stream and add extra functionality to it (line numbering, some output counters etc.).
8
1799
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello( ostream & os ) { return os << "regular: hello"; }
2
1431
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) is used, I want '1' to be displayed as "1.000", '1.2' to be displayed as "1.200", '1.23' to be displayed as "1.230", '1.234' to be displayed as "1.234", and '1.2345' to be displayed as "1.234" (or the rounded version "1.235" is ok too). Thanx...
6
3596
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 syntax is used. The reason for this is that I have a log file class that needs to know how many lines have been written. When the line exceeds a certain number, I need to close the log file, and open a new one. I would like to be able to track...
5
1576
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 not correct because "<<" needs 2 arguments, so i wrote
4
2155
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 can't find any flags or manipulators that could handle this. Is this possible with stream I/O?
2
4489
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 -I../include -g -02 -Wall -Wno-unused -pedantic -MT FileAppender.lo -MD -MP -MF .deps/FileAppender.Tpo -c FileAppender.cpp -fPIC -DPIC -o .libs/FileAppender.o ../include/log4cpp/Manipulator.hh:29: error: extra ';' make: *** Error 1
0
1024
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? Do someone has samples or sample code or any step-by-step instructions? Thanks alot
6
2612
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 inside template class definition (commented out at //1) then it compiles and runs fine, but if I declare and define the function separately (at //2). Is the following syntax supported by g++?
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.