📄 ansistdio.html
字号:
)</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine opens a file whose name is the string pointed to by <i>file</i>and associates a stream with it.The argument <i>mode</i> points to a string beginning with one of the following sequences:<dl><dt><b>r</b><dd>open text file for reading<dt><b>w</b><dd>truncate to zero length or create text file for writing<dt><b>a</b><dd>append; open or create text file for writing at end-of-file<dt><b>rb</b><dd>open binary file for reading<dt><b>wb</b><dd>truncate to zero length or create binary file for writing<dt><b>ab</b><dd>append; open or create binary file for writing at end-of-file<dt><b>r+</b><dd>open text file for update (reading and writing)<dt><b>w+</b><dd>truncate to zero length or create text file for update.<dt><b>a+</b><dd>append; open or create text file for update, writing at end-of-file<dt><b>r+b / rb+</b><dd>open binary file for update (reading and writing)<dt><b>w+b / wb+</b><dd>truncate to zero length or create binary file for update<dt><b>a+b / ab+</b><dd>append; open or create binary file for update, writing at end-of-file</dl><p>Opening a file with read mode (<b>r</b> as the first character in the <i>mode</i>argument) fails if the file does not exist or cannot be read.<p>Opening a file with append mode (<b>a</b> as the first character in the <i>mode</i>argument) causes all subsequent writes to the file to be forced to thethen current end-of-file, regardless of intervening calls to <b><a href="./ansiStdio.html#fseek">fseek</a>( )</b>. Insome implementations, opening a binary file with append mode (<b>b</b> as thesecond or third character in the <i>mode</i> argument) may initially positionthe file position indicator for the stream beyond the last data written,because of null character padding. In VxWorks, whether append mode issupported is device-specific.<p>When a file is opened with update mode (<b>+</b> as the second or thirdcharacter in the <i>mode</i> argument), both input and output may be performedon the associated stream. However, output may not be directly followed byinput without an intervening call to <b><a href="./ansiStdio.html#fflush">fflush</a>( )</b> or to a file positioningfunction (<b><a href="./ansiStdio.html#fseek">fseek</a>( )</b>, <b><a href="./ansiStdio.html#fsetpos">fsetpos</a>( )</b>, or <b><a href="./ansiStdio.html#rewind">rewind</a>( )</b>), and input may not be directlyfollowed by output without an intervening call to a file positioningfunction, unless the input operation encounters end-of-file. Opening (orcreating) a text file with update mode may instead open (or create) abinary stream in some implementations.<p>When opened, a stream is fully buffered if and only if it can be determinednot to refer to an interactive device. The error and end-of-fileindicators for the stream are cleared.<p></blockquote><h4>INCLUDE FILES</h4><blockquote><p><b>stdio.h</b> <p></blockquote><h4>RETURNS</h4><blockquote><p><p>A pointer to the object controlling the stream, or a null pointer if theoperation fails.<p></blockquote><h4>SEE ALSO</h4><blockquote><p><b><a href="./ansiStdio.html#top">ansiStdio</a></b>, <b><a href="./ansiStdio.html#fdopen">fdopen</a>( )</b>, <b><a href="./ansiStdio.html#freopen">freopen</a>( )</b><hr><a name="fprintf"></a><p align=right><a href="rtnIndex.htm"><i>OS Libraries : Routines</i></a></p></blockquote><h1>fprintf( )</h1> <blockquote></a></blockquote><h4>NAME</h4><blockquote> <p><strong>fprintf( )</strong> - write a formatted string to a stream (ANSI)</p></blockquote><h4>SYNOPSIS</h4><blockquote><p><pre>int fprintf ( FILE * fp, /* stream to write to */ const char * fmt, /* format string */ ... /* optional arguments to format string */ )</pre></blockquote><h4>DESCRIPTION</h4><blockquote><p>This routine writes output to a specified stream under control of the string<i>fmt</i>. The string <i>fmt</i> contains ordinary characters, which are writtenunchanged, plus conversion specifications, which cause the arguments thatfollow <i>fmt</i> to be converted and printed as part of the formatted string.<p>The number of arguments for the format is arbitrary, but they mustcorrespond to the conversion specifications in <i>fmt</i>. If there areinsufficient arguments, the behavior is undefined. If the format isexhausted while arguments remain, the excess arguments are evaluated butotherwise ignored. The routine returns when the end of the format stringis encountered.<p>The format is a multibyte character sequence, beginning and ending in itsinitial shift state. The format is composed of zero or more directives:ordinary multibyte characters (not <b>%</b>) that are copied unchanged to theoutput stream; and conversion specification, each of which results infetching zero or more subsequent arguments. Each conversion specificationis introduced by the <b>%</b> character. After the <b>%</b>, the following appear insequence:<ul><li>Zero or more flags (in any order) that modify the meaning of the </li>conversion specification.<li>An optional minimum field width. If the converted value has fewer</li>characters than the field width, it will be padded with spaces (bydefault) on the left (or right, if the left adjustment flag, described later, has been given) to the field width. The fieldwidth takes the form of an asterisk (<b>*</b>) (described later) or a decimalinteger.<li>An optional precision that gives the minimum number of digits to</li>appear for the <b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>, <b>x</b>, and <b>X</b> conversions, the number of digits to appear after the decimal-point character for <b>e</b>, <b>E</b>, and <b>f</b>conversions, the maximum number of significant digits for the <b>g</b> and<b>G</b> conversions, or the maximum number of characters to be written from a string in the <b>s</b> conversion. The precision takes the form of a period (<b>.</b>) followed either by an asterisk (<b>*</b>) (described later) or byan optional decimal integer; if only the period is specified, the precision is taken as zero. If a precision appears with any otherconversion specifier, the behavior is undefined.<li>An optional <b>h</b> specifying that a following <b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>, <b>x</b>, and</li><b>X</b> conversion specifier applies to a <b>short int</b> or <b>unsigned short int</b>argument (the argument will have been promoted according to the integralpromotions, and its value converted to <b>short int</b> or<b>unsigned short int</b> before printing); an optional <b>h</b> specifying that afollowing <b>n</b> conversion specifier applies to a pointer to a <b>short int</b>argument; an optional <b>l</b> (el) specifying that a following <b>d</b>, <b>i</b>, <b>o</b>,<b>u</b>, <b>x</b>, and <b>X</b> conversion specifier applies to a <b>long int</b> or<b>unsigned long int</b> argument; or an optional <b>l</b> specifying that a following<b>n</b> conversion specifier applies to a pointer to a <b>long int</b>argument. If an <b>h</b> or <b>l</b> appears with any other conversionspecifier, the behavior is undefined.<p></blockquote><h4>WARNING</h4><blockquote><p>ANSI C also specifies an optional <b>L</b> in some of the samecontexts as <b>l</b> above, corresponding to a <b>long double</b> argument.However, the current release of the VxWorks libraries does not support <b>long double</b> data; using the optional <b>L</b> gives unpredictable results.<li>A character that specifies the type of conversion to be applied.</li></ul><p>As noted above, a field width, or precision, or both, can be indicated byan asterisk (<b>*</b>). In this case, an <b>int</b> argument supplies the field widthor precision. The arguments specifying field width, or precision, or both,should appear (in that order) before the argument (if any) to be converted.A negative field width argument is taken as a <b>-</b> flag followed by a positivefield width. A negative precision argument is taken as if the precisionwere omitted.<p>The flag characters and their meanings are:<dl><dt><b>-</b><dd>The result of the conversion will be left-justified within the field.(it will be right-justified if this flag is not specified.)<dt><b>+</b><dd>The result of a signed conversion will always begin with a plus or minus sign. (It will begin with a sign only when a negative valueis converted if this flag is not specified.)<dt><b>space</b><dd>If the first character of a signed conversion is not a sign, or if a signed conversion results in no characters, a space will be prefixed to the result. If the <b>space</b> and <b>+</b> flags both appear, the<b>space</b> flag will be ignored.<dt><b>#</b><dd>The result is to be converted to an "alternate form." For <b>o</b> conversionit increases the precision to force the first digit of the result to be azero. For <b>x</b> (or <b>X</b>) conversion, a non-zero result will have "0x" (or"0X") prefixed to it. For <b>e</b>, <b>E</b>, <b>f</b>, <b>g</b>, and <b>G</b> conversions, theresult will always contain a decimal-point character, even if no digitsfollow it. (Normally, a decimal-point character appears in the result ofthese conversions only if no digit follows it). For <b>g</b> and <b>G</b>conversions, trailing zeros will not be removed from the result. Forother conversions, the behavior is undefined.<dt><b>0</b><dd>For <b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>, <b>x</b>, <b>X</b>, <b>e</b>, <b>E</b>, <b>f</b>, <b>g</b>, and <b>G</b> conversions,leading zeros (following any indication of sign or base) are used to padto the field width; no space padding is performed. If the <b>0</b> and <b>-</b>flags both appear, the <b>0</b> flag will be ignored. For <b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>,<b>x</b>, and <b>X</b> conversions, if a precision is specified, the <b>0</b> flag willbe ignored. For other conversions, the behavior is undefined.</dl><p>The conversion specifiers and their meanings are:<dl><dt><b>d</b>, <b>i</b><dd>The <b>int</b> argument is converted to signed decimal in the style<b>[-]dddd</b>. The precision specifies the minimum number of digits to appear; if the value being converted can be represented infewer digits, it will be expanded with leading zeros. Thedefault precision is 1. The result of converting a zero valuewith a precision of zero is no characters.<dt><b>o</b>, <b>u</b>, <b>x</b>, <b>X</b><dd>The <b>unsigned int</b> argument is converted to unsigned octal (<b>o</b>),unsigned decimal (<b>u</b>), or unsigned hexadecimal notation (<b>x</b> or <b>X</b>)in the style <b>dddd</b>; the letters abcdef are used for <b>x</b> conversionand the letters ABCDEF for <b>X</b> conversion. The precision specifiesthe minimum number of digits to appear; if the value being converted can be represented in fewer digits, it will be expanded with leading zeros. The default precision is 1. Theresult of converting a zero value with a precision of zero is no characters.<dt><b>f</b><dd>The <b>double</b> argument is converted to decimal notation in the style <b>[-]ddd.ddd</b>, where the number of digits after the decimalpoint character is equal to the precision specification. If theprecision is missing, it is taken as 6; if the precision is zeroand the <b>#</b> flag is not specified, no decimal-point characterappears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriatenumber of digits.<dt><b>e</b>, <b>E</b><dd>The <b>double</b> argument is converted in the style <b>[-]d.ddde+/-dd</b>,where there is one digit before the decimal-point character (which is non-zero if the argument is non-zero) and the numberof digits after it is equal to the precision; if the precisionis missing, it is taken as 6; if the precision is zero and the <b>#</b> flag is not specified, no decimal-point character appears. Thevalue is rounded to the appropriate number of digits. The <b>E</b>conversion specifier will produce a number with <b>E</b> instead of <b>e</b>introducing the exponent. The exponent always contains at leasttwo digits. If the value is zero, the exponent is zero.<dt><b>g</b>, <b>G</b><dd>The <b>double</b> argument is converted in style <b>f</b> or <b>e</b> (or in style <b>E</b> in the case of a <b>G</b> conversion specifier), with the precisionspecifying the number of significant digits. If the precisionis zero, it is taken as 1. The style used depends on the value converted; style <b>e</b> (or <b>E</b>) will be used only if the exponent resulting from such a conversion is less than -4 or greater than or equal to the precision. Trailing zeros areremoved from the fractional portion of the result; a decimal-pointcharacter appears only if it is followed by a digit.<dt><b>c</b><dd>The <b>int</b> argument is converted to an <b>unsigned char</b>, and the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -