C++ Ostream Library - put



Description

It is used to inserts character c into the stream.this function accesses the output sequence by first constructing a sentry object. Then (if good), it inserts c into its associated stream buffer object as if calling its member function sputc, and finally destroys the sentry object before returning.

Declaration

Following is the declaration for std::ostream::put.

ostream& put (char c);

Parameters

c − Character to write.

Return Value

It returns the ostream object (*this).

Exceptions

Basic guarantee − if an exception is thrown, the object is in a valid state.

Data races

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.

Example

In below example explains about std::ostream::put.

#include <iostream>
#include <fstream>

int main () {
   std::ofstream outfile ("test.txt");
   char ch;

   std::cout << "Type some text (type a dot to finish):\n";
   do {
      ch = std::cin.get();
      outfile.put(ch);
   } while (ch!='.');

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Type some text (type a dot to finish):
tutorialspoint.
ostream.htm
Advertisements