vfscanf() Function

The vfscanf() function in C stdio.h reads formatted data from an input stream and assigns the extracted values to a variable argument list. It interprets a provided format string to determine how to parse the input and populate the corresponding variables.


Syntax of vfscanf()

</>
Copy
int vfscanf(FILE *stream, const char *format, va_list arg);

Parameters

ParameterDescription
streamPointer to a FILE object that identifies an input stream.
formatA C string containing the format to be used for reading data, following the same specifications as scanf().
argA variable argument list initialized with va_start that will receive the parsed input.

It is essential that the variable argument list (arg) is initialized with va_start before calling vfscanf() and later cleaned up using va_end. The function internally uses va_arg to extract the values, which may modify the state of the argument list.


Return Value

On success, vfscanf() returns the number of input items successfully matched and assigned. If a matching failure occurs before any conversion, or if an input error happens, the function returns EOF. In case of an encoding error when processing wide characters, errno is set to EILSEQ.


Examples for vfscanf()

Example 1: Reading an Integer and a String from a File

This example demonstrates how to use vfscanf() to read an integer and a string from a file.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

void readData(FILE *fp, const char *format, ...) {
    va_list args;
    va_start(args, format);
    vfscanf(fp, format, args);
    va_end(args);
}

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

    int number;
    char word[50];
    readData(fp, "%d %49s", &number, word);
    printf("Read integer: %d\n", number);
    printf("Read string: %s\n", word);

    fclose(fp);
    return 0;
}

Explanation:

  1. A file named data.txt is opened for reading.
  2. The helper function readData() initializes a variable argument list and calls vfscanf() using the provided format.
  3. An integer and a string are read from the file using the format "%d %49s".
  4. The extracted values are printed to the console.

Program Output:

Read integer: 123
Read string: example

Example 2: Handling Matching Failure and End-of-File

This example illustrates how vfscanf() handles scenarios where the expected input cannot be matched or the end-of-file is reached prematurely.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

void readData(FILE *fp, const char *format, ...) {
    va_list args;
    va_start(args, format);
    int result = vfscanf(fp, format, args);
    if (result == EOF) {
        perror("Read error or end-of-file reached");
    } else {
        printf("Number of items read: %d\n", result);
    }
    va_end(args);
}

int main() {
    FILE *fp = fopen("empty.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return EXIT_FAILURE;
    }

    int value;
    readData(fp, "%d", &value);

    fclose(fp);
    return 0;
}

Explanation:

  1. The file empty.txt is opened and is assumed to be empty or not contain the expected integer.
  2. The function readData() calls vfscanf() with a format expecting an integer.
  3. If no items are read, vfscanf() returns EOF and an error message is printed.
  4. If items are read, the number of successfully read items is displayed.

Example 3: Reading Multiple Values of Mixed Types

This example demonstrates how to use vfscanf() to read multiple values of different types from a file.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

void readData(FILE *fp, const char *format, ...) {
    va_list args;
    va_start(args, format);
    vfscanf(fp, format, args);
    va_end(args);
}

int main() {
    FILE *fp = fopen("multi.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return EXIT_FAILURE;
    }

    int num1, num2;
    char str[50];
    readData(fp, "%d %d %49s", &num1, &num2, str);
    printf("Read numbers: %d, %d\n", num1, num2);
    printf("Read string: %s\n", str);

    fclose(fp);
    return 0;
}

Explanation:

  1. The file multi.txt is opened for reading multiple values.
  2. The helper function readData() reads two integers and a string using the format "%d %d %49s".
  3. The function populates the provided variables with the corresponding values from the file.
  4. The extracted values are printed to verify successful data reading.

Program Output:

Read numbers: 10, 20
Read string: hello