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

📄 scanf.3s

📁 <B>Digital的Unix操作系统VAX 4.2源码</B>
💻 3S
字号:
.\" SCCSID: @(#)scanf.3s	2.5	8/12/87.TH scanf 3s.SH Namescanf, fscanf, sscanf \- convert formatted input.SH Syntax.B #include <stdio.h>.PP.B int scanf(.IR format [, .I pointer ] ....B ).br.B char .IR *format ;.PP.B int fscanf(.IR stream ,.I format[,.I pointer] ....B ).br.B FILE.IR *stream ;.br.B char .IR *format ;.PP.B int sscanf(.IR s ,.I format[,.I pointer] ....B ).br.B char \fI*s, *format\fP;.SH Description.NXR "scanf function".NXR "fscanf function".NXR "sscanf function".NXR "formatted input" "converting".NXR "scanf function" "conversion specification list"Each function reads characters, interpretsthem according to a format, and stores the results in its arguments.Each expects, as arguments, a control string,.I format,and a set of.I pointerarguments that indicate where to store the converted input.The.PN scanffunction reads from the standard input stream.IR stdin .The.PN fscanffunction reads from the named input.IR stream .The.PN sscanf\^function reads from the character string.IR s ..PPIn the .I formatstring you specify how to convert the input stream.  You may useone or more conversion specifications in a single format string,depending on the number of .I pointerarguments you specify.   Conversion specifications are introduced by apercent sign and specify the format of one input field.  You may alsouse spaces, tabs, form feeds, new-line characters, alphabeticcharacters, and numbers in the format string.  The following listdescribes conversionspecifications and the other components of a .I formatstring:.IP \(bu 5Conversion specifications have the following format:.EX%[*][\fIw\fP][l][h][\fIcode\fP].EEEach conversion specification must be introduced by a percent sign.The rest of the conversion specification is optional and has thefollowing purpose:.IP * 5Specifies that an input field in the input string is not read by .PN scanf;that is, the function skips the field..IP \fIw\fP 5Specifies the maximum field width..IP \fBl\fP 5Specifies that the variable where the input value is stored is alongword integer or a double-precision variable. The.PN scanffunction ignores the \fBl\fP if the input field is a character string ora pointer..IP \fBh\fP 5Specifies that the variable where the input value is stored is ashort integer or floating-point variable.  The.PN scanffunction ignores the \fBh\fP if the input field is a character string or apointer. .IP \fItype\fP 5Specifies the conversion code.  Possible values for the conversioncode are described in the paragraphs that follow..br.ne 3.IP \(bu 5Alphabetic characters and numbers that appear inside the.I formatstring, but not in a conversion specification, specify that.PN scanfignore those characters in the input string..IP \(bu 5The white-space characters in a .I formatstring that appear outside of a conversion specification normally haveno effect on how.PN scanfformats data.  The exception is when the white space characterprecedes the \fBc\fP conversion code in the .I format string.  In this case, the white space causes .PN scanfto ignore leading white space in the input field.  Normally, .PN scanftreats leading white space as part of the input character string forthe \fBc\fP conversion code..PPEach conversion specification in the .I formatstring directs the conversion of the next input field.  The .PN scanffunction stores the result of each conversion in the .I pointer that corresponds to the conversion specification.Thus, the conversion specificationcontrols how.PN scanf converts the first unread input field, and.PN scanf stores the result in the first.I pointer.  The second conversion specification controls how .PN scanfconverts the next input field.The.PN scanffunction stores the result of the second conversion in the second.IR pointer ,and so on.  .PPYou do not include.I pointersfor conversion specifications that contain the asterisk character.These specifications cause .PN scanfto ignore an input field, so no .I pointerstorage is needed..PPAn input field is defined as a string of non-space characters; itbegins at the first unread character and extends to the firstinappropriate character or EOF. An inappropriate character is one thatis not validfor the value.PN scanfis reading.  For example, the letter ``z'' is invalid for an integervalue.If the.PN scanffunction does not reach EOF and encounters no inappropriatecharacters, the field width isthe number of characters specified by .IR w .For all conversion codes except left-bracket ( .BR [ ) and .BR c ,.PN scanfignores leading white space in an input field..PPThe conversion code controls how.PN scanfconverts an input field.  The data type of a.I pointerthat corresponds to a conversion specification must match theconversion code.  For example, the .I pointerthat corresponds to a .B cconversion code must point to a character variable. The .I pointer that corresponds to a .B dconversion code must point to an integer, and so on. The following list describes the valid conversion codes:.PP.TP.B %The input field is a percent sign. The.PN scanffunction does not move any value to.IR pointer ..TP.B d DThe input field is a decimal integer; the corresponding .I pointermust point to an integer.  If you specify .B h ,.I pointercan point to a short integer.  .TP.B u UThe input field is a decimal integer;.I pointermust point to an unsigned integer.  .TP.B o 0The input field is octal integer is expected;the corresponding .I pointermust point to an integer.  If you specify .B h ,.I pointercan be a short integer. .TP.B x XThe input field is a hexadecimal integer;the corresponding .I pointermust point to an integer pointer.  If you specify .BR h ,.I pointercan be a short integer.  .TP\fBe\fP,\fBf\fP,\fBg\fPThe input field is an optionally signed string ofdigits. The field may contain a radix character and an exponent fieldbegins with a letter .B Eor .BR e , followed by an optional sign or space and an integer.  The .I pointermust point to a floating-point variable.  If you specify .BR l ,.I pointer must point to a double-precision variable..TP.B sThe input field is a character string.  The.I pointermust point to an array of characters large enough to contain thestring and a termination character (\\0). The.PN scanf function adds the termination character automatically.A white-space character terminates the input field, so the input fieldcannot contain spaces..TP.B cThe input field is a character or character string.  The.I pointermust point to either a character variable or a character array..IPThe.PN scanffunction reads white space in the input field, including leadingwhite space.  To cause.PN scanfto ignore white space, you can include a space in front of theconversion specification that includes the \fBc\fP.  .TP.B [The input field is a character string.  The.I pointermust point to an array of characters large enough to contain thestring and a termination character (\\0). The.PN scanf function adds the termination character automatically..IPFollowing the left bracket, you specify a list of characters and aright bracket ( ] ).The.PN scanffunction reads the input field until it encounters a character otherthan those listed between the brackets.  The.PN scanffunction ignores white-space characters..IPYou can change the meaning of the characters within the brackets byincluding a circumflex (\fB^\fP) character before the list ofcharacters. The circumflex causes.PN scanfto read the input field until it encounters one of the characters inthe list..IPYou can represent a range of characters by specifying the firstcharacter, a hyphen (-), and the last character.  For example, you canexpress [0123456789] using [0\-9].When you use a hyphen to represent a range of characters, the firstcharacter you specify must precede or be equal to the last character you specify inthe current collating sequence.  If the last character sorts beforethe first character, the hyphen stands for itself.  The hyphen also stands foritself when it is the first or the last character that appears withinthe brackets..IPTo include the right square bracket as a character within the list, put the right bracket first in the list.  If the right bracket ispreceded by any character other than the circumflex, .PN scanfinterprets it as a closing bracket..IPAt least one input character must be validfor this conversion to be considered successful..TP.B iThe input field is an integer.  If the field begins with a zero, .PN scanfinterprets it as an octal value.  If the field begins with ``0X'' or``0x,\*u .PN scanfinterprets it as a hexadecimal value.  The.I pointermust point to an integer.  If you specify .BR h , .I pointercan point to a short integer..PP.TP.B nThe.PN scanffunction maintains a running total of the number of input fields it hasread so far.  This conversion code causes .PN scanfto store that total in the integer that corresponds to.I pointer..PP.TP.B pThe input field is a pointer.  The.I pointermust point to an integer variable..sp.PPIn all cases,.PN scanfuses the radix character and collating sequence that is defined by thelast successful call to.PN setlocalecategory.PN LC_NUMERIC or .PN LC_COLLATE .If the radix or collating sequenceis undefined, the.PN scanffunction uses the C locale definitions..SS International Environment.IP LC_NUMERIC 15If this environment is set and valid, .PN scanfuses the international language database named in the definition todetermine radix character rules..IP LANG 15If this environment variable is set and valid .PN scanfuses the international language database named in the definition todetermine collation and character classification rules.  If.PN LC_NUMERIC is defined, its definition supersedes the definition ofLANG..SH Restrictions.NXR "scanf subroutine" "restrictions"You cannot directly determine whether conversion codes that cause .PN scanfto ignore data (for example, brackets and asterisks) succeeded..PPThe.PN scanffunction ignores any trailing white-space characters, including a newlinecharacter. If you want .PN scanfto read a trailing white-space character, include the character in theconversion code for the data item that contains it..SH Examples.NXR "scanf function"The following shows an example of calling the.PN scanffunction:.EXint i, n; float x; char name[50];n = scanf("%d%f%s", &i, &x, name);.EESuppose the input to the.PN scanffunction appear as follows:.EX25 54.32E\-1 thompson.EEIn this case, .PN scanfassigns the value 25 to the .I ivariable and the value 5.432 to the .I xvariable.  The character variable .I namereceives the value thompson\\0.  The function returns the value 3 tothe .I n variable because it read and assigned three input fields..PPThe following example demonstrates using the \fBd\fP conversion code to cause .PN scanf to ignore characters:.EXint i; float x; char name[5];scanf("%2d%f %*d %[0-9]", &i, &x, name);.EESuppose the following shows the input to the function:.EX56789 0123 56a72.EEIn this case, the.PN scanffunction assigns the value 56 to the .I ivariable and the value 789.0 to the .I xvariable.  The function ignores the 0123 input field, because the.B %*dconversion specification causes .PN scanfto skip one input field.  The function assigns 56 to .IR name ; it reads the first two characters in the last input field and stops at the thirdcharacter.  The letter 'a' is not in the set of characters from 0 to 9..SH Return ValuesThe.PN scanffunction returns the number of successfully matchedand assigned input fields.  This number can be zero if the .PN scanffunction encounters invalid input characters, as specified by theconversion specification, before it can assign input characters..PPIf the input ends before the first conflict or conversion,.PN scanfreturns EOF.These functions return EOF on end of input and a short count for missing or invalid data items..SH EnvironmentIn POSIX mode, the.BR E ,.BR F ,and.B Xformats are treated the same as the.BR e ,.BR f ,and.B xformats, respectively; otherwise, the upper-case formats expect double, double, and long arguments, respectively..SH See Alsoatof(3), nl_scanf(3int), getc(3s), printf(3s), environ(5int).br.I Guide to Developing International Software

⌨️ 快捷键说明

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