C++ basic_ios Library - sync


Description

It is used to synchronize input buffer.

Declaration

Following is the declaration for std::basic_istream::sync.

int sync();

Parameters

none

Return Value

If the function fails, either because no stream buffer object is associated to the stream (rdbuf is null), or because the call to its pubsync member fails, it returns -1.therwise, it returns zero, indicating success.

Exceptions

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

Data races

Modifies the stream object.

Example

In below example for std::basic_istream::sync.

#include <iostream>     

int main () {
   char first, second;

   std::cout << "Please, enter a word: ";
   first = std::cin.get();
   std::cin.sync();

   std::cout << "Please, enter another word: ";
   second = std::cin.get();

   std::cout << "The first word began by " << first << '\n';
   std::cout << "The second word began by " << second << '\n';

   return 0;
}

The output should be like this −

Please, enter a word: test
Please enter another word: text
The first word began by t
The second word began by t
istream.htm
Advertisements