Read formatted text from a stream.
#include <stdio.h> int fscanf(FILE *stream, const char *format, ...);
stream
A pointer to a file stream.
format
A pointer to a character string containing format information.
The fscanf() function reads programmer-defined, formatted text from stream. The function operates identically to the scanf() function with the addition of the stream argument indicating the stream to read from.
If the file is opened in update mode (+) a file cannot be read from and then written to without repositioning the file using one of the file positioning functions ( fseek(), fsetpos(), or rewind()) unless the last read or write reached the end-of-file.
This facility may not be available on configurations of the EWL that run on platforms without file systems.
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *f; int i; double x; char c; // create a new file for output and input if (( f = fopen("foobar", "w+")) == NULL) { printf("Can't create new file.\n"); exit(1); } // output formatted text to the file fprintf(f, "%d\n%f\n%c\n", 45, 983.3923, 'M'); // go to the beginning of the file rewind(f); // read from the stream using fscanf() fscanf(f, "%d %lf %c", &i, &x, &c); // close the file fclose(f); printf("The integer read is %d.\n", i); printf("The floating point value is %f.\n", x); printf("The character is %c.\n", c); return 0; } Output: The integer read is 45. The floating point value is 983.392300. The character is M.