public member function
<streambuf> <iostream>

std::streambuf::sputc

int sputc (char c);
Store character at current put position and increase put pointer
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
int sputc(char c) {
  if ( (!pptr()) || (pptr()==epptr()) ) return overflow(c);
  *pptr()=c;
  pbump(1);
  return c;
}

Parameters

c
Character to be put.

Return Value

In case of success, the character put is returned, as a value of type int.
Otherwise, it returns the end-of-file value (EOF) to signal a failure.

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