[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


fscanf

Read formatted text from a stream.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <stdio.h>
int fscanf(FILE *stream, 
   const char *format, ...);
Parameters:

Parameters for this facility are:

stream  
FILE *  
A pointer to a FILE stream  
format  
const char *  
A format string  

Remarks:

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. Refer to the scanf() function description.

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.


NOTE

On embedded/ RTOS systems this function only is implemented for stdin, stdout and stderr files.

Input Control String and Conversion Specifiers

The format argument is a character array containing normal text, white space (space, tab, newline), and conversion specifications. The normal text specifies literal characters that must be matched in the input stream. A white space character indicates that white space characters are skipped until a non-white space character is reached. The conversion specifications indicate what characters in the input stream are to be converted and stored.

The conversion specifications must have matching arguments in the order they appear in format. Because scanf() stores data in memory, the matching conversion specification arguments must be pointers to objects of the relevant types.

A conversion specification consists of the percent sign (%) prefix, followed by an optional maximum width or assignment suppression, and ending with a conversion type. A percent sign can be skipped by doubling it in format; %% signifies a single % in the input stream.

An optional width is a decimal number specifying the maximum width of an input field. scanf() will not read more characters for a conversion than is specified by the width.

An optional assignment suppression character (*) can be used to skip an item by reading it but not assigning it. A conversion specification with assignment suppression must not have a corresponding argument.

The last character, the conversion type, specifies the kind of conversion requested. "Length Modifiers And Conversion Specifiers For Formatted Input Functions," describes the conversion type characters.


MSL AltiVec Extensions for Scanf

The AltiVec extensions to the standard scanf family of functions is supported in Metrowerks Standard Libraries.

Separator arguments after % and before any specifier may be any character or may be the @ symbol. The @ symbol is a non-Motorola extension that will use a specified string as a specifier.

In the specific case of a 'c' specifier any char may be used as a sepaator for all other specifiers '-', '+', '#', ' ' may not be used.

The listing "Example of AltiVec Scanf Extensions" demonstrates their use.

Length Modifiers And Conversion Specifiers For Formatted Input Functions:

Modifier
Description

Length Specifiers
hh  

The hh flag indicates that the following d, i, o, u, x, X or n conversion specifier applies to an argument that is of type char or unsigned char.

h  
The h flag indicates that the following d, i, o, u, x, X or n conversion specifier applies to an argument that is of type short int or unsigned short int.  
l  
When used with integer conversion specifier, the l flag indicates long int or an unsigned long int type. When used with floating point conversion specifier, the l flag indicates a double.   When used with a c or s conversion specifier, the l flag indicates that the corresponding argument with type pointer to wchar_t.  
ll  
When used with integer conversion specifier, the ll flag indicates that the corresponding argument is of type long long or an unsigned long long.  
L  
The L flag indicates that the corresponding float conversion specifier corresponds to an argument of type long double.  
v  
AltiVec: A vector bool char, vector signed char or vector unsigned char when followed by c, d, i, o, u, x or X   A vector float, when followed by f..  
vh   hv  
AltiVec: vector short, vector unisgned short, vector bool short or vector pixel when followed by c, d, i, o, u, x or X  
vl   lv  
AltiVec: vector long, vector unsigned long or vector bool when followed by c, d, i, o, u, x or X  

Conversion Sepcifiers
d  
A decimal integer is read.  
i  
A decimal, octal, or hexadecimal integer is read. The integer can be prefixed with a plus or minus sign (+, -), 0 for octal numbers, 0x or 0X for hexadecimal numbers.  
o  
An octal integer is read.  
u  
An unsigned decimal integer is read.  
x, X  
A hexadecimal integer is read.  
e, E, f, g, G  
A floating point number is read. The number can be in plain decimal format (e.g. 3456.483) or in scientific notation   ([-]b.aaadd).  
s  
A character string is read. The input character string is considered terminated when a white space character is reached or the maximum width has been reached. The null character is appended to the end of the array.  
c  
A character is read. White space characters are not skipped, but read using this conversion specifier..  
p  
A pointer address is read. The input format should be the same as that output by the p conversion type in printf().  
n  
This conversion type does not read from the input stream but stores the number of characters read in its corresponding argument.  
[scanset]  
Input stream characters are read and filtered determined by the scanset. See "Scanset," for a full description.  


Scanset

The conversion specifier %[ allows you to specify a scanset, which is a sequence of characters that will be read and stored in the string pointed to by the scanset's corresponding argument. The characters between the [ and the terminating ] define the scanset. A null character is appended to the end of the character sequence.

Input stream characters are read until a character is found that is not in the scanset. If the first character of scanset is a circumflex (^) then input stream characters are read until a character from the scanset is read. A null character is appended to the end of the character array.

Thus, the conversion specifier %[abcdef] specifies that the scanset is abcdef and any of the characters `a' through `f' are to be accepted and stored. As soon as any character outside this set is encountered, reading and storing will cease. Thus, for example, assuming we have the declaration:


   char str[20];

the execution of


   sscanf("acdfxbe", "%[abcdef]", str);

will store acdf in str; the `x' and following characters will not be stored because the `x' is not in the scanset.

If the first character of the scanset is the circumflex, ^, then the following characters will define a set of characters such that encountering any one of them will cause reading and storing to stop; any character outside a scanset defined in this way will be accepted, we will call this an exclusionary scanset. Thus execution of


   sscanf("stuvawxyz", "%[^abcdef]", str);

will store stuv in str. If you want ^ to be part of the scanset, you cannot list it as the first character otherwise it will be interpreted as introducing the members of an exclusionary scanset. Thus %[^abc] defines the exclusionary scanset abc whereas %[a^bc] defines the scanset abc^. %[^a^bc] defines the exclusionary scanset abc^, as does %[^^abc].

If you want ] to be in the scanset, it must be the first character of the scanset, immediately following the %[ or, to be in an exclusionary scanset, immediately after the ^, for example, %[]abc] or %[^]abc]. In any other position, the ] will be interpreted as terminating the scanset.

To include the - character in the scanset, it must be either listed first (possibly after an initial ^ or last, thus for example, %[-abc], %[abc-], %[^-abc], or %[^abc-]. The C Standard explicitly states:

If a - character is in the scanlist and is not the first, nor the second where the first character is a ^, nor the last character, the behavior is implementation-defined.

MSL interprets such a use of - in a scanlist as defining a range of characters; thus, the specification %[a-z] as being the equivalent of %[abcdefghijklmnopqrstuvwxyz]. You should bear in mind that this is MSL's interpretation and such usage may be interpreted differently in other C library implementations. Note also that it is assumed that the numeric value of the character before the - is less than that of the one after. If this relationship does not hold undefined and probably unwanted effects may be experienced.

Return:

fscanf() returns the number of items read. If there is an error in reading data that is inconsistent with the format string, fscanf() sets errno to a nonzero value. fscanf() returns EOF if it reaches the end-of-file.

See Also:

"Wide Character and Byte Character Stream Orientation"

"errno"

"scanf"

Example of fscanf() usage.:
#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.


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000