std::fgetws

From cppreference.com
< cpp‎ | io‎ | c
 
 
 
C-style I/O
Types and objects
Functions
File access
Direct input/output
Unformatted input/output
Formatted input
(C++11)(C++11)(C++11)    
(C++11)(C++11)(C++11)    
 
Defined in header <cwchar>
wchar_t* fgetws( wchar_t* str, int count, std::FILE* stream );

Reads at most count - 1 wide characters from the given file stream and stores them in str. The produced wide string is always null-terminated. Parsing stops if end-of-file occurs or a newline wide character is found, in which case str will contain that wide newline character.

Parameters

str - wide string to read the characters to
count - the length of str
stream - file stream to read the data from

Return value

str on success, a null pointer on an error

Example

#include <array>
#include <clocale>
#include <cstdio>
#include <cstdlib>
#include <cwchar>
#include <iostream>
 
int main()
{
    // Create temp file that contains wide characters
    std::setlocale(LC_ALL, "en_US.utf8");
    std::FILE* tmpf = std::tmpfile();
 
    for (const wchar_t* text :{
        L"Tétraèdre"    L"\n",
        L"Cube"         L"\n",
        L"Octaèdre"     L"\n",
        L"Icosaèdre"    L"\n",
        L"Dodécaèdre"   L"\n",
        })
        if (int rc = std::fputws(text, tmpf); rc == EOF) {
            std::perror("fputws()"); // POSIX requires that errno is set
            return EXIT_FAILURE;
        }
 
    std::rewind(tmpf);
 
    std::array<wchar_t, 16> buf;
    while (std::fgetws(buf.data(), buf.size(), tmpf) != nullptr)
        std::wcout << L'"' << buf.data() << L'"' << L'\n';
 
    return EXIT_SUCCESS;
}

Possible output:

"Tétraèdre
"
"Cube
"
"Octaèdre
"
"Icosaèdre
"
"Dodécaèdre
"

See also

reads formatted wide character input from stdin, a file stream or a buffer
(function)
gets a wide character from a file stream
(function)
writes a wide string to a file stream
(function)