function
<cwchar>
ungetwc
wint_t ungetwc (wint_t wc, FILE* stream);
Unget wide character from stream
The wide character wc is virtually put back into an input stream, decreasing its internal file position as if a previous getwc operation was undone.
This wide character may or may not be the one read from the stream in the preceding input operation. In any case, the next wide character retrieved from stream is wc, independently of the original one.
Notice though, that this only affects further input operations on that stream, and not the content of the physical file associated with it, which is not modified by any calls to this function.
Some library implementations may support this function to be called multiple times, making the characters available in the reverse order in which they were put back. Although this behavior has no standard portability guarantees, and further calls may simply fail after any number of calls beyond the first.
If successful, the function clears the end-of-file indicator of stream (if it was currently set). The position indicator has unspecified value until all characters put back with ungetwc have been read again or discarded.
A call to fseek, fsetpos or rewind on stream will discard any characters previously put back into it with this function.
If the argument passed as wc is WEOF, the operation fails and the input stream remains unchanged.
This is the wide character equivalent of ungetc (<cstdio>).
Parameters
- wc
- The wint_t promotion of the wide character to be put back.
The value is internally converted to a wchar_t when put back.
- stream
- Pointer to a FILE object that identifies an input stream.
The stream shall not have an orientation yet, or be wide-oriented (the first i/o operation on a stream determines whether it is byte- or wide- oriented, see fwide).
Return Value
On success, the wide character put back is returned.
If the operation fails, WEOF is returned.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
/* ungetwc example */
#include <stdio.h>
#include <wchar.h>
int main ()
{
FILE * pFile;
wint_t wc;
wchar_t buffer [256];
pFile = fopen ("myfile.txt","rt");
if (pFile!=NULL) while (!feof (pFile)) {
wc=getwc (pFile);
if (wc != WEOF) {
if (wc == L'#') ungetwc (L'@',pFile);
else ungetwc (wc,pFile);
fgetws (buffer,255,pFile);
fputws (buffer,stdout);
}
}
return 0;
}
|
This example opens an existing file called myfile.txt for reading and prints its lines, but first gets the first character of every line and puts it back into the stream except if the line begins with #, in which case it is replaced by @.
See also
- ungetc
- Unget character from stream (function)
- getwc
- Get wide character from stream (function)
- fgetwc
- Get wide character from stream (function)
- putwc
- Write wide character to stream (function)