📄 index.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file stdio.sgml dated Wed Dec 21 13:07:57 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/stdio/index.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:55:39 GMT --><head><meta name=GENERATOR content="faqproc"><title>Stdio</title></head><body bgcolor="#ffffff"> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><H1>12. Stdio</H1><p><a href="getcharc.html" rel=subdocument>12.1</a>What's wrong withthis code?<pre>char c;while((c = getchar()) != EOF) ...</pre></p><p><a href="eofval.html" rel=subdocument>12.1b</a>I have a simple little program that reads characters until EOF,but how do I actually <em>enter</em> that ``EOF'' valuefrom the keyboard?I see that <TT>EOF</TT> is defined by <TT><stdio.h></TT> to be -1;am I supposed to enter -1?</p><p><a href="feof.html" rel=subdocument>12.2</a>Whydoesthesimple line-copying loop<TT>while(!feof(infp)) {fgets(buf, MAXLINE, infp);fputs(buf, outfp);}</TT>copythe last line twice?</p><p><a href="linebfdur.html" rel=subdocument>12.3</a>I'm using <TT>fgets</TT> to readlines from a file into anarrayof pointers.Why do all the lines end up containing copies of the last line?</p><p><a href="fflush.html" rel=subdocument>12.4</a>My program's prompts and intermediate output don't always show upon the screen,especially when I pipe the output through another program.</p><p><a href="charatatime.html" rel=subdocument>12.5</a>How can I read one character at a time,without waiting for the RETURN key?</p><p><a href="printfpercent.html" rel=subdocument>12.6</a>How can I print a <TT>'%'</TT> characterin a <TT>printf</TT> format string?I tried <TT>\%</TT>, but it didn't work.</p><p><a href="printftypes.html" rel=subdocument>12.7</a>Why doesn't<pre>long int n = 123456;printf("%d\n", n);</pre>work?</p><p><a href="varargproto.html" rel=subdocument>12.8</a>I thought that ANSI function prototypeswere supposed to guard against argument type mismatches.</p><p><a href="scanfvsprintf.html" rel=subdocument>12.9</a>Someone told me it was wrong to use <TT>%lf</TT> with <TT>printf</TT>.How can<TT>printf</TT> use <TT>%f</TT> for type <TT>double</TT>,if <TT>scanf</TT> requires <TT>%lf</TT>?</p><p><a href="printftypedef.html" rel=subdocument>12.9b</a>What <TT>printf</TT> format should I use for a typedeflike <TT>size_t</TT>when I don't knowwhether it's <TT>long</TT> or some other type?</p><p><a href="printfvwid.html" rel=subdocument>12.10</a>How can I implement a variable field width with <TT>printf</TT>?That is, instead ofsomething like<TT>%8d</TT>,I want the width to be specifiedat run time.</p><p><a href="commaprint.html" rel=subdocument>12.11</a>How can I print numbers with commas separating the thousands?<br>What about currency formatted numbers?</p><p><a href="scanf1.html" rel=subdocument>12.12</a>Why doesn't the call<TT>scanf("%d", i)</TT>work?</p><p><a href="scanf1a.html" rel=subdocument>12.12b</a>Why <em>does</em> the call<pre>char s[30];scanf("%s", s);</pre>work?I thought you always needed an <TT>&</TT>on each variable passed to <TT>scanf</TT>.</p><p><a href="scanf2.html" rel=subdocument>12.13</a>Why doesn'tthis code:<pre>double d;scanf("%f", &d);</pre>work?</p><p><a href="scanfh.html" rel=subdocument>12.14</a>Why doesn't the code<pre>short int s;scanf("%d", &s);</pre>work?</p><p><a href="scanfvwid.html" rel=subdocument>12.15</a>How can I specify a variable width in a <TT>scanf</TT> format string?</p><p><a href="datafmts.html" rel=subdocument>12.16</a>How can I read data fromdata files with particular formats?<br>How can I read ten floats without having to use a jawbreaker <TT>scanf</TT> format<br>like<TT>"%f %f %f %f %f %f %f %f %f %f"</TT>?<br>How can I read an arbitrary number of fields from a line into an array?</p><p><a href="scanfhang.html" rel=subdocument>12.17</a>When I readnumbersfrom the keyboard with<TT>scanf</TT>and a<TT>"%d\n"</TT>format,like this:<pre> int n; scanf("%d\n", &n); printf("you typed %d\n", n);</pre>it seems to hang until I type one extra line of input.</p><p><a href="scanfinterlace.html" rel=subdocument>12.18a</a>I'm reading a number with <TT>scanf</TT>and<TT>%d</TT>,and then a string with<TT>gets()</TT>:<pre> int n; char str[80]; printf("enter a number: "); scanf("%d", &n); printf("enter a string: "); gets(str); printf("you typed %d and \"%s\"\n", n, str);</pre>but the compiler seems to beskipping the call to <TT>gets()</TT>!</p><p><a href="scanfc.html" rel=subdocument>12.18b</a>I'm using <TT>scanf %c</TT> to read a Y/N response,but later input gets skipped.</p><p><a href="scanfjam.html" rel=subdocument>12.19</a>I figured I could use <TT>scanf</TT> more safelyif I checked its return valueto make sure that the usertyped the numeric values I expect:<pre> int n; while(1) { printf("enter a number: "); if(scanf("%d", &n) == 1) break; printf("try again: "); } printf("you typed %d\n", n);</pre>but sometimes it seems to go into an infinite loop.<a href="achtung.html" rel=subdocument>[footnote]</a>Why?</p><p><a href="scanfprobs.html" rel=subdocument>12.20</a>Why does everyone say not to use <TT>scanf</TT>?What should I use instead?</p><p><a href="sprintfsize.html" rel=subdocument>12.21</a>How can I tell how much destination buffer space I'll needfor an arbitrary <TT>sprintf</TT> call?How can I avoidoverflowingthe destination buffer with <TT>sprintf</TT>?</p><p><a href="sprintfret.html" rel=subdocument>12.22</a>What's the deal on <TT>sprintf</TT>'s return value?Is it an <TT>int</TT> or a <TT>char *</TT>?</p><p><a href="getsvsfgets.html" rel=subdocument>12.23</a>Why does everyone say not to use <TT>gets()</TT>?</p><p><a href="printferrno.html" rel=subdocument>12.24</a>I thought I'd check <TT>errno</TT>after a long string of <TT>printf</TT> calls,to see if any of them had failed:<pre> errno = 0; printf("This\n"); printf("is\n"); printf("a\n"); printf("test.\n"); if(errno != 0) fprintf(stderr, "printf failed: %s\n", strerror(errno));</pre>Why is it printingsomething strange like``printf failed: Not a typewriter''when I redirect the output to a file?</p><p><a href="fgetpos.html" rel=subdocument>12.25</a>What's the difference between <TT>fgetpos</TT>/<TT>fsetpos</TT>and <TT>ftell</TT>/<TT>fseek</TT>?<br>What are <TT>fgetpos</TT> and <TT>fsetpos</TT> good for?</p><p><a href="stdinflush.html" rel=subdocument>12.26a</a>How can I flush pending input so that a user's typeaheadisn't read at the next prompt?Will <TT>fflush(stdin)</TT>work?</p><p><a href="stdinflush2.html" rel=subdocument>12.26b</a>If <TT>fflush</TT> won't work,what can I use to flush input?</p><p><a href="fopenfp.html" rel=subdocument>12.27</a>I wrote this routine which is supposed to open a file:<pre> myfopen(char *filename, FILE *fp) { fp = fopen(filename, "r"); }</pre>But when I call it like this:<pre> FILE *infp; myfopen("filename.dat", infp);</pre>the <TT>infp</TT> variablein the caller doesn't get set properly.</p><p><a href="fopenmodec.html" rel=subdocument>12.28</a>I can't even get a simple <TT>fopen</TT> call to work!What's wrong withthis call?<pre> FILE *fp = fopen(filename, 'r');</pre></p><p><a href="constrpath.html" rel=subdocument>12.28b</a>How can I open files with names like``<TT>file1</TT>'',``<TT>file2</TT>'',``<TT>file3</TT>'',etc.,where the numeric part is controlled by a variable?Basically I want ``<TT>file%d</TT>'',like <TT>printf</TT>.</p><p><a href="fopenpath.html" rel=subdocument>12.29</a><TT>fopen</TT> is failing for certain pathnames.</p><p><a href="fupdate.html" rel=subdocument>12.30</a>I'm trying to update a file in place,by using <TT>fopen</TT> mode <TT>"r+"</TT>,reading a certain string,and writing back a modified string,but it's not working.</p><p><a href="insdelrec.html" rel=subdocument>12.31</a>How can I insert or delete a line(or record)in the middle of a file?</p><p><a href="fdfilename.html" rel=subdocument>12.32</a>How can I recover the file name given an openstream?</p><p><a href="freopen.html" rel=subdocument>12.33</a>How can I redirect <TT>stdin</TT> or <TT>stdout</TT>to a filefrom within a program?</p><p><a href="undofreopen.html" rel=subdocument>12.34</a>Once I've used <TT>freopen</TT>, how can I get the original<TT>stdout</TT>(or <TT>stdin</TT>)back?</p><p><a href="redirp.html" rel=subdocument>12.35</a>How can I tell if standard input or output is redirected(i.e. whether ``<TT><</TT>'' or ``<TT>></TT>''was used on the invocation command line)?</p><p><a href="devtty.html" rel=subdocument>12.36</a>I'm trying to write a program like ``<TT>more</TT>.''How can Igetbacktothe interactive keyboard if <TT>stdin</TT> is redirected?</p><p><a href="multiway.html" rel=subdocument>12.36b</a>How can I arrange to have output go two places at once,e.g. to the screen and to a file?</p><p><a href="binaryio.html" rel=subdocument>12.37</a>I want toread and write numbersbetween files and memoryin a byte-at-a-time way,not as formatted charactersthe way <TT>fprintf</TT> and <TT>fscanf</TT> do.Howcan I do this?</p><p><a href="fopenbinary.html" rel=subdocument>12.38</a>How can I read a binary data file properly?I'm occasionally seeing <TT>0x0a</TT> and <TT>0x0d</TT> values getting garbled,andI seemto hit EOF prematurely if the data contains the value <TT>0x1a</TT>.</p><p><a href="stdinbinary.html" rel=subdocument>12.39</a>I'm writing a``filter''for binary files,but <TT>stdin</TT> and <TT>stdout</TT> are preopenedas text streams.How can I changetheir modeto binary?</p><p><a href="textvsbinary.html" rel=subdocument>12.40</a>What's the difference between text and binary I/O?</p><p><a href="structio.html" rel=subdocument>12.41</a>How can I read/write structures from/todata files?</p><p><a href="extconform.html" rel=subdocument>12.42</a>How can I write code to conform to these old, binary data file formats?</p><p><a href="runtimesc2.html" rel=subdocument>12.43</a>I'm readingstringstyped bythe userinto an array,and then printing them out later.When the user typesa sequence like <TT>\n</TT>,whyisn't it beinghandled properly?</p><hr><p><a href="../index-2.html">top</a></p><p><a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></p></body><!-- Mirrored from c-faq.com/stdio/index.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:55:58 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -