From cppreference.com
basic_ostream&write(constchar_type*s,std::streamsizecount);Behaves as an
. After constructing and checking the sentry object, outputs the characters from successive locations in the character array whose first element is pointed to by s. Characters are inserted into the output sequence until one of the following occurs:
exactly count characters are inserted
inserting into the output sequence fails (in which case setstate(badbit) is called).
Parameters
s - pointer to the character string to write count - number of characters to write Return value
*this
Exceptions
if an error occurred (the error state flag is not
) and
is set to throw for that state.
If an internal operation throws an exception, it is caught and
is set. If
is set for badbit, the exception is rethrown.
Notes
This function is not overloaded for the types signedchar or unsignedchar, unlike the formatted
.
Also, unlike the formatted output functions, this function does not set the
on failure.
When using a non-converting locale (the default locale is non-converting), the overrider of this function in
may be optimized for zero-copy bulk I/O (by means of overriding
).
Example
This function may be used to output object representations, i.e. binary output
Run this code
#include<iostream>intmain(){intn=0x41424344;std::cout.write(reinterpret_cast<char*>(&n),sizeofn)<<'\n';charc[]="This is sample text.";std::cout.write(c,4).write("!\n",2);}Possible output:
DCBA This! See also