📄 faqcatea63.html
字号:
a carriage returnwithout a line feed, so that you can overwrite the current line.The character<TT>'\b'</TT>is a backspace,and will usually move the cursor onepositionto the left.</p><p>Using these characters,you can print a percentage-done indicator:<pre> for(i = 0; i < lotsa; i++) { printf("\r%3d%%", (int)(100L * i / lotsa)); fflush(stdout); do_timeconsuming_work(); } printf("\ndone.\n");</pre>or a baton:<pre> printf("working: "); for(i = 0; i < lotsa; i++) { printf("%c\b", "|/-\\"[i%4]); fflush(stdout); do_timeconsuming_work(); } printf("done.\n");</pre></p><p>See also question <a href="faqcat1d60.html?sec=stdio#fflush">12.4</a>.</p><p>References:ISO Sec. 5.2.2<hr><hr><hr><a name="termcap"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/termcap.html"><!-- qtag -->Question 19.4</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I clear the screen?<br>How can Iprinttext in color?<br>How can Imove the cursorto a specific x, y position?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Such things depend on theterminal type (or display)you're using.You will have to use a library such astermcap, terminfo, or curses,or some system-specific routines, to perform these operations.</p><p>Functionsin the curses libraryto look for are<TT>clear</TT>,<TT>move</TT>,<TT>standout</TT>/<TT>standend</TT>,and<TT>attron</TT>/<TT>attroff</TT>/<TT>attrset</TT>;the last three work withattribute codes such as A_REVERSE.In MS-DOS libraries,there are typically functions named<TT>gotoxy</TT>and<TT>clrscr</TT>or<TT>_clearscreen</TT>;you can also use the ANSI.SYS driver or low-level interrupts.Under termcap or terminfo,use <TT>tgetstr</TT>to retrieve strings like<TT>cl</TT>,<TT>so</TT>/<TT>se</TT>,and <TT>cm</TT>for clear screen, standout mode, and cursor motion respectively,then output the strings;using <TT>cm</TT> additionally requires calling <TT>tgoto</TT>.Some baroque terminals requireattention to other ``capabilities'' as well;study thedocumentation carefully.Be aware that some older terminalsmaynot support the desired capabilitiesat all.</p><p>Most modern terminal emulation schemessupport the ANSI escape sequencesfor cursor motion and visual attributes,so if you're willing to sacrifice portability,you can print those sequences directly.Here is a tiny example to whet your appetite:<pre>printf("\033[2J"); /* clear screen */printf("\033[%d;%dH", 10, 20); /* move cursor (row 10, col 20) */printf("Hello, ");printf("\033[7mworld\033[0m!"); /* inverse video */</pre>Here is a link to some<a href="../../osdep/sd21.html" rel=subdocument>more explanation, and brief lists of codes.</a>The portable way of emitting these sequences(if you're not going whole-hog and using curses)is to use termcap or terminfo;here is<a href="../../osdep/sd22.html" rel=subdocument>an example.</a></p><p>For clearing the screen,a halfway portable solution is to printa form-feed character(<TT>'\f'</TT>),which will causesomedisplaysto clear.Even more portable(albeit even more gunky)might beto print enough newlinesto scroll everything away(although of coursethis leaves the cursor at the bottom of the screen,not the top).As a last resort,you could use <TT>system</TT>(see question <a href="faqcatea63.html?sec=osdep#system">19.27</a>)to invokean operating system clear-screen command.</p><p>References:PCS Sec. 5.1.4 pp. 54-60, Sec. 5.1.5 pp. 60-62<br>Strang, <I>Programming with curses</I><br>Strang, Mui, and O'Reilly, <I>termcap & terminfo</I><hr><hr><hr><a name="waitforkey"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/waitforkey.html"><!-- qtag -->Question 19.4b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm compiling some test programs on a windows-based system,and the windows containing my program's outputare closing so quicklyafter my program calls <TT>exit</TT>that I can't see the output.How can I make it pause before closing?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>After wondering why the author of your compiler's run-time systemdidn't take care of this for you,simply add the lines<pre> printf("Hit RETURN to exit"\n"); fflush(stdout); (void)getchar();</pre>just before the end of your program.(If you want to wait for any keystroke,not just the RETURN key,see question <a href="faqcatea63.html?sec=osdep#cbreak">19.1</a>.)<hr><hr><hr><a name="specialkeys"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/specialkeys.html"><!-- qtag -->Question 19.5</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How do I read the arrow keys?What about function keys?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Terminfo,some versions of termcap,and some versions of curseshave support for these non-ASCII keys.Typically,a special key sends a multicharacter sequence(usually beginning with ESC, <TT>'\033'</TT>);parsing these can be tricky.(curses will do the parsing for you,if you call <TT>keypad</TT> first.)</p><p>Under MS-DOS, if you receive acharacter withvalue 0(<em>not</em> <TT>'0'</TT>!)while reading the keyboard,it's a flag indicatingthat the next character read will be a code indicating a special key.See any DOS programming guide for lists of keyboard scan codes.(Very briefly:the up, left, right, and down arrow keys are 72, 75, 77, and 80,and the function keysare 59 through 68.)</p><p>References:PCS Sec. 5.1.4 pp. 56-7<hr><hr><hr><a name="mouse"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/mouse.html"><!-- qtag -->Question 19.6</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How do I read the mouse?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Consult your system documentation,or ask on an appropriate system-specific newsgroup(but check its FAQ list first).Mouse handling is completely different under theX window system,MS-DOS,the Macintosh,and probably every other system.</p><p>References:PCS Sec. 5.5 pp. 78-80<hr><hr><hr><a name="serial"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/serial.html"><!-- qtag -->Question 19.7</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I do serial (``comm'') port I/O?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's system-dependent.Under Unix,you typically open, read, and writea device file in <TT>/dev</TT>,and use thefacilities of the terminal driverto adjust its characteristics.(See also questions <a href="faqcatea63.html?sec=osdep#cbreak">19.1</a>and <a href="faqcatea63.html?sec=osdep#readavail">19.2</a>.)Under MS-DOS, you canusethe predefined stream <TT>stdaux</TT>,or a special file like COM1,orsome primitive BIOS interrupts,or (if you requiredecentperformance)any number of interrupt-driven serial I/O packages.Severalnettersrecommend the book<I>C Programmer's Guide to Serial Communications</I>,by Joe Campbell.<hr><hr><hr><a name="printer"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/printer.html"><!-- qtag -->Question 19.8</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I direct output to the printer?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Under Unix,either use <TT>popen</TT>(see question <a href="faqcatea63.html?sec=osdep#popen">19.30</a>)to write to the<TT>lp</TT>or<TT>lpr</TT> program,or perhaps opena special file like <TT>/dev/lp</TT>.Under MS-DOS, write to the(nonstandard)predefined stdio stream <TT>stdprn</TT>,or open the special files PRN or LPT1.Under some circumstances,another(and perhaps the only)possibility is to use a window manager's screen-capture function,and print the resulting bitmap.</p><p>References:PCS Sec. 5.3 pp. 72-74<hr><hr><hr><a name="escseqs"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/escseqs.html"><!-- qtag -->Question 19.9</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How do I send escape sequencesto control a terminal or other device?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>If you can figure out how to send characters to the device at all(see question <a href="faqcatea63.html?sec=osdep#printer">19.8</a>),it's easy enough to send escape sequences.In ASCII,the ESC code is 033(27 decimal),socode like<pre> fprintf(ofd, "\033[J");</pre>sends the sequence <TT>ESC [ J.</TT></p><p>Some programmers prefer to parameterize the ESC code,like this:<pre> #define ESC 033 fprintf(ofd, "%c[J", ESC);</pre><hr><hr><hr><a name="portio"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/portio.html"><!-- qtag -->Question 19.9b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I access an I/O board directly?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>At one level, at least, it's quite simple: you have a deviceregister which is actually wired up so that the bits written toit get coverted to actual voltage levels in the real world thatyou can do interesting things with.In general, there are two ways toget the bits in and out.(A particular I/O board willuse one method or the other;you'll need to consult its documentation for details.)<OL><li>If the device is accessed via a dedicated ``I/O port'',use system-specific functionsto communicate with it.Under MS-DOS, for example, there were quasistandard``inport'' and ``outport''instructions.<li>If the device uses ``memory-mapped I/O'',that is,if the device register(s) are accessed as if they were normal memoryat particular, known locations within the processor's addressing space,use contrived pointer variables to accessthoselocations.See question <a href="faqcatea63.html?sec=osdep#rawmemadr">19.25</a>.<hr><hr><hr><a name="graphics"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/graphics.html"><!-- qtag -->Question 19.10</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I do graphics?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Once upon a time,Unixhad a fairly nice little set of device-independent plotfunctions described in plot(3) and plot(5).The GNU libplotlibrary,written by<a href="http://uranium.math.arizona.edu/~rsm/">Robert Maier</a>,maintainsthe same spiritand supports many modern plot devices;see<a href="http://www.gnu.org/software/plotutils/plotutils.html"><TT>http://www.gnu.org/software/plotutils/plotutils.html</TT></a>.</p><p>A modern, platform-independent graphics library(which also supports 3D graphics and animation)is OpenGL.Other graphics standards which may be of interest are GKS and PHIGS.</p><p>If you're programming for MS-DOS,you'll probablywant to use librariesconforming to the VESA or BGIstandards.</p><p>If you're trying to talk to a particular plotter,making it draw is usually a matter of sending it the appropriateescape sequences;see also question <a href="faqcatea63.html?sec=osdep#escseqs">19.9</a>.The vendor may supply a C-callable library,or you may be able to find one on the net.</p><p>If you're programming for a particularwindow system(Macintosh, X windows, Microsoft Windows),you will use its facilities;see the relevantdocumentationornewsgrouporFAQ list.</p><p>References:PCS Sec. 5.4 pp. 75-77<hr><hr><hr><a name="gifjpeg"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/gifjpeg.html"><!-- qtag -->Question 19.10b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can Idisplay GIF and JPEG images?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Itwill dependon your display environment,which may already providethese functions.Reference JPEG software is at<a href="http://www.ijg.org/files/"><TT>http://www.ijg.org/files/</TT></a>.<hr><hr><hr><a name="fonts"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/fonts.html"><!-- qtag -->Question 19.10c</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I load new fonts for display?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's system-dependent.<hr><hr><hr><a name="sendmail"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/sendmail.html"><!-- qtag -->Question 19.10d</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I send mail from within a C program?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Under Unix,opena pipe to the <TT>mail</TT> program,or perhaps <TT>/usr/lib/sendmail</TT>.See question <a href="faqcatea63.html?sec=osdep#popen">19.30</a>.<hr><hr><hr><a name="fileexists"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../osdep/fileexists.html"><!-- qtag -->Question 19.11</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I check whether a file exists?I want towarn the userif a requested input file is missing.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It's surprisingly difficult tomake this determinationreliably and portably.Any test you make can be invalidatedif the file is created or deleted(i.e.by some other process)between the time you make the testand the time you try to open the file.</p><p>Three possible test functions are<TT>stat</TT>,<TT>access</TT>,and<TT>fopen</TT>.(Tomake an approximatetestusing<TT>fopen</TT>,just open for reading and close immediately,although failure does not necessarily indicate nonexistence.)Of these,only <TT>fopen</TT> is widely portable,and <TT>access</TT>,where it exists,must be used carefully ifthe programuses the Unixset-UIDfeature.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -