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()
int vfscanf(FILE *stream, const char *format, va_list arg);
Parameters
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies an input stream. |
format | A C string containing the format to be used for reading data, following the same specifications as scanf() . |
arg | A 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
#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:
- A file named
data.txt
is opened for reading. - The helper function
readData()
initializes a variable argument list and callsvfscanf()
using the provided format. - An integer and a string are read from the file using the format
"%d %49s"
. - 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
#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:
- The file
empty.txt
is opened and is assumed to be empty or not contain the expected integer. - The function
readData()
callsvfscanf()
with a format expecting an integer. - If no items are read,
vfscanf()
returnsEOF
and an error message is printed. - 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
#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:
- The file
multi.txt
is opened for reading multiple values. - The helper function
readData()
reads two integers and a string using the format"%d %d %49s"
. - The function populates the provided variables with the corresponding values from the file.
- The extracted values are printed to verify successful data reading.
Program Output:
Read numbers: 10, 20
Read string: hello