⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib_scan.html

📁 ST20 Embedded Toolset R2.0.5用于开发基于ST20芯片机顶盒软件的开发平台,2.0.5版本,国内找不到的.在国外论坛上花了N天才找到!
💻 HTML
字号:
<HTML><HEAD><TITLE>Formatted Input</TITLE></HEAD><BODY><H1><A NAME="Formatted Input">Formatted Input</A></H1><HR><P><B><A HREF="#Scan Formats">Scan Formats</A>&#183; <A HREF="#Scan Functions">Scan Functions</A>&#183; <A HREF="#Scan Conversion Specifiers">Scan Conversion Specifiers</A></B></P><HR><P>Several library functions help you convert data values fromtext sequences that are generally readable by people toencoded internal representations. You provide a<A HREF="lib_prin.html#format string">format string</A> as the value of the<CODE>format</CODE> argument to each of these functions, hencethe term <B>formatted input</B>.The functions(declared in<A HREF="stdio.html#&lt;stdio.h&gt;"><CODE>&lt;stdio.h&gt;</CODE></A>)convert sequences of type <I>char</I> to internal representations,and help you scan such sequences that you read:<A HREF="stdio.html#fscanf">fscanf</A>,<A HREF="stdio.html#scanf">scanf</A>,and<A HREF="stdio.html#sscanf">sscanf</A>.For these function, a format string is a<A HREF="lib_over.html#multibyte string">multibyte string</A>that begins and ends in the<A HREF="charset.html#initial shift state">initial shift state</A>.</P><H2><A NAME="Scan Formats">Scan Formats</A></H2><P>A format string has the same general<A HREF="lib_prin.html#format string">syntax</A>for the scan functions as for the<A HREF="lib_prin.html#Formatted Output">print functions</A>:zero or more<A HREF="lib_prin.html#conversion specification">conversion specifications</A>,interspersed with literal text and<A HREF="lib_prin.html#white space">white space</A>.For the scan functions, however, a conversion specification is one of the<A HREF="#scan conversion specification">scan conversion specifications</A> described below.</P><P>A scan function scans the format string once from beginningto end to determine what conversions to perform. Every scanfunction accepts a<A HREF="stdarg.html#varying number of arguments">varying numberof arguments</A>, either directly or under control of an argument of type<A HREF="stdarg.html#va_list"><CODE>va_list</CODE></A>.Some scan conversion specificationsin the format string use the next argument in the list.A scan function uses each successive argument no more thanonce. Trailing arguments can be left unused.</P><P>In the description that follows, the<A HREF="lib_prin.html#integer conversions">integer conversions</A> and<A HREF="lib_prin.html#floating-point conversions">floating-point conversions</A> are the same as for the<A HREF="lib_prin.html#Formatted Output">print functions</A>.</P><H2><A NAME="Scan Functions">Scan Functions</A></H2><P>For the scan functions, literal text in a format string mustmatch the next characters to scan in the input text.<A HREF="lib_prin.html#white space">White space</A> ina format string must match the longest possible sequence of the nextzero or more white-space characters in the input. Except for the<A HREF="#Scan Conversion Specifiers">scan conversion specifier</A><A HREF="#%n"><CODE>%n</CODE></A>(which consumes no input), each<B><A NAME="scan conversion specification">scan conversion specification</A></B>determines a pattern that one or more of the next characters in theinput must match. And except for the<A HREF="#Scan Conversion Specifiers">scan conversion specifiers</A><A HREF="#%c"><CODE>c</CODE></A>,<A HREF="#%n"><CODE>n</CODE></A>, and<A HREF="#%["><CODE>[</CODE></A>,every match begins by skipping any<A HREF="lib_prin.html#%n">white space</A> characters in the input.</P><P>A scan function returns when:</P><UL><LI>it reaches the terminating null in the format string</LI><LI>it cannot obtain additional input characters to scan(<B><A NAME="input failure">input failure</A></B>)</LI><LI>a conversion fails(<B><A NAME="matching failure">matching failure</A></B>)</LI></UL><P>A scan function returns<A HREF="stdio.html#EOF"><CODE>EOF</CODE></A>if an input failure occurs before any conversion. Otherwise it returnsthe number of converted values stored. If one or more characters forma valid prefix but the conversion fails, the valid prefix is consumedbefore the scan function returns. Thus:</P><PRE>    scanf("%i", &amp;i)       <B>consumes 0X from field 0XZ</B>    scanf("%f", &amp;f)       <B>consumes 3.2E from field 3.2EZ</B></PRE><P>A scan conversion specification typically converts the matched inputcharacters to a corresponding encoded value. The next argument valuemust be the address of an object. The conversion converts the encodedrepresentation (as necessary) and stores its value in the object.A scan conversion specification has the format shown in the diagram.</P><P><IMG SRC="scan.gif"></P><P><B>Boldface</B> indicates a feature added with<A HREF="lib_over.html#C99">C99</A>.</P><P>Following the percent character (<B><CODE>%</CODE></B>)in the format string, you can write an asterisk (<B><CODE>*</CODE></B>)to indicate that the conversion should not storethe converted value in an object.</P><P>Following any <CODE>*</CODE>, you can write a nonzero<B><A NAME="scan field width">field width</A></B>that specifies the maximum number of input charactersto match for the conversion (not counting any<A HREF="lib_prin.html#white space">white space</A> that thepattern can first skip).</P><H2><A NAME="Scan Conversion Specifiers">Scan Conversion Specifiers</A></H2><P>Following any<A HREF="#scan field width">field width</A>,you must write a one-character <B>scan conversion specifier</B>,either a one-character code or a<A HREF="#scan set">scan set</A>,possibly preceded by a one-character qualifier.Each combination determines the type required of thenext argument (if any) and howthe scan functions interpret the text sequence and converts it toan encoded value.The <A HREF="lib_prin.html#integer conversions">integer</A> and<A HREF="lib_prin.html#floating-point conversions">floating-point conversions</A> also determinewhat base to assume for the text representation. (The base isthe <CODE>base</CODE> argument to the functions<A HREF="stdlib.html#strtol"><CODE>strtol</CODE></A> and<A HREF="stdlib.html#strtoul"><CODE>strtoul</CODE></A>.)The following table lists all defined combinations andtheir properties.</P><PRE><B>Conversion</B>  <B>Argument</B>          <B>Conversion</B> <B>Specifier</B>    <B>Type</B>             <B>Function</B>  <B>Base</B>   %c       char x[]   %d       int *x                 strtol     10  %hd       short *x               strtol     10  %ld       long *x                strtol     10   %e       float *x               strtod     10  %le       double *x              strtod     10  %Le       long double *x         strtod     10   %E       float *x               strtod     10  %lE       double *x              strtod     10  %LE       long double *x         strtod     10   %f       float *x               strtod     10  %lf       double *x              strtod     10  %Lf       long double *x         strtod     10   %g       float *x               strtod     10  %lg       double *x              strtod     10  %Lg       long double *x         strtod     10   %G       float *x               strtod     10  %lG       double *x              strtod     10  %LG       long double *x         strtod     10   %i       int *x                 strtol      0  %hi       short *x               strtol      0  %li       long *x                strtol      0   %n       int *x  %hn       short *x  %ln       long *x   %o       unsigned int *x        strtoul     8  %ho       unsigned short *x      strtoul     8  %lo       unsigned long *x       strtoul     8   %p       void **x   %s       char x[]   %u       unsigned int *x        strtoul    10  %hu       unsigned short *x      strtoul    10  %lu       unsigned long *x       strtoul    10   %x       unsigned int *x        strtoul    16  %hx       unsigned short *x      strtoul    16  %lx       unsigned long *x       strtoul    16   %X       unsigned int *x        strtoul    16  %hX       unsigned short *x      strtoul    16  %lX       unsigned long *x       strtoul    16 %[...]     char x[]   %%       <B>none</B></PRE><P>The scan conversion specifier (or<A HREF="#scan set">scan set</A>) determines any behaviornot summarized in this table. In the following descriptions,examples follow each of the scan conversion specifiers.In each example, the function<A HREF="stdio.html#sscanf"><CODE>sscanf</CODE></A>matches the <B><CODE>bold</CODE></B> characters.</P><P>You write <B><A NAME="%c"><CODE>%c</CODE></A></B>to store the matched input characters inan array object. If you specify no field width <I>w,</I> then <I>w</I>has the value one. The match does not skip leading<A HREF="lib_prin.html#white space">white space</A>. Anysequence of <I>w</I> characters matches the conversion pattern.</P><PRE>sscanf("<B>1</B>29E-2", "%c", &amp;c)             <B>stores '1'</B>sscanf("<B>12</B>9E-2", "%2c", &amp;c[0])         <B>stores '1', '2'</B></PRE><P>You write <B><A NAME="%d"><CODE>%d</CODE></A></B>,<B><A NAME="%i"><CODE>%i</CODE></A></B>,<B><A NAME="%o"><CODE>%o</CODE></A></B>,<B><A NAME="%u"><CODE>%u</CODE></A></B>,<B><A NAME="%x"><CODE>%x</CODE></A></B>, or<B><A NAME="%X"><CODE>%X</CODE></A></B> to convertthe matched input characters as a signed integerand store the result in an integer object.</P><PRE>sscanf("<B>129E</B>-2", "%o%d%x", &amp;i, &amp;j, &amp;k) <B>stores 10, 9, 14</B></PRE><P>You write<B><A NAME="%e"><CODE>%e</CODE></A></B>,<B><A NAME="%E"><CODE>%E</CODE></A></B>,<B><A NAME="%f"><CODE>%f</CODE></A></B>,<B><A NAME="%g"><CODE>%g</CODE></A></B>, or<B><A NAME="%G"><CODE>%G</CODE></A></B>to convert the matched input characters as a signed fraction, withan optional exponent, and store the result in a floating-point object.</P><PRE>sscanf("<B>129E-2</B>", "%e", &amp;f)             <B>stores 1.29</B></PRE><P>You write <B><A NAME="%n"><CODE>%n</CODE></A></B>to store the number of charactersmatched (up to this point in the format) in an integer object. Thematch does not skip leading<A HREF="lib_prin.html#white space">white space</A>and does not match any input characters.</P><PRE>sscanf("<B>12</B>9E-2", "12%n", &amp;i)           <B>stores 2</B></PRE><P>You write <B><A NAME="%p"><CODE>%p</CODE></A></B>to convert the matched input characters asan external representation of a <I>pointer to void</I> and store theresult in an object of type <I>pointer to void.</I> The input charactersmust match the form generated by the<A HREF="lib_prin.html#%p"><CODE>%p</CODE></A><A HREF="lib_prin.html#print conversion specification">print conversion specification</A>.</P><PRE>sscanf("<B>129E</B>-2", "%p", &amp;p)             <B>stores, e.g. 0x129E</B></PRE><P>You write <B><A NAME="%s"><CODE>%s</CODE></A></B>to store the matched input characters inan array object, followed by a terminating null character. If youdo not specify a field width <I>w,</I> then <I>w</I> has a large value.Any sequence of up to <I>w</I> non white-space characters matchesthe conversion pattern.</P><PRE>sscanf("<B>129E-2</B>", "%s", &amp;s[0])          <B>stores "129E-2"</B></PRE><P>You write <B><A NAME="%["><CODE>%[</CODE></A></B>to store the matched input characters inan array object, followed by a terminating null character. If youdo not specify a field width <I>w,</I> then <I>w</I> has a large value.The match does not skip leading<A HREF="lib_prin.html#white space">white space</A>.A sequence of up to <I>w</I>characters matches the conversion pattern in the<B><A NAME="scan set">scan set</A></B> that follows.To complete the scan set, you follow the left bracket(<CODE>[</CODE>) in the conversion specification with a sequenceof zero or more <B>match</B> characters, terminated by a right bracket(<B><CODE>]</CODE></B>).</P><P>If you do not write a caret (<B><CODE>^</CODE></B>)immediately after the <CODE>[</CODE>, then eachinput character must match <I>one</I> of the matchcharacters. Otherwise, each input character must not match <I>any</I>of the match characters, which begin with the character followingthe <CODE>^</CODE>. If you write a <B><CODE>]</CODE></B>immediately after the <CODE>[</CODE> or <CODE>[^</CODE>,then the <CODE>]</CODE> is the first match character, notthe terminating <CODE>]</CODE>. If you write a minus(<B><CODE>-</CODE></B>) as other than the first or last match character,an implementation can give it special meaning.It usually indicates a range of characters, in conjunction with thecharacters immediately preceding or following, as in<CODE>0-9</CODE> for all the digits.)You cannot specify a null match character.</P><PRE>sscanf("<B>12</B>9E-2", "%[54321]", &amp;s[0])     <B>stores "12"</B></PRE><P>You write <B><A NAME="%%"><CODE>%%</CODE></A></B>to match the percent character (<CODE>%</CODE>).The function does not store a value.</P><PRE>sscanf("<B>%  0XA</B>", "%% %i", &amp;i)          <B>stores 10</B></PRE><HR><P>See also the<B><A HREF="index.html#Table of Contents">Table of Contents</A></B> and the<B><A HREF="_index.html">Index</A></B>.</P><P><I><A HREF="crit_pb.html">Copyright</A> &#169; 1989-2002by P.J. Plauger and Jim Brodie. All rights reserved.</I></P><!--V4.01:1125--></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -