📄 lib_scan.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>· <A HREF="#Scan Functions">Scan Functions</A>· <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 fall into two categories:</P><P>The <B><A NAME="byte scan functions">byte scan functions</A></B>(declared in<A HREF="stdio.html#<stdio.h>"><CODE><stdio.h></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><P>The <B><A NAME="wide scan functions">wide scan functions</A></B>(declared in<A HREF="wchar.html#<wchar.h>"><CODE><wchar.h></CODE></A>and hence added with<B><A HREF="lib_over.html#Amendment 1">Amendment 1</A></B>)convert sequences of type<A HREF="stddef.html#wchar_t"><CODE>wchar_t</CODE></A>,to internal representations,and help you scan such sequences that you read:<A HREF="wchar.html#fwscanf">fwscanf</A>,<A HREF="wchar.html#wscanf">wscanf</A>,and<A HREF="wchar.html#swscanf">swscanf</A>.For these functions, a format string is a<A HREF="lib_over.html#wide-character string">wide-character string</A>.In the descriptions that follow, a wide character <CODE>wc</CODE>from a format string or a stream is compared to a specific (byte)character <CODE>c</CODE> as if by evaluating the expression<CODE><A HREF="wchar.html#wctob">wctob</A>(wc) == c</CODE>.</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", &i) <B>consumes 0X from field 0XZ</B> scanf("%f", &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[] %lc wchar_t 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[] %ls wchar_t x[]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -