basic_ostream, endl, ends, flush, operator<<, ostream, wostream - defines the templateclass that performs insertions
A Gentle Introduction to C IO Streams By Manasij Mukherjee One of the great strengths of C is its I/O system, IO Streams. As Bjarne Stroustrup says in his book 'The C Programming Language', 'Designing and implementing a general input/output facility for a programming language is notoriously difficult'.
C Not Null Hi, i am trying to convert a simple integer or byte or anything else to a string in order to write to a file on disk. It does not work the way i did it for years with e.g. Microsoft C7.0 (Win3.1) and Visual C 6.0 (W32). C Not Null Dec 27, 2018 The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.
Synopsis
Description
Include the iostreamsstandard header <ostream>
to definetemplateclass basic_ostream
,which mediates insertions for the iostreams.The header also defines several relatedmanipulators.(This header is typically included for you by anotherof the iostreams headers. You seldom have occasion to include itdirectly.)
basic_ostream
basic_ostream, flush, operator<<, opfx, osfx, put, seekp, sentry, tellp, write
The template class describes an object that controlsinsertion of elements and enSTRONGd objects into astream bufferwith elements of type Eso known aschar_typeosecharacter traits are determined by theclass T
, also known astraits_type
.
Most of the member functions that overloadoperator<<
are formatted output functions.They follow the pattern:
Two other member functions areunformatted output functions.They follow the pattern:
Both groups of functions callsetstate(badbit)
if they encounter a failure while inserting elements.
An object of class basic_istream<E, T>
stores onlya virtual public base object of classbasic_ios<E, T>
basic_ostream::basic_ostream
The constructor initializes the base class by callinginit(sb)
.
basic_ostream::flush
If rdbuf()
isnot a null pointer, the function callsrdbuf()->pubsync()
.If that returns -1, the function callssetstate(badbit)
.It returns *this
.
basic_ostream::operator<<
The first member function ensures that an expression of theform ostr << endl
callsendl(ostr)
, then returns *this
.The second and third functions ensure that othermanipulators,such as hex
behavesimilarly. The remaining functions are allformatted output functions.
The function:
extracts elements from sb
,if sb
is not a null pointer, and inserts them.Extraction stops on end-of-file,or if an extraction throws an exception (which is rethrown).It also stops, without extracting the element in question,if an insertion fails. If the function inserts no elements, orif an extraction throws an exception, the function callssetstate(failbit)
.In any case, the function returns *this
.
The function:
converts n
to a boolean field and inserts it by callinguse_facet<num_put<E,OutIt>(getloc()).put(OutIt(rdbuf()), *this,getloc(), n)
. Here, OutIt
is defined asostreambuf_iterator<E,T>
.The function returns *this
.
The functions:
each convert n
to a numeric field and insert it by callinguse_facet<num_put<E,OutIt>(getloc()).put(OutIt(rdbuf()), *this,getloc(), n)
. Here, OutIt
is defined asostreambuf_iterator<E,T>
.
The function returns *this
.
The functions:
each convert n
to a numeric field and insert it by callinguse_facet<num_put<E,OutIt>(getloc()).put(OutIt(rdbuf()), *this,getloc(), n)
. Here, OutIt
is defined asostreambuf_iterator<E,T>
. The function returns *this
.
basic_ostream::opfx
If good()
is true, andtie()
is nota null pointer, the member function callstie->flush()
.It returns good()
.
You should not call opfx
directly.It is called as needed by an object of classsentry
.
basic_ostream::osfx
If flags() &unitbuf
is nonzero,the member function callsflush()
.You should not call osfx
directly.It is called as needed by an object of classsentry
.
basic_ostream::put
The unformatted output functioninserts the element c
. It returns *this
.
basic_ostream::seekp
If fail()
is false,the first member function callsrdbuf()->pubseekpos(pos)
.If fail()
is false, the second function callsrdbuf()->pubseekoff(off,way)
. Both functions return *this
.
basic_ostream::sentry
The nested class describes an object whose declaration structures theformatted output functionsand theunformatted output functions.The constructor effectively callsos.opfx()
andstores the return value. operator bool()
delivers thisreturn value. The destructor effectively callsos.osfx()
,but only ifuncaught_exception()
returns false.
basic_ostream::tellp
If fail()
is false,the member function returnsrdbuf()->pubseekoff(0,cur,in)
.Otherwise, it returns pos_type(-1)
.
basic_ostream::write
The unformatted output functioninserts the sequence of n
elementsbeginning at s
.
endl
The manipulator callsos.put(os.widen('n'))
,then callsos.flush()
.It returns os
.
ends
The manipulator callsos.put(E('0'))
.It returns os
.
flush
The manipulator callsos.flush()
.It returns os
.
operator<<
The template function:
is aformatted output functionsthat determines the length n =traits_type::length(s)
of the sequence beginning at s
, and inserts the sequence. Ifn < os.width()
,then the function also inserts a repetition of os.width() - n
fill characters.The repetition precedes the sequence if(os.flags() &adjustfield !=left
.Otherwise, the repetition follows the sequence.The function returns os
.
The template function:
inserts the element c
. If1 < os.width()
,then the function also inserts a repetition of os.width() - 1
fill characters.The repetition precedes the sequence if(os.flags() &adjustfield !=left
.Otherwise, the repetition follows the sequence.It returns os
.
The template function:
behaves the same as:
except that each element c
of the sequence beginningat s
is converted to an object of type E
by callingos.put(os.widen(c))
.
The template function:
behaves the same as:
except that c
is converted to an objectof type E
by callingos.put(os.widen(c))
.
The template function:
behaves the same as:
(It does not have to widen the elements before inserting them.)
The template function:
behaves the same as:
(It does not have to widen c
before inserting it.)
The template function:
returns os << (const char *)s
.
The template function:
returns os << (char)c
.
The template function:
returns os << (const char *)s
.
The template function:
returns os << (char)c
.
ostream
The type is a synonym for template classbasic_ostream
, specializedfor elements of type char with defaultcharacter traits.
wostream
The type is a synonym for template classbasic_ostream
, specializedfor elements of type wchar_t
with defaultcharacter traits.
References
exception(C++std) ,ios(C++std) ,iterator(C++std) ,locale(C++std) ,streambufC Ostream Dev Null Command
(C++std) ,stringC Ostream Dev Null
(C++std)18 February 2000© 2000 The Santa Cruz Operation, Inc. All rights reserved.
Copyright © 1992-1996by P.J. Plauger. Portions derived from workcopyright © 1994by Hewlett-Packard Company. All rights reserved.
Standard no-op output stream (4)
Is there a way to create an ostream instance which basically doesn't do anything ?
For example :
I could just create an ostringstream, but data will be buffered (and I really don't want to make anything with them, so it adds a useless overhead).
Any idea ?
/dev/null Minecraft
[edit] Found this related question which suits my needs. However, I think it could be useful to have a answer saying how to create a valid (no badbit) output stream with standard c++.
I needed a null stream that was of type ostream so I did something like this:
Application code:
C Ostream Dev Null Function
The real issue is all the public methods that I inherited but don't care about so I just didn't bother overriding them.