Chapter Contents

Previous

Next
fscanf

fscanf



Read Formatted Input from a File

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
DIAGNOSTICS
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

int fscanf(FILE *f, const char *format, loc1, loc2, ...);


DESCRIPTION

fscanf reads formatted input from the FILE designated by f according to the format specified by the string format . Following the format in the argument list may be one or more additional pointers ( loc1, loc2,..., locn ), addressing storage where the input values are stored.

format points to a string that contains zero or more of the following:

The format string contains format specifiers or characters to be matched from the input. Format items have the following form:

%[*][ width][~type] [h | l | L | hh | z | t | ll | j]form

The specifiers have the following meanings:

The format string is a C string. With the exception of the c and [ or < specifiers, white-space characters in the format string cause white-space characters in the input to be skipped. Characters other than format specifiers are expected to match the next nonwhite-space character in the input. The input is scanned through white space to locate the next input item in all cases except the c and [ ] specifiers, where the initial scan is bypassed. The s specifier terminates on any white space.

The fscanf formats are described in more detail in the ISO/ANSI C standard. As an extension, uppercase characters may also be used for the format characters specified in lowercase in the previous list.


RETURN VALUE

fscanf returns EOF if end of file (or an input error) occurs before any values are stored. If values are stored, it returns the number of items stored; that is, the number of times a value is assigned with one of the fscanf argument pointers.


DIAGNOSTICS

EOF is returned if an error occurs before any items are matched.


IMPLEMENTATION

The format string can also contain multibyte characters. For details on how fscanf treats multibyte characters in the format string and in conversions, see Chapter 11 in the SAS/C Library Reference, Volume 2.

Because square brackets do not exist on some 370 I/O devices, the library allows the format %[xyz] to be replaced by the alternate form %<xyz> . This is not a portable format.


EXAMPLE

This example writes out the data stored in lines to a temporary file and reads them back with fscanf:

#include <stdio.h>
#include <stdlib.h>

static char *lines[] = {
   "147.8 pounds\n"
   "51.7 miles\n",
   "4.3 light-years\n",
   "10000 volts\n",
   "19.5 gallons\n"
};

main()
{
   FILE *tmpf;
   int i;
   _ _binfmt float amount;  // Declare a binary floating point number
   char unit[20];
   int count;

   tmpf = tmpfile();
   if (!tmpf){
         puts("Couldn't open temporary file.");
         exit(EXIT_FAILURE);
   }

   for (i = 0; i < sizeof(lines)/sizeof(char *); ++i){
      fputs(lines[i], tmpf);
   }
   rewind(tmpf);
   for(;;){

// Note: auto variables (stack allocated) are always 31-bit addressable.
//       As such the use of '~n' and the cast to a _ _near pointer are
//       not really necessary but are shown here as an example of 
//       combining two SAS/C specifiers; i.e., near(~n) and binary(~b)

      count = fscanf(tmpf, "%~n.bf %s",
                    (_ _binfmt float _ _near *)&amount, unit);
      if (feof(tmpf)) break;
      if (count < 2){
         puts("Unexpected error in input data.");
         exit(EXIT_FAILURE);
      }
         printf("amount = %~bf, units = \"%s\"\n", amount, unit);
   }
   fclose(tmpf);
}


RELATED FUNCTIONS

fprintf , scanf , sscanf


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2004 by SAS Institute Inc., Cary, NC, USA. All rights reserved.