public member function
<streambuf> <iostream>

std::basic_streambuf::sputc

int_type sputc (char_type c);
Put character and advance to next position
The character c is stored at the current position of the controlled output sequence, and then advances the position indicator to the next character.

Internally, the function calls the virtual protected member overflow if there are no write positions available at the put pointer (pptr). Otherwise, the function uses the put pointer (pptr) directly, without calling virtual member functions.

Its behavior is the same as if implemented as:
1
2
3
4
5
6
7
int_type sputc(char_type c) {
  if ( (!pptr()) || (pptr()==epptr()) )
    return overflow(traits_type::to_int_type(c));
  *pptr()=c;
  pbump(1);
  return traits_type::to_int_type(c);
}


Parameters

c
Character to be put.
Member type char_type is the type of the characters in the stream buffer (the first class template parameter).

Return Value

In case of success, the character put is returned, converted to a value of type int_type using member traits_type::to_int_type.
Otherwise, it returns the end-of-file value (traits_type::eof()) to signal a failure.
Member type int_type is an integral type able to represent any character value or the special end-of-file value.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// typewriter - sputc() example
#include <iostream>     // std::cin, std::cout, std::streambuf
#include <fstream>      // std::ofstream

int main () {
  char ch;
  std::ofstream ostr ("test.txt");
  if (ostr) {
    std::cout << "Writing to file. Type a dot (.) to end.\n";
    std::streambuf * pbuf = ostr.rdbuf();
    do {
      ch = std::cin.get();
      pbuf->sputc(ch);
    } while (ch!='.');
    ostr.close();
  }

  return 0;
}


This example code writes every character to a file until a dot character (.) is introduced.

Data races

Modifies the stream buffer object.
Concurrent access to the same stream buffer object may introduce data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream buffer is in a valid state (this also applies to standard derived classes).

See also