std:: istream::sync
Synchronizes the associated stream buffer with its controlled input sequence.
Specifics of the operation depend on the particular implementation of the stream buffer object associated to the stream.
Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true ). Then (if good ), it calls pubsync on its associated stream buffer object (if rdbuf is null, the function returns -1 instead). Finally, it destroys the sentry object before returning.
If the call to pubsync fails (i.e., it returns -1 ), the function sets the badbit flag, and returns -1 . Otherwise it returns zero, indicating success.
Notice that the called function may succeed when no action is performed, if that is the behavior defined for the stream buffer object on synchronization.
Calling this function does not alter the value returned by gcount .
Parameters
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 .
Otherwise, it returns zero, indicating success.
Errors are signaled by modifying the internal state flags:
| flag | error |
|---|---|
| eofbit | — |
| failbit | The construction of sentry failed (such as when the stream state was not good before the call). |
| badbit | Either the internal call to pubsync returned -1 , or some other error occurred on the stream (such as when the function catches an exception thrown by an internal operation). When set, the integrity of the stream may have been affected. |
Multiple flags may be set by a single operation.
If the operation sets an internal state flag that was registered with member exceptions , the function throws an exception of member type failure .
Example
This example demonstrates how sync behaves on certain implementations of cin , removing any unread character from the standard input queue of characters.
Cin sync c что это
What is a buffer?
A temporary storage area is called a buffer. All standard input and output devices contain an input and output buffer. In standard C/C++, streams are buffered. For example, in the case of standard input, when we press the key on the keyboard, it isn’t sent to your program, instead of that, it is sent to the buffer by the operating system, till the time is allotted to that program.
How does it affect Programming?
On various occasions, you may need to clear the unwanted buffer so as to get the next input in the desired container and not in the buffer of the previous variable. For example, in the case of C after encountering “scanf()”, if we need to input a character array or character, and in the case of C++, after encountering the “cin” statement, we require to input a character array or a string, we require to clear the input buffer or else the desired input is occupied by a buffer of the previous variable, not by the desired container. On pressing “Enter” (carriage return) on the output screen after the first input, as the buffer of the previous variable was the space for a new container(as we didn’t clear it), the program skips the following input of the container.
In the case of C Programming
std:: istream::sync
Synchronizes the associated stream buffer with its controlled input sequence.
Specifics of the operation depend on the particular implementation of the stream buffer object associated to the stream.
Internally, the function accesses the input sequence by first constructing a sentry object (with noskipws set to true ). Then (if good ), it calls pubsync on its associated stream buffer object (if rdbuf is null, the function returns -1 instead). Finally, it destroys the sentry object before returning.
If the call to pubsync fails (i.e., it returns -1 ), the function sets the badbit flag, and returns -1 . Otherwise it returns zero, indicating success.
Notice that the called function may succeed when no action is performed, if that is the behavior defined for the stream buffer object on synchronization.
Calling this function does not alter the value returned by gcount .
Parameters
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 .
Otherwise, it returns zero, indicating success.
Errors are signaled by modifying the internal state flags:
| flag | error |
|---|---|
| eofbit | — |
| failbit | The construction of sentry failed (such as when the stream state was not good before the call). |
| badbit | Either the internal call to pubsync returned -1 , or some other error occurred on the stream (such as when the function catches an exception thrown by an internal operation). When set, the integrity of the stream may have been affected. |
Multiple flags may be set by a single operation.
If the operation sets an internal state flag that was registered with member exceptions , the function throws an exception of member type failure .
Example
This example demonstrates how sync behaves on certain implementations of cin , removing any unread character from the standard input queue of characters.
The difference between cin.ignore and cin.sync
What is the difference between cin.ignore and cin.sync ?
1 Answer 1
cin.ignore discards characters, up to the number specified, or until the delimiter is reached (if included). If you call it with no arguments, it discards one character from the input buffer.
For example, cin.ignore (80, ‘\n’) would ignore either 80 characters, or as many as it finds until it hits a newline.
cin.sync discards all unread characters from the input buffer. However, it is not guaranteed to do so in each implementation. Therefore, ignore is a better choice if you want consistency.
cin.sync() would just clear out what’s left. The only use I can think of for sync() that can’t be done with ignore is a replacement for system («PAUSE»); :
With cin.ignore() and cin.get() , this could be a bit of a mixture:
If there was a newline left over, just putting ignore will seem to skip it. However, putting both will wait for two inputs if there is no newline. Discarding anything that’s not read solves that problem, but again, isn’t consistent.