C++ vfscanf()

The vfscanf() function in C++ is used to read the data from a file stream.

The vfscanf() function is defined in <cstdio> header file.

vfscanf() prototype

int vfscanf(FILE* stream, const char* format, va_list vlist );

The vfscanf() function reads the data from the file stream stream and stores the values into the respective locations as defined by vlist.


vfscanf() Parameters

  • stream: 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 %.
  • vlist: Variable argument list where the values are to be stored
    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, vfscanf() 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 vfscanf() 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
  • vlist: A list of arguments for receiving the inputs.

vfscanf() Return value

  • If successful, the vfscanf() function returns the number of arguments successfully read.
  • On failure, EOF is returned.

Example: How vfscanf() function works?

#include <cstdio>
#include <cstdarg>

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

int main ()
{
	char myFriends[5][20] = {"Robert", "Syd", "Brian", "Eddie", "Ray"};
	FILE *fp = fopen("example.txt","w+");
	char name[20];

	for (int i=0; i<5; i++)
		fprintf(fp, "%s ", myFriends[i]);
	rewind(fp);

	printf("Here are the list of my friends\n");
	for (int i=0; i<5; i++)
	{
		read(fp, "%s ", &name);
		printf("%s\n", name);
	}
	
	fclose(fp);
	return 0;
}

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

Here are the list of my friends
Robert
Syd
Brian
Eddie
Ray
Did you find this article helpful?