[Linux manuál]
Návod, kniha: POSIX Programmer's Manual
int fscanf(FILE *restrict stream, const char
*restrict format, ... );
int scanf(const char *restrict format, ... );
int sscanf(const char *restrict s, const char
*restrict format, ... );
with the input line:
assigns to n the value 3, to i the value 25, to x the value
5.432, and name contains the string "Hamster" .
The call:
with input:
assigns 56 to i, 789.0 to x, skips 0123, and places the string
"56\0" in name. The next call to getchar()
shall return the character 'a' .
fscanf, scanf, sscanf: převést formátovaný vstup
Originální popis anglicky: fscanf, scanf, sscanf - convert formatted inputNávod, kniha: POSIX Programmer's Manual
STRUČNĚ
#include <stdio.h>POPIS / INSTRUKCE
The fscanf() function shall read from the named input stream. The scanf() function shall read from the standard input stream stdin. The sscanf() function shall read from the string s. Each function reads bytes, interprets them according to a format, and stores the results in its arguments. Each expects, as arguments, a control string format described below, and a set of pointer arguments indicating where the converted input should be stored. The result is undefined if there are insufficient arguments for the format. If the format is exhausted while arguments remain, the excess arguments shall be evaluated but otherwise ignored. Conversions can be applied to the nth argument after the format in the argument list, rather than to the next unused argument. In this case, the conversion specifier character % (see below) is replaced by the sequence "%n$", where n is a decimal integer in the range [1,{NL_ARGMAX}]. This feature provides for the definition of format strings that select arguments in an order appropriate to specific languages. In format strings containing the "%n$" form of conversion specifications, it is unspecified whether numbered arguments in the argument list can be referenced from the format string more than once. The format can contain either form of a conversion specification-that is, % or "%n$"-but the two forms cannot be mixed within a single format string. The only exception to this is that %% or %* can be mixed with the "%n$" form. When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the ( N-1)th, are pointers. The fscanf() function in all its forms shall allow detection of a language-dependent radix character in the input string. The radix character is defined in the program's locale (category LC_NUMERIC ). In the POSIX locale, or in a locale where the radix character is not defined, the radix character shall default to a period ( '.' ). The format is a character string, beginning and ending in its initial shift state, if any, composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters ( <space>s, <tab>s, <newline>s, <vertical-tab>s, or <form-feed>s); an ordinary character (neither '%' nor a white-space character); or a conversion specification. Each conversion specification is introduced by the character '%' or the character sequence "%n$", after which the following appear in sequence:- *
- An optional assignment-suppressing character '*' .
- *
- An optional non-zero decimal integer that specifies the maximum field width.
- *
- An option length modifier that specifies the size of the receiving object.
- *
- A conversion specifier character that specifies the type of conversion to be applied. The valid conversion specifiers are described below.
- hh
- Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to signed char or unsigned char.
- h
- Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to short or unsigned short.
- l (ell)
- Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to long or unsigned long; that a following a , A , e , E , f , F , g , or G conversion specifier applies to an argument with type pointer to double; or that a following c , s , or [ conversion specifier applies to an argument with type pointer to wchar_t.
- ll (ell-ell)
-
Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to long long or unsigned long long.
- j
- Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to intmax_t or uintmax_t.
- z
- Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to size_t or the corresponding signed integer type.
- t
- Specifies that a following d , i , o , u , x , X , or n conversion specifier applies to an argument with type pointer to ptrdiff_t or the corresponding unsigned type.
- L
- Specifies that a following a , A , e ,
E , f , F , g , or G conversion
specifier applies to an argument with type pointer to long double.
- d
- Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of strtol() with the value 10 for the base argument. In the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer to int.
- i
- Matches an optionally signed integer, whose format is the same as expected for the subject sequence of strtol() with 0 for the base argument. In the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer to int.
- o
- Matches an optionally signed octal integer, whose format is the same as expected for the subject sequence of strtoul() with the value 8 for the base argument. In the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer to unsigned.
- u
- Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of strtoul() with the value 10 for the base argument. In the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer to unsigned.
- x
- Matches an optionally signed hexadecimal integer, whose format is the same as expected for the subject sequence of strtoul() with the value 16 for the base argument. In the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer to unsigned.
- a, e, f, g
-
Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected for the subject sequence of strtod(). In the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer to float.
- s
- Matches a sequence of bytes that are not white-space characters. The application shall ensure that the corresponding argument is a pointer to the initial byte of an array of char, signed char, or unsigned char large enough to accept the sequence and a terminating null character code, which shall be added automatically.
- [
- Matches a non-empty sequence of bytes from a set of expected bytes (the scanset). The normal skip over white-space characters shall be suppressed in this case. The application shall ensure that the corresponding argument is a pointer to the initial byte of an array of char, signed char, or unsigned char large enough to accept the sequence and a terminating null byte, which shall be added automatically.
- c
- Matches a sequence of bytes of the number specified by the field width (1 if no field width is present in the conversion specification). The application shall ensure that the corresponding argument is a pointer to the initial byte of an array of char, signed char, or unsigned char large enough to accept the sequence. No null byte is added. The normal skip over white-space characters shall be suppressed in this case.
- p
- Matches an implementation-defined set of sequences, which shall be the same as the set of sequences that is produced by the %p conversion specification of the corresponding fprintf() functions. The application shall ensure that the corresponding argument is a pointer to a pointer to void. The interpretation of the input item is implementation-defined. If the input item is a value converted earlier during the same program execution, the pointer that results shall compare equal to that value; otherwise, the behavior of the %p conversion specification is undefined.
- n
- No input is consumed. The application shall ensure that the corresponding argument is a pointer to the integer into which shall be written the number of bytes read from the input so far by this call to the fscanf() functions. Execution of a %n conversion specification shall not increment the assignment count returned at the completion of execution of the function. No argument shall be converted, but one shall be consumed. If the conversion specification includes an assignment-suppressing character or a field width, the behavior is undefined.
- C
- Equivalent to lc .
- S
- Equivalent to ls .
- %
- Matches a single '%' character; no conversion or
assignment occurs. The complete conversion specification shall be
%% .
NÁVRATOVÁ HODNOTA
Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error.CHYBY / ERRORY
For the conditions under which the fscanf() functions fail and may fail, refer to fgetc() or fgetwc() . In addition, fscanf() may fail if:- EILSEQ
- Input byte sequence does not form a valid character.
- EINVAL
- There are insufficient arguments.
PŘÍKLADY POUŽITÍ
The call:
int i, n; float x; char name[50];
n = scanf("%d%f%s", &i, &x, name);
25 54.32E-1 Hamster
int i; float x; char name[50];
(void) scanf("%2d%f%*d %[0123456789]", &i, &x, name);
56789 0123 56a72
Reading Data into an Array
The following call uses fscanf() to read three floating-point numbers from standard input into the input array.float input[3]; fscanf (stdin, "%f %f %f", input, input+1, input+2);
APPLICATION USAGE
If the application calling fscanf() has any objects of type wint_t or wchar_t, it must also include the <wchar.h> header to have these objects defined.RATIONALE
This function is aligned with the ISO/IEC 9899:1999 standard, and in doing so a few "obvious" things were not included. Specifically, the set of characters allowed in a scanset is limited to single-byte characters. In other similar places, multi-byte characters have been permitted, but for alignment with the ISO/IEC 9899:1999 standard, it has not been done here. Applications needing this could use the corresponding wide-character functions to achieve the desired results.FUTURE DIRECTIONS
None.SOUVISEJÍCÍ
getc() , printf() , setlocale() , strtod() , strtol() , strtoul() , wcrtomb() , the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 7, Locale, <langinfo.h>, <stdio.h>, <wchar.h>COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .| 2003 | IEEE/The Open Group |