ungetwc() considerations

ungetwc() pushes wide characters back onto the input stream for binary and text files. You can use it to push one wide character onto the ungetwc() buffer. Never use ungetc() on a wide-oriented file. After you call ungetwc(), calling fflush() backs up the file position by one wide character and clears the pushed-back wide character from the stream. Backing up by one wide character skips over shift characters and backs up to the start of the previous character (whether single-byte or double-byte). For text files, z/OS® XL C counts the new-lines added to the records as single-byte characters when it calculates the file position. For example, if you have the following stream,

Graphic showing ungetwc() considerations
you can run the code fragment shown in Figure 1.

Figure 1. ungetwc() Example
   fgetwc(fp);      /* Returns X'00C1' (the hexadecimal          */
                    /*    wchar representation of A)             */
   fgetwc(fp);      /* Returns X'00C2' (the hexadecimal          */
                    /*    wchar representation of B)             */
   fgetwc(fp);      /* Returns X'7FFE' (the hexadecimal          */
                    /*    wchar representation of the DBCS       */
                    /*    character) between the SO and SI       */
                    /*    characters; leaves file position at C  */
   ungetwc('Z',fp); /* Logically inserts Z before SI character   */
   fflush(fp);      /* Backs up one wchar, leaving position at   */
                    /*    beginning of X'7FFE' DBCS char         */
                    /*    and DBCS state in double-byte mode;    */
                    /*    clears Z from the logical stream       */

You can set the _EDC_COMPAT environment variable before you open the file, so that fflush() ignores any character pushed back with ungetwc() or ungetc(), and leaves the file position where it was when ungetwc() or ungetc() was first issued. Any characters pushed back are still cleared. For more information about _EDC_COMPAT, see Using environment variables.