CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 23 Jul 2025 15:02:26 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/ostream/ostream/write/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 23 Jul 2025 15:02:27 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"7bea-4yQ4/iwZ0XPnsXomgmYARWYKPRk"
Content-Encoding: gzip
Inserts the first n characters of the array pointed by s into the stream.
This function simply copies a block of data, without checking its contents: The array may contain null characters, which are also copied without stopping the copying process.
Errors are signaled by modifying the internal state flags:
Multiple flags may be set by a single operation.
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
This example copies a file into memory and then writes its content to a new file.
Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog) when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).
It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
ostream
- ostream::~ostream
- ostream::ostream
member classes
member functions
non-member overloads
protected members
-
ostream::operator=C++11
-
ostream::swapC++11
-
public member function
<ostream> <iostream>
std::ostream::write
ostream& write (const char* s, streamsize n);
Write block of data
This function simply copies a block of data, without checking its contents: The array may contain null characters, which are also copied without stopping the copying process.
Internally, the function accesses the output sequence by first constructing a sentry object.
Then (if good), it inserts character into its associated stream buffer object as if calling its member function sputc until n characters have been written or until an insertion fails (in this case it sets the badbit flag).
Finally, it destroys the sentry object before returning.
Then (if good), it inserts character into its associated stream buffer object as if calling its member function sputc until n characters have been written or until an insertion fails (in this case it sets the badbit flag).
Finally, it destroys the sentry object before returning.
Internally, the function accesses the output sequence by first constructing a sentry object.
Then (if good), it inserts character into its associated stream buffer object as if calling its member functions sputc or sputn until n characters have been written or until an insertion fails (in this case it sets the badbit flag).
Finally, it destroys the sentry object before returning.
Then (if good), it inserts character into its associated stream buffer object as if calling its member functions sputc or sputn until n characters have been written or until an insertion fails (in this case it sets the badbit flag).
Finally, it destroys the sentry object before returning.
Parameters
- s
- Pointer to an array of at least n characters.
- n
- Number of characters to insert.
Integer value of type streamsize representing the size in characters of the block of data to write.
streamsize is a signed integral type.
Return Value
The ostream object (*this
).Errors are signaled by modifying the internal state flags:
flag | error |
---|---|
eofbit | - |
failbit | May be set if the construction of sentry failed. |
badbit | Either an insertion on the stream failed, or some other error happened (such as when this function catches an exception thrown by an internal operation). When set, the integrity of the stream may have been affected. |
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
Example
|
|
This example copies a file into memory and then writes its content to a new file.
Data races
Access up to n characters pointed by s.Modifies the stream object.
Concurrent access to the same stream object may cause data races, except for the standard stream objects (cout, cerr, clog) when these are synchronized with stdio (in this case, no data races are initiated, although no guarantees are given on the order in which characters from multiple threads are inserted).
Exception safety
Basic guarantee: if an exception is thrown, the object is in a valid state.It throws an exception of member type failure if the resulting error state flag is not goodbit and member exceptions was set to throw for that state.
Any exception thrown by an internal operation is caught and handled by the function, setting badbit. If badbit was set on the last call to exceptions, the function rethrows the caught exception.
See also
- ostream::put
- Put character (public member function)
- ostream::operator<<
- Insert formatted output (public member function)
- istream::read
- Read block of data (public member function)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us