Стандарт языка Си С99 TC (1113411), страница 54
Текст из файла (страница 54)
Theresulting field is nevertheless a sequence of multibyte characters that begins in the initial shift state.§7.19.6.2Library285ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256characters that begins in the initial shift state. Each multibyte character isconverted to a wide character as if by a call to the mbrtowc function, withthe conversion state described by an mbstate_t object initialized to zerobefore the first multibyte character is converted. The corresponding argumentshall be a pointer to the initial element of an array of wchar_t large enoughto accept the sequence and the terminating null wide character, which will beadded automatically.[Matches a nonempty sequence of characters from a set of expected characters(the scanset).252)If no l length modifier is present, the corresponding argument shall be apointer to the initial element of a character array large enough to accept thesequence and a terminating null character, which will be added automatically.If an l length modifier is present, the input shall be a sequence of multibytecharacters that begins in the initial shift state.
Each multibyte character isconverted to a wide character as if by a call to the mbrtowc function, withthe conversion state described by an mbstate_t object initialized to zerobefore the first multibyte character is converted. The corresponding argumentshall be a pointer to the initial element of an array of wchar_t large enoughto accept the sequence and the terminating null wide character, which will beadded automatically.The conversion specifier includes all subsequent characters in the formatstring, up to and including the matching right bracket (]). The charactersbetween the brackets (the scanlist) compose the scanset, unless the characterafter the left bracket is a circumflex (^), in which case the scanset contains allcharacters that do not appear in the scanlist between the circumflex and theright bracket.
If the conversion specifier begins with [] or [^], the rightbracket character is in the scanlist and the next following right bracketcharacter is the matching right bracket that ends the specification; otherwisethe first following right bracket character is the one that ends thespecification. If a - character is in the scanlist and is not the first, nor thesecond where the first character is a ^, nor the last character, the behavior isimplementation-defined.p286Matches an implementation-defined set of sequences, which should be thesame as the set of sequences that may be produced by the %p conversion ofthe fprintf function.
The corresponding argument shall be a pointer to apointer to void. The input item is converted to a pointer value in animplementation-defined manner. If the input item is a value converted earlierduring the same program execution, the pointer that results shall compareequal to that value; otherwise the behavior of the %p conversion is undefined.Library§7.19.6.2WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3nNo input is consumed. The corresponding argument shall be a pointer tosigned integer into which is to be written the number of characters read fromthe input stream so far by this call to the fscanf function. Execution of a%n directive does not increment the assignment count returned at thecompletion of execution of the fscanf function.
No argument is converted,but one is consumed. If the conversion specification includes an assignmentsuppressing character or a field width, the behavior is undefined.%Matches a single % character; no conversion or assignment occurs. Thecomplete conversion specification shall be %%.13If a conversion specification is invalid, the behavior is undefined.253)14The conversion specifiers A, E, F, G, and X are also valid and behave the same as,respectively, a, e, f, g, and x.15Trailing white space (including new-line characters) is left unread unless matched by adirective. The success of literal matches and suppressed assignments is not directlydeterminable other than via the %n directive.Returns16The fscanf function returns the value of the macro EOF if an input failure occursbefore any conversion.
Otherwise, the function returns the number of input itemsassigned, which can be fewer than provided for, or even zero, in the event of an earlymatching failure.17EXAMPLE 1The call:#include <stdio.h>/* ... */int n, i; float x; char name[50];n = fscanf(stdin, "%d%f%s", &i, &x, name);with the input line:25 54.32E-1 thompsonwill assign to n the value 3, to i the value 25, to x the value 5.432, and to name the sequencethompson\0.18EXAMPLE 2The call:#include <stdio.h>/* ... */int i; float x; char name[50];fscanf(stdin, "%2d%f%*d %[0123456789]", &i, &x, name);with input:253) See ‘‘future library directions’’ (7.26.9).§7.19.6.2Library287ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N125656789 0123 56a72will assign to i the value 56 and to x the value 789.0, will skip 0123, and will assign to name thesequence 56\0.
The next character read from the input stream will be a.19EXAMPLE 3To accept repeatedly from stdin a quantity, a unit of measure, and an item name:#include <stdio.h>/* ... */int count; float quant; char units[21], item[21];do {count = fscanf(stdin, "%f%20s of %20s", &quant, units, item);fscanf(stdin,"%*[^\n]");} while (!feof(stdin) && !ferror(stdin));20If the stdin stream contains the following lines:2 quarts of oil-12.8degrees Celsiuslots of luck10.0LBSofdirt100ergs of energythe execution of the above example will be analogous to the following assignments:quantcountquantcountcountquantcountcountcount21EXAMPLE 4=========2; strcpy(units, "quarts"); strcpy(item, "oil");3;-12.8; strcpy(units, "degrees");2; // "C" fails to match "o"0; // "l" fails to match "%f"10.0; strcpy(units, "LBS"); strcpy(item, "dirt");3;0; // "100e" fails to match "%f"EOF;In:#include <stdio.h>/* ...
*/int d1, d2, n1, n2, i;i = sscanf("123", "%d%n%n%d", &d1, &n1, &n2, &d2);the value 123 is assigned to d1 and the value 3 to n1. Because %n can never get an input failure the valueof 3 is also assigned to n2. The value of d2 is not affected. The value 1 is assigned to i.22EXAMPLE 5 In these examples, multibyte characters do have a state-dependent encoding, and themembers of the extended character set that consist of more than one byte each consist of exactly two bytes,the first of which is denoted here by a and the second by an uppercase letter, but are only recognized assuch when in the alternate shift state.
The shift sequences are denoted by ↑ and ↓, in which the first causesentry into the alternate shift state.23After the call:288Library§7.19.6.2WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3#include <stdio.h>/* ... */char str[50];fscanf(stdin, "a%s", str);with the input line:a↑ X Y↓ bcstr will contain ↑ X Y↓\0 assuming that none of the bytes of the shift sequences (or of the multibytecharacters, in the more general case) appears to be a single-byte white-space character.24In contrast, after the call:#include <stdio.h>#include <stddef.h>/* ... */wchar_t wstr[50];fscanf(stdin, "a%ls", wstr);with the same input line, wstr will contain the two wide characters that correspond to X and Y and aterminating null wide character.25However, the call:#include <stdio.h>#include <stddef.h>/* ... */wchar_t wstr[50];fscanf(stdin, "a↑ X↓%ls", wstr);with the same input line will return zero due to a matching failure against the ↓ sequence in the formatstring.26Assuming that the first byte of the multibyte character X is the same as the first byte of the multibytecharacter Y, after the call:#include <stdio.h>#include <stddef.h>/* ...
*/wchar_t wstr[50];fscanf(stdin, "a↑ Y↓%ls", wstr);with the same input line, zero will again be returned, but stdin will be left with a partially consumedmultibyte character.Forward references: the strtod, strtof, and strtold functions (7.20.1.3), thestrtol, strtoll, strtoul, and strtoull functions (7.20.1.4), conversion state(7.24.6), the wcrtomb function (7.24.6.3.3).§7.19.6.2Library289ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N12567.19.6.3 The printf functionSynopsis1#include <stdio.h>int printf(const char * restrict format, ...);Description2The printf function is equivalent to fprintf with the argument stdout interposedbefore the arguments to printf.Returns3The printf function returns the number of characters transmitted, or a negative value ifan output or encoding error occurred.7.19.6.4 The scanf functionSynopsis1#include <stdio.h>int scanf(const char * restrict format, ...);Description2The scanf function is equivalent to fscanf with the argument stdin interposedbefore the arguments to scanf.Returns3The scanf function returns the value of the macro EOF if an input failure occurs beforeany conversion.
Otherwise, the scanf function returns the number of input itemsassigned, which can be fewer than provided for, or even zero, in the event of an earlymatching failure.7.19.6.5 The snprintf functionSynopsis1#include <stdio.h>int snprintf(char * restrict s, size_t n,const char * restrict format, ...);Description2The snprintf function is equivalent to fprintf, except that the output is written intoan array (specified by argument s) rather than to a stream. If n is zero, nothing is written,and s may be a null pointer.
Otherwise, output characters beyond the n-1st arediscarded rather than being written to the array, and a null character is written at the endof the characters actually written into the array. If copying takes place between objectsthat overlap, the behavior is undefined.290Library§7.19.6.5WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3Returns3The snprintf function returns the number of characters that would have been writtenhad n been sufficiently large, not counting the terminating null character, or a negativevalue if an encoding error occurred.