Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ () — Coherent 3.1.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought


fscanf()                      STDIO                      fscanf()




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]);


COHERENT Lexicon                                           Page 1



fscanf()                      STDIO                      fscanf()



}


***** See Also *****

scanf(), sscanf(), STDIO

***** 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.











































COHERENT Lexicon                                           Page 2


Typewritten Software • bear@typewritten.org • Edmonds, WA 98026