fwscanf - Linux


Overview

fwscanf reads formatted data from a wide character stream, interpreting it according to the specified format and storing the results in the arguments. It is useful for parsing text files, extracting specific information from user input, and manipulating wide character data.

Syntax

#include <wchar.h>

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

Parameters:

  • stream: The wide character input stream to read from.
  • format: A wide character string specifying the format of the input. See scanf for details on format specifiers.
  • : Optional arguments to receive the parsed values.

Options/Flags

There are no specific options or flags for fwscanf.

Examples

Extract a float and an integer from a text file:

#include <stdio.h>
#include <wchar.h>

int main() {
    FILE *file = fopen("data.txt", "r");
    if (file == NULL) {
        perror("fopen");
        return 1;
    }

    float value1;
    int value2;
    int result = fwscanf(file, L"%f %d", &value1, &value2);
    if (result == 2) {
        printf("Parsed: %.2f, %d\n", value1, value2);
    } else {
        printf("Error parsing input\n");
    }

    fclose(file);
    return 0;
}

Parse formatted user input:

#include <stdio.h>
#include <wchar.h>

int main() {
    wchar_t input[256];
    printf("Enter a number: ");
    wscanf(L"%lf", input);
    double number = wcstod(input, NULL);
    printf("Entered number: %.2f\n", number);

    return 0;
}

Common Issues

  • Invalid format string: Ensure that the format string matches the data being parsed.
  • Insufficient arguments: Provide enough arguments to receive all the parsed values.
  • File open errors: Verify that the input file can be opened successfully.

Integration

fwscanf can be combined with other IO functions like fprintf to write wide character formatted data. It can also be used in conjunction with string manipulation functions like wcscpy and wcslen to process wide character strings.

Related Commands

  • scanf: Reads formatted data from stdin.
  • fscanf: Reads formatted data from a file.
  • swscanf: Reads formatted data from a wide character string.
  • vscanf: Reads formatted data with a variable number of arguments.