public member function
<ostream> <iostream>

std::basic_ostream::write

basic_ostream& write (const char_type* s, streamsize n);
Write block of data
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.

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.
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.

Parameters

s
Pointer to an array of at least n elements.
Member type char_type is the type of characters used by the stream (i.e., its first class template parameter, charT).
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 basic_ostream object (*this).

Errors are signaled by modifying the internal state flags:
flagerror
eofbit-
failbitMay be set if the construction of sentry failed.
badbitEither 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.
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.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copy a file
#include <fstream>      // std::ifstream, std::ofstream

int main () {
  std::ifstream infile ("test.txt",std::ifstream::binary);
  std::ofstream outfile ("new.txt",std::ofstream::binary);

  // get size of file
  infile.seekg (0,infile.end);
  long size = infile.tellg();
  infile.seekg (0);

  // allocate memory for file content
  char* buffer = new char[size];

  // read content of infile
  infile.read (buffer,size);

  // write to outfile
  outfile.write (buffer,size);

  // release dynamically-allocated memory
  delete[] buffer;

  outfile.close();
  infile.close();
  return 0;
}

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, wcout, wcerr and wclog) 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