📄 fscanf.html
字号:
If an l (ell) qualifier is present, the input is a sequence ofcharacters that begins in the initial shift state. Eachcharacter is converted to a wide-character as if by a callto the<i><a href="mbrtowc.html">mbrtowc()</a></i>function, with the conversion state described by an<b>mbstate_t</b>object initialised to zero before the first character isconverted. The corresponding argument must be a pointer to an arrayof<b>wchar_t</b>large enough to accept the sequence and the terminating null wide-character, which will be added automatically.<dt>[<dd>Matches a non-empty sequence of bytes from a set of expectedbytes (the<i>scanset</i>).The normal skip over white-space characters is suppressed in this case.The corresponding argument must be a pointer to the initialbyte of an array of<b>char</b>,<b>signed char</b>or<b>unsigned char</b>large enough to accept the sequence and aterminating null byte, which will be added automatically.If an l (ell) qualifier is present, the input is a sequence ofcharacters that begins in the initial shift state. Eachcharacter in the sequence is converted to a wide-characteras if by a call to the<i><a href="mbrtowc.html">mbrtowc()</a></i>function, with the conversion state described by an<b>mbstate_t</b>object initialised to zero before the first character isconverted. The corresponding argument must be a pointer to an arrayof<b>wchar_t</b>large enough to accept the sequence and the terminating nullwide-character, which will be added automatically.The conversion specification includes all subsequent bytes inthe<i>format</i>string up to and including the matching right square bracket (]).The bytes between the square brackets (the<i>scanlist</i>)comprise the scanset, unless the byte after the left square bracketis a circumflex (^), in which case the scanset contains allbytes that do not appear in the scanlist between thecircumflex and the right square bracket.If the conversion specification begins with []or [^], the right square bracket is included in the scanlist and thenext right square bracket is the matching right square bracket that ends theconversion specification; otherwise the first right square bracket is theone that ends the conversion specification.If a - is in the scanlist and is not the first character, nor the secondwhere the first character is a ^, nor the last character,the behaviour is implementation-dependent.<dt>c<dd>Matches a sequence of bytes of the number specified by thefield width (1 if no field width is present in the conversion specification).The corresponding argument must be a pointer to the initialbyte of an array of <b>char</b>, <b>signed char</b> or <b>unsigned char</b>large enough to accept the sequence.No null byte is added.The normal skip over white-space characters is suppressed in this case.If an l (ell) qualifier is present, the input is a sequence ofcharacters that begins in the initial shift state. Eachcharacter in the sequence is converted to a wide-characteras if by a call to the<i><a href="mbrtowc.html">mbrtowc()</a></i>function, with the conversion state described by an<b>mbstate_t</b>object initialised to zero before the first character isconverted. The corresponding argument must be a pointer to an arrayof<b>wchar_t</b>large enough to accept the resulting sequence of wide-characters. Nonull wide-character is added.<dt>p<dd>Matches an implementation-dependent set of sequences, which mustbe the same as the set of sequences that is produced by the %pconversion of the corresponding<i><a href="fprintf.html">fprintf()</a></i>functions.The corresponding argument must be a pointer to a pointer to<b>void</b>.The interpretation of the input item is implementation-dependent.If the input item is a value converted earlier during thesame program execution, the pointer that results will compare equalto that value; otherwise the behaviour of the %p conversion is undefined.<dt>n<dd>No input is consumed.The corresponding argument must be a pointer tothe integer into which is to be written the number ofbytes read from the input so far by this call to the<i>fscanf()</i>functions.Execution of a %nconversion specificationdoes not increment the assignment count returned at thecompletion of execution of the function.<dt>C<dd>Same as lc.<dt>S<dd>Same as ls.<dt>%<dd>Matches a single %;no conversion or assignment occurs.The complete conversion specification must be %%.</dl><p>If a conversion specification is invalid, the behaviour isundefined.<p>The conversion characters E, G and Xare also valid and behave the same as, respectively, e, g and x.<p>If end-of-file is encountered during input, conversion is terminated.If end-of-file occurs before any bytes matching the currentconversion specification (except for%n) have been read(other than leading white-space characters, wherepermitted), execution of the current conversion specificationterminates with aninput failure. Otherwise, unless execution of the currentconversion specification is terminated with a matching failure,execution of the following conversion specification (if any)is terminated with an input failure.<p>Reaching the end of the string in<i>sscanf()</i>is equivalent to encountering end-of-file for<i>fscanf()</i>.<p>If conversion terminates on a conflicting input, theoffending input is left unread in the input.Any trailing white space (including newline characters) is leftunread unless matched by a conversion specification.The success of literal matches and suppressed assignments is onlydirectly determinable via the %n conversion specification.<p>The<i>fscanf()</i>and<i>scanf()</i>functions may mark the<i>st_atime</i>field of the file associated with<i>stream</i>for update.The<i>st_atime</i>field will be marked for update by the first successful execution of<i><a href="fgetc.html">fgetc()</a></i>,<i><a href="fgets.html">fgets()</a></i>,<i><a href="fread.html">fread()</a></i>,<i><a href="getc.html">getc()</a></i>,<i><a href="getchar.html">getchar()</a></i>,<i><a href="gets.html">gets()</a></i>,<i>fscanf()</i>or<i>fscanf()</i>using<i>stream</i>that returns data not supplied by a prior call to<i><a href="ungetc.html">ungetc()</a></i>.</blockquote><h4><a name = "tag_000_005_269"> </a>RETURN VALUE</h4><blockquote>Upon successful completion, these functions return the number of successfullymatched and assigned input items; this number can be 0 in the event of anearly matching failure. If the input ends before the first matching failureor conversion, EOF is returned. If a read error occurs the error indicatorfor the stream is set, EOF is returned, and<i>errno</i>is set to indicate the error.</blockquote><h4><a name = "tag_000_005_270"> </a>ERRORS</h4><blockquote>For the conditions under which the<i>fscanf()</i>functions will fail and may fail, refer to<i><a href="fgetc.html">fgetc()</a></i>or<i><a href="fgetwc.html">fgetwc()</a></i>.<p>In addition,<i>fscanf()</i>may fail if:<dl compact><dt>[EILSEQ]<dd>Input byte sequence does not form a valid character.<dt>[EINVAL]<dd>There are insufficient arguments.</dl></blockquote><h4><a name = "tag_000_005_271"> </a>EXAMPLES</h4><blockquote>The call:<pre><code>int i, n; float x; char name[50];n = scanf("%d%f%s", &i, &x, name);</code></pre>with the input line:<pre><code>25 54.32E-1 Hamster</code></pre>will assign to<i>n</i>the value 3, to<i>i</i>the value 25, to<i>x</i>the value 5.432, and<i>name</i>will contain the string Hamster.<p>The call:<pre><code>int i; float x; char name[50];(void) scanf("%2d%f%*d %[0123456789]", &i, &x, name);</code></pre>with input:<pre><code>56789 0123 56a72</code></pre><p>will assign 56 to<i>i</i>,789.0 to<i>x</i>,skip 0123, and place the string 56\0 in<i>name</i>.The next call to<i><a href="getchar.html">getchar()</a></i>will return the character a.</blockquote><h4><a name = "tag_000_005_272"> </a>APPLICATION USAGE</h4><blockquote>If the application calling<i>fscanf()</i>has any objects of type<b>wint_t</b>or<b>wchar_t,</b>it must also include the header<i><a href="wchar.h.html"><wchar.h></a></i>to have these objects defined.</blockquote><h4><a name = "tag_000_005_273"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_005_274"> </a>SEE ALSO</h4><blockquote><i><a href="getc.html">getc()</a></i>,<i><a href="printf.html">printf()</a></i>,<i><a href="setlocale.html">setlocale()</a></i>,<i><a href="strtod.html">strtod()</a></i>,<i><a href="strtol.html">strtol()</a></i>,<i><a href="strtoul.html">strtoul()</a></i>,<i><a href="wcrtomb.html">wcrtomb()</a></i>,<i><a href="langinfo.h.html"><langinfo.h></a></i>,<i><a href="stdio.h.html"><stdio.h></a></i>,<i><a href="wchar.h.html"><wchar.h></a></i>,the <b>XBD</b> specification, <a href="../xbd/locale.html"><b>Locale</b> </a>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 1 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -