cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : istream : sync
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
istream
· istream::istream
· istream::~istream
member classes:
· istream::sentry
member functions:
· istream::gcount
· istream::get
· istream::getline
· istream::ignore
· istream::operator>>
· istream::peek
· istream::putback
· istream::read
· istream::readsome
· istream::seekg
· istream::sync
· istream::tellg
· istream::unget

-

istream::sync public member function
int sync ( );

Synchronize input buffer with source of characters

Synchronizes the buffer associated with the stream to its controlled input sequence. This effectively means that the unread characters in the buffer are discarded.

The function only has meaning for buffered streams, in which case it effectively calls the pubsync member of the streambuf object (rdbuf()->pubsync()) associated to the input sequence.

Parameters

none

Return Value

If the stream is a buffered stream and the function is successful, zero is returned.
If the stream is unbuffered, the function returns -1.
On error, the badbit flag is set (which can be checked with member bad). Also, depending on the values set through member exceptions, an exception may be thrown in this case.

Example

// read a file into memory
#include <iostream>
using namespace std;

int main () {
  char first, second;

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

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

  cout << "The first word began by " << first << endl;
  cout << "The second word began by " << second << endl;

  return 0;
}

This example demonstrates how sync behaves on cin, removing any unread character from the standard input queue of characters.

Basic template member declaration

( basic_istream<charT,traits> )
int sync ();

See also

ostream::flush Flush output stream buffer (public member function)
streambuf::pubsync Synchronize stream buffer (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us