ungetwc() considerations

For text files, the library functions fgetpos() and ftell() take into account the character you have pushed back onto the input stream with ungetwc(), and move the file position back by one wide character. The starting position for an fseek() call with a whence value of SEEK_CUR also takes into account this pushed-back wide character. Backing up one wide character means backing up either a single-byte character or a multibyte character, depending on the type of the preceding character. The implicit new-lines at the end of each record are counted as wide characters.

For binary files, the library functions fgetpos() and ftell() also take into account the character you have pushed back onto the input stream with ungetwc(), and adjust the file position accordingly. However, the ungetwc() must push back the same type of character just read by fgetwc(), so that ftell() and fgetpos() can save the state correctly. An fseek() with an offset of SEEK_CUR also accounts for the pushed-back character. Again, the ungetwc() must unget the same type of character for this to work properly. If the ungetwc() pushes back a character in the opposite state, you will get undefined behavior.

You can make only one call to ungetwc(). If the current logical file position is already at or before the first wchar in the file, a call to ftell() or fgetpos() after ungetwc() fails.

When you are using fseek() with a whence value of SEEK_CUR, the starting point for the reposition also accounts for the presence of ungetwc() characters and compensates as ftell() and fgetpos() do. Specifying a relative offset other than 0 is not supported and results in undefined behavior.

You can set the _EDC_COMPAT environment variable to specify that ungetwc() should not affect fgetpos() or fseek(). (It will still affect ftell().) If the environment variable is set, fgetpos() and fseek() ignore any pushed-back wide character. See Using environment variables for more information about _EDC_COMPAT.

If a repositioning operation fails, z/OS® XL C attempts to restore the original file position by treating the operation as a call to fflush(). It does not account for the presence of ungetwc() characters, which are lost.