C Standard Libraries C++



wchar_t* fgetws(wchar_t* wpString, int iLength, FILE* qpStream);


This function reads up to "iLength" minus 1 wide characters into "wpString" from "qpStream." The function will also stop reading if it encounters a newline character. In which case, the newline character will put into the string as long as the number of characters does not exceed iLength - 1. The function will also stop reading if it reaches the end of the stream. If an error or end-of-file occurs, the function returns NULL instead of "wpString." Depending on whether the mode is binary or text, the function will read "wpString" as a wide-character or multibyte-character string, respectively.

Input File

fgetws() Input File


#include <iostream>
#include <cwchar>

int main()
    using namespace std;
    char* cpFileName = "XoaX.txt";
    FILE* qpFile = fopen(cpFileName, "r");

    // Check that the file could be opened
    if (!qpFile) {
        wcout << L"Could not open the file" << endl;
        return 1;

    // Read the first string of the file.
    wchar_t waString[20];
    wchar_t* wpReturn = fgetws(waString, 20, qpFile);
    if (wpReturn != NULL) {
        // Output the string
        wcout << L"String read: " << waString << endl;
    } else {
        wcout << L"Read Error" << endl;

    return 0;


fgetws() Output

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