COHERENT manpages

This page displays the COHERENT manpage for fscanf() [Format input from a file stream].

List of available manpages
Index


fscanf() -- STDIO Function (libc)

Format input from a file stream
#include <stdio.h>
int fscanf(fp, format, arg1, ... argN)
FILE *fp; char *format;
[data type] *arg1, ... *argN;

fscanf() reads the file stream pointed to by fp, and uses the string format
to format  the arguments arg1 through  argN, each of which  must point to a
variable of the appropriate data type.

fscanf()  returns either  the number  of  arguments matched,  or EOF  if no
arguments matched.

For more information on fscanf()'s conversion codes, see scanf().

Example

The following  example uses fprintf() to  write some data into  a file, and
then reads it back using fscanf().

#include <stdio.h>

main ()
{
    FILE *fp;
    char let[4];

    /* open file into write/read mode */
    if ((fp = fopen("tmpfile", "wr")) == NULL) {
        printf("Cannot open 'tmpfile'\n");
        exit(1);
    }

    /* write a string of chars into file */
    fprintf(fp, "1234");

    /* move file pointer back to beginning of file */
    rewind(fp);

    /* 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\n",
        let[3], let[2], let[1], let[0]);
}

See Also

libc,
scanf(),
sscanf()
ANSI Standard, §7.9.6.2
POSIX Standard, §8.1

Notes

Because C does not perform type  checking, it is essential that an argument
match its  specification.  For that reason,  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 fprintf().