##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_ostream::write

From cppreference.com
< cpp‎ | io‎ | basic ostream

basic_ostream& write( const char_type* s, std::streamsize count ); |  |

Behaves as an `UnformattedOutputFunction`. 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)

### [edit] Parameters

s | - | pointer to the character string to write
count | - | number of characters to write

### [edit] Return value

*this

### [edit] Notes

This function is not overloaded for the types signed char or unsigned char, unlike the formatted operator<<

Also, unlike the formatted output functions, this function does not set the `failbit` on failure.

### [edit] Exceptions

If an exception occurs during output and exceptions() & badbit != 0, rethrows that exception.

If output fails and exceptions() & badbit != 0, throws ios_base::failure.

### [edit] Example

This function may be used to output object representations, i.e. binary output

Run this code

    #include <iostream>

    int main()
    {
        int n = 0x41424344;
        std::cout.write(reinterpret_cast<char*>(&n), sizeof n) << '\n';

        char c[]="This is sample text.";
        std::cout.write(c,4)<<'\n';
    }

Possible output:

    DCBA
    This

### [edit] See also

operator<<(std::basic_ostream) | inserts character data (function template) [edit]
put | inserts a character (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ostream/write&oldid=88815"