C language fscanf of function and vfscanf of function use

  • 2020-04-02 03:18:24
  • OfStack

C language fscanf() function: input function (more commonly used)
The header file:


#include <stdio.h>

Definition function:


int fscanf(FILE * stream, const char *format, ...);

Fscanf () will read the string from the file stream of the parameter stream, and then convert and format the data according to the parameter format string. Please refer to scanf() for the format conversion form. The converted structure is stored in the corresponding parameters.

Return value: returns the number of arguments on success, -1 on failure, and the reason for the error is in errno.

sample


#include <stdio.h>
main()
{
  int i;
  unsigned int j;
  char s[5];
  fscanf(stdin, "%d %x %5[a-z] %*s %f", &i, &j, s, s);
  printf("%d %d %s n", i, j, s);
}

Perform:


10 0x1b aaaaaaaaa bbbbbbbbbb //Input from the keyboard
10 27 aaaaa

C language vfscanf() function: input function (first format the string and then input)
The header file:


#include <stdio.h>

Definition function:


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

Vfscanf () reads a string from the file stream of the parameter stream, then converts and formats the data according to the parameter format string. Refer to scanf() for the format conversion. The converted results are stored in the corresponding parameters. Refer to appendix C or vprintf() for the usage of va_list.

Return value: returns the number of arguments on success, -1 on failure, and the reason for the error is in errno.


Related articles: