C Standard Libraries C++

ungetwc()

Declaration

wint_t ungetwc(wint_t wChar, FILE* qpStream);

Description

This function pushes the wide-character "wChar" back into the stream "qpStream" at the current location and sets the file pointer back to that character. The function also clears the end-of-file indicator, if it is set. If the function was successful, the character "wChar" is returned. Otherwise, the function returns WEOF.

Input File

ungetwc() Input File

Example

#include <cwchar>
#include <cstdio>

int main()
{
    using namespace std;

    char* cpFileName = "XoaX.txt";
    FILE* qpFile = fopen(cpFileName, "r");

    // Check that the file could be opened
    if (!qpFile) {
        wprintf(L"Could not open file.\n");
        return 1;
    }

    // Read 8 chars from the file
    for (int iIndex = 0; iIndex < 8; ++iIndex) {
         int iReturn = getwc(qpFile);
         wprintf(L"%wc", (wchar_t)iReturn);
    }
    wprintf(L"\n");

    // Put 4 Ts back into the stream
    for (int iIndex = 0; iIndex < 4; ++iIndex) {
        wchar_t wChar = L'T';
        int iReturn = ungetwc(wChar, qpFile);
        wprintf(L"%wc", (wchar_t)iReturn);
    }
    wprintf(L"\n");

    // Read 10 chars from the file
    for (int iIndex = 0; iIndex < 10; ++iIndex) {
         int iReturn = getwc(qpFile);
         wprintf(L"%wc", (wchar_t)iReturn);
    }
    wprintf(L"\n");

    fclose(qpFile);
    // Keep the window open until "Enter" is pressed
    getwchar();
    return 0;
}

Output

ungetwc() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.