Next: , Previous: , Up: Stdio   [Contents][Index]


4.65 swscanf, fwscanf, wscanf—scan and format wide character input

Synopsis

#include <stdio.h>

int wscanf(const wchar_t *__restrict format, ...);
int fwscanf(FILE *__restrict fd,
    const wchar_t *__restrict format, ...);
int swscanf(const wchar_t *__restrict str, 
    const wchar_t *__restrict format, ...);

int _wscanf_r(struct _reent *ptr, const wchar_t *format, ...);
int _fwscanf_r(struct _reent *ptr, FILE *fd, 
    const wchar_t *format, ...);
int _swscanf_r(struct _reent *ptr, const wchar_t *str,
    const wchar_t *format, ...);

Description
wscanf scans a series of input fields from standard input, one wide character at a time. Each field is interpreted according to a format specifier passed to wscanf in the format string at *format. wscanf stores the interpreted input from each field at the address passed to it as the corresponding argument following format. You must supply the same number of format specifiers and address arguments as there are input fields.

There must be sufficient address arguments for the given format specifiers; if not the results are unpredictable and likely disasterous. Excess address arguments are merely ignored.

wscanf often produces unexpected results if the input diverges from an expected pattern. Since the combination of gets or fgets followed by swscanf is safe and easy, that is the preferred way to be certain that a program is synchronized with input at the end of a line.

fwscanf and swscanf are identical to wscanf, other than the source of input: fwscanf reads from a file, and swscanf from a string.

The routines _wscanf_r, _fwscanf_r, and _swscanf_r are reentrant versions of wscanf, fwscanf, and swscanf that take an additional first argument pointing to a reentrancy structure.

The string at *format is a wide character sequence composed of zero or more directives. Directives are composed of one or more whitespace characters, non-whitespace characters, and format specifications.

Whitespace characters are blank ( ), tab (\t), or newline (\n). When wscanf encounters a whitespace character in the format string it will read (but not store) all consecutive whitespace characters up to the next non-whitespace character in the input.

Non-whitespace characters are all other ASCII characters except the percent sign (%). When wscanf encounters a non-whitespace character in the format string it will read, but not store a matching non-whitespace character.

Format specifications tell wscanf to read and convert characters from the input field into specific types of values, and store then in the locations specified by the address arguments.

Trailing whitespace is left unread unless explicitly matched in the format string.

The format specifiers must begin with a percent sign (%) and have the following form:

       %[*][width][size]type

Each format specification begins with the percent character (%). The other fields are:


Returns
wscanf returns the number of input fields successfully scanned, converted and stored; the return value does not include scanned fields which were not stored.

If wscanf attempts to read at end-of-file, the return value is EOF.

If no fields were stored, the return value is 0.

wscanf might stop scanning a particular field before reaching the normal field end character, or may terminate entirely.

wscanf stops scanning and storing the current field and moves to the next input field (if any) in any of the following situations:

When wscanf stops scanning the current input field for one of these reasons, the next character is considered unread and used as the first character of the following input field, or the first character in a subsequent read operation on the input.

wscanf will terminate under the following circumstances:

When the format string contains a wide character sequence that is not part of a format specification, the same wide character sequence must appear in the input; wscanf will scan but not store the matched characters. If a conflict occurs, the first conflicting wide character remains in the input as if it had never been read.


Portability
wscanf is C99, POSIX-1.2008.

Supporting OS subroutines required: close, fstat, isatty, lseek, read, sbrk, write.



Next: , Previous: , Up: Stdio   [Contents][Index]