.TH fscanf() "" "" "STDIO Function (libc)"
.PC "Format input from a file stream"
.B "#include <stdio.h>"
\fBint fscanf(\fIfp, format, arg1, .\^.\^. argN\^\fB)\fR
\fBFILE *\fIfp\^\fB; char *\fIformat\^\fB;\fR
\fB[\fIdata type\^\fB] *\fIarg1, .\^.\^. \fB*\fIargN\^\fB;\fR
.PP
.B fscanf()
reads the file stream pointed to by
.IR fp ,
and uses the string
.I format
to format the arguments
.I arg1
through
.IR argN ,
each of which must point to a variable of the appropriate data type.
.PP
.B fscanf()
returns either the number of arguments matched, or EOF if no arguments matched.
.PP
For more information on
.BR fscanf() 's
conversion codes,
see
.BR scanf() .
.SH Example
The following example uses \fBfprintf()\fR to write some data into
a file, and then reads it back using \fBfscanf()\fR.
.DM
#include <stdio.h>
.sp \n(pDu
main ()
{
	FILE *fp;
	char let[4];
.DE
.DM
	/* open file into write/read mode */
	if ((fp = fopen("tmpfile", "wr")) == NULL) {
		printf("Cannot open 'tmpfile'\en");
		exit(1);
	}
.DE
.DM
	/* write a string of chars into file */
	fprintf(fp, "1234");
.DE
.DM
	/* move file pointer back to beginning of file */
	rewind(fp);
.DE
.DM
	/* read and print data from file */
	fscanf(fp, "%c %c %c %c",
		&let[0], &let[1], &let[2], &let[3]);
	printf("%c %c %c %c\en",
		let[3], let[2], let[1], let[0]);
}
.DE
.SH "See Also"
.Xr "libc," libc
.Xr "scanf()," scanf
.Xr "sscanf()" sscanf
.br
\*(AS, \(sc7.9.6.2
.br
\*(PX Standard, \(sc8.1
.SH Notes
Because C does not perform type checking, it is essential that
an argument match its specification.
For that reason, 
.B fscanf()
is best used only to process data that
you are certain are in the correct data format, such as data
previously written out with
.BR fprintf() .
