In C, fgetws() reads a line of wide characters from a stream. It belongs to the wide-character tools declared in <wchar.h>.
Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.
Syntax
The following concrete call shows the argument shape before the formal declaration.
C
fgetws(buffer, 32, fp);
Call
What it does
fgetws(buffer, 32, fp)
Reads a line of wide characters from a stream in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Library declaration
wchar_t *fgetws(wchar_t *restrict ws, int n, FILE *restrict stream);
Parameters
Parameter
Description
ws
A wide-string buffer, source string, or format string.
n
The buffer capacity or maximum number of elements to process.
stream
The FILE stream to read, write, inspect, or push back into.
Return Value
Return Value
Description
ws
The destination buffer on success.
NULL
No characters were read because of EOF or an error.
C Examples
Example 1 — Basic stream operation
This example uses fgetws() to basic stream operation, then exposes the result so you can connect the call with its output.
Prepare a practical variation. The data is arranged specifically to use wide-oriented i/o.
Use the stream correctly.fgetws(buffer, 32, fp) performs the wide-oriented stream operation.
Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
Interpret the output.Hello confirms the behavior demonstrated by this example.
Frequently Asked Questions
fgetws() reads a line of wide characters from a stream. It is part of the standard wide-character library.
Its parameters are ws, n, stream. ws: A wide-string buffer, source string, or format string. n: The buffer capacity or maximum number of elements to process. stream: The FILE stream to read, write, inspect, or push back into.
ws means the destination buffer on success. NULL means no characters were read because of EOF or an error.
Include <wchar.h> for fgetws(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
Do not mix byte I/O and wide I/O after a stream becomes oriented. Check WEOF or the documented status before using data.