C++ fwscanf()

The fwscanf() function in C++ reads wide character from file stream.

The fwscanf() function is defined in <cwchar> header file.

fwscanf() prototype

int fwscanf( FILE* stream, const wchar_t* format, ... );

The fwscanf() function reads the data from the file stream stream and stores the values into the respective variables.


fwscanf() Parameters

  • stream: The input file stream to read the data from.
  • format: Pointer to a null-terminated character string that specifies how to read the input. It consists of format specifiers starting with %.
    The format string has the following parts:
    • Non whitespace characters except % each of which consumes one identical character from the input stream. It can cause the function to fail if the next character on the stream does not compare equal.
    • Whitespace character: All the consecutive whitespace characters are treated as single whitespace character. Further, '\n', '\t' and ' ' are considered same.
    • Conversion specification: It follows the following format:
      • Initial % character that specifies the beginning
      • An optional * called assignment-suppressing character. If this character is present, fwscanf() does not assign the result to any receiving argument.
      • An optional positive integer number that specifies maximum field width. It specifies the maximum number of characters that fwscanf() is allowed to consume when doing the conversion specified by the current conversion specification.
      • An optional length modifier specifying the size of the receiving argument.
      • A conversion format specifier.
    Format specifiers
    Format Specifier Description
    % Matches the literal %
    c Matches a single character or multiple characters. If width is defined, matches exactly width characters.
    s Matches consecutive non whitespace characters. If width is defined, matches exactly width characters or until first whitespace is found.
    [set] Matches a non empty sequence of character from the given set of characters. If ^ is present at the beginning of set, then all the characters not in set are matched.
    d Matches a decimal integer.
    i Matches an integer.
    o Matches an unsigned octal integer.
    X or x Matches an unsigned hexadecimal integer.
    u Matches an unsigned decimal integer.
    A or a, E or e, F or f, G or g Matches a floating-point number.
    n Returns the number of characters read so far.
    p Matches an implementation defined character sequence defining a pointer.

    So the general format of format specifier is:
    %[*][width][length]specifier
  • … : Other additional arguments for receiving data. They occur in a sequence according to the format specifier.

fwscanf() Return value

  • The fwscanf() function returns the number of receiving arguments successfully assigned.
  • If failure occurs before the first receiving argument was assigned, EOF is returned.

Example: How fwscanf() function works?

#include <cwchar>
#include <clocale>
#include <cwctype>
#include <cstdio>

int main()
{
	FILE *fp = fopen("example.txt","w+");
	wchar_t str[10], ch;

	setlocale(LC_ALL, "en_US.UTF-8");
	fwprintf(fp, L"%ls %lc", L"Summation", L'\u2211');
	fwprintf(fp, L"%ls %lc", L"Integral", L'\u222b');

	rewind(fp);
	
	while((fwscanf(fp, L"%ls %lc", str, &ch))!=EOF)
	{
		wprintf(L"%lc is %ls\n", ch, str);
	}

	fclose(fp);
	return 0;
}

When you run the program, a possible output will be:

∑ is Summation
∫ is Integral