📄 fprintf.html
字号:
name of the entry.</p><pre><tt>#include <stdio.h>#include <time.h>#include <langinfo.h>...struct dirent *dp;struct tm *tm;char datestring[256];...strftime(datestring, sizeof(datestring), nl_langinfo (D_T_FMT), tm);<br>printf(" %s %s\n", datestring, dp->d_name);...</tt></pre><h5><a name="tag_03_180_06_04"></a>Printing Error Information</h5><p>The following example uses <i>fprintf</i>() to write error information to standard error.</p><p>In the first group of calls, the program tries to open the password lock file named <b>LOCKFILE</b>. If the file already exists,this is an error, as indicated by the O_EXCL flag on the <a href="../functions/open.html"><i>open</i>()</a> function. If the callfails, the program assumes that someone else is updating the password file, and the program exits.</p><p>The next group of calls saves a new password file as the current password file by creating a link between <b>LOCKFILE</b> andthe new password file <b>PASSWDFILE</b>.</p><pre><tt>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h><br>#define LOCKFILE "/etc/ptmp"#define PASSWDFILE "/etc/passwd"...int pfd;...if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1){ fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n"); exit(1);}...if (link(LOCKFILE,PASSWDFILE) == -1) { fprintf(stderr, "Link error: %s\n", strerror(errno)); exit(1);}...</tt></pre><h5><a name="tag_03_180_06_05"></a>Printing Usage Information</h5><p>The following example checks to make sure the program has the necessary arguments, and uses <i>fprintf</i>() to print usageinformation if the expected number of arguments is not present.</p><pre><tt>#include <stdio.h>#include <stdlib.h>...char *Options = "hdbtl";...if (argc < 2) { fprintf(stderr, "Usage: %s -%s <file\n", argv[0], Options); exit(1);}...</tt></pre><h5><a name="tag_03_180_06_06"></a>Formatting a Decimal String</h5><p>The following example prints a key and data pair on <i>stdout</i>. Note use of the <tt>'*'</tt> (asterisk) in the format string;this ensures the correct number of decimal places for the element based on the number of elements requested.</p><pre><tt>#include <stdio.h>...long i;char *keystr;int elementlen, len;...while (len < elementlen) {... printf("%s Element%0*ld\n", keystr, elementlen, i);...}</tt></pre><h5><a name="tag_03_180_06_07"></a>Creating a Filename</h5><p>The following example creates a filename using information from a previous <a href="../functions/getpwnam.html"><i>getpwnam</i>()</a> function that returned the HOME directory of the user.</p><pre><tt>#include <stdio.h>#include <sys/types.h>#include <unistd.h>...char filename[PATH_MAX+1];struct passwd *pw;...sprintf(filename, "%s/%d.out", pw->pw_dir, getpid());...</tt></pre><h5><a name="tag_03_180_06_08"></a>Reporting an Event</h5><p>The following example loops until an event has timed out. The <a href="../functions/pause.html"><i>pause</i>()</a> functionwaits forever unless it receives a signal. The <i>fprintf</i>() statement should never occur due to the possible return values of<a href="../functions/pause.html"><i>pause</i>()</a>.</p><pre><tt>#include <stdio.h>#include <unistd.h>#include <string.h>#include <errno.h>...while (!event_complete) {... if (pause() != -1 || errno != EINTR) fprintf(stderr, "pause: unknown error: %s\n", strerror(errno));}...</tt></pre><h5><a name="tag_03_180_06_09"></a>Printing Monetary Information</h5><p>The following example uses <a href="../functions/strfmon.html"><i>strfmon</i>()</a> to convert a number and store it as aformatted monetary string named <i>convbuf</i>. If the first number is printed, the program prints the format and the description;otherwise, it just prints the number.</p><pre><tt>#include <monetary.h>#include <stdio.h>...struct tblfmt { char *format; char *description;};<br>struct tblfmt table[] = { { "%n", "default formatting" }, { "%11n", "right align within an 11 character field" }, { "%#5n", "aligned columns for values up to 99999" }, { "%=*#5n", "specify a fill character" }, { "%=0#5n", "fill characters do not use grouping" }, { "%^#5n", "disable the grouping separator" }, { "%^#5.0n", "round off to whole units" }, { "%^#5.4n", "increase the precision" }, { "%(#5n", "use an alternative pos/neg style" }, { "%!(#5n", "disable the currency symbol" },};...float input[3];int i, j;char convbuf[100];...strfmon(convbuf, sizeof(convbuf), table[i].format, input[j]);<br>if (j == 0) { printf("%s %s %s\n", table[i].format, convbuf, table[i].description);}else { printf(" %s\n", convbuf);}...</tt></pre><h5><a name="tag_03_180_06_10"></a>Printing Wide Characters</h5><p>The following example prints a series of wide characters. Suppose that <tt>"L`@`"</tt> expands to three bytes:</p><pre><tt>wchar_t wz [3] = L"@@"; // Zero-terminatedwchar_t wn [3] = L"@@@"; // Unterminated<br>fprintf (stdout,"%ls", wz); // Outputs 6 bytesfprintf (stdout,"%ls", wn); // Undefined because wn has no terminatorfprintf (stdout,"%4ls", wz); // Outputs 3 bytesfprintf (stdout,"%4ls", wn); // Outputs 3 bytes; no terminator neededfprintf (stdout,"%9ls", wz); // Outputs 6 bytesfprintf (stdout,"%9ls", wn); // Outputs 9 bytes; no terminator neededfprintf (stdout,"%10ls", wz); // Outputs 6 bytesfprintf (stdout,"%10ls", wn); // Undefined because wn has no terminator</tt></pre><p>In the last line of the example, after processing three characters, nine bytes have been output. The fourth character must thenbe examined to determine whether it converts to one byte or more. If it converts to more than one byte, the output is only ninebytes. Since there is no fourth character in the array, the behavior is undefined.</p></blockquote><h4><a name="tag_03_180_07"></a>APPLICATION USAGE</h4><blockquote><p>If the application calling <i>fprintf</i>() has any objects of type <b>wint_t</b> or <b>wchar_t</b>, it must also include the <ahref="../basedefs/wchar.h.html"><i><wchar.h></i></a> header to have these objects defined.</p></blockquote><h4><a name="tag_03_180_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_180_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_180_10"></a>SEE ALSO</h4><blockquote><p><a href="fputc.html"><i>fputc</i>()</a>, <a href="fscanf.html"><i>fscanf</i>()</a>, <a href="setlocale.html"><i>setlocale</i>()</a>, <a href="strfmon.html"><i>strfmon</i>()</a>, <a href="wcrtomb.html"><i>wcrtomb</i>()</a>, the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap07.html">Chapter 7, Locale</a>, <ahref="../basedefs/stdio.h.html"><i><stdio.h></i></a>, <a href="../basedefs/wchar.h.html"><i><wchar.h></i></a></p></blockquote><h4><a name="tag_03_180_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_180_12"></a>Issue 5</h4><blockquote><p>Aligned with ISO/IEC 9899:1990/Amendment 1:1995 (E). Specifically, the <tt>l</tt> (ell) qualifier can now be used with<tt>c</tt> and <tt>s</tt> conversion specifiers.</p><p>The <i>snprintf</i>() function is new in Issue 5.</p></blockquote><h4><a name="tag_03_180_13"></a>Issue 6</h4><blockquote><p>Extensions beyond the ISO C standard are marked.</p><p>The DESCRIPTION is updated to avoid use of the term "must" for application requirements.</p><p>The following changes are made for alignment with the ISO/IEC 9899:1999 standard:</p><ul><li><p>The prototypes for <i>fprintf</i>(), <i>printf</i>(), <i>snprintf</i>(), and <i>sprintf</i>() are updated, and the XSI shadingis removed from <i>snprintf</i>().</p></li><li><p>The description of <i>snprintf</i>() is aligned with the ISO C standard. Note that this supersedes the <i>snprintf</i>()description in The Open Group Base Resolution bwg98-006, which changed the behavior from Issue 5.</p></li><li><p>The DESCRIPTION is updated.</p></li></ul><p>The DESCRIPTION is updated to use the terms "conversion specifier" and "conversion specification" consistently.</p><p>ISO/IEC 9899:1999 standard, Technical Corrigendum 1 is incorporated.</p><p>An example of printing wide characters is added.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -