- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
 |
< ><FONT color=#ff0000>函数名: sbrk </FONT>
1 Y4 |2 }3 `! F功 能: 改变数据段空间位置 % v& ~* R \( m! G
用 法: char *sbrk(int incr);
' C/ g- y* q6 z3 ?; u程序例: </P>
+ o/ y0 q9 O5 G% Q9 H% X9 a< ><FONT color=#0000ff>#include <STDIO.H>
7 Y4 T6 U1 J8 x2 m$ Z. f9 G* q! {8 j#include <ALLOC.H></FONT></P>7 ?! E/ Z9 r5 O
< ><FONT color=#0000ff>int main(void) ) F8 ~$ w8 I9 Z" r# X. U) [
{
& i2 @% C% {2 H: j2 O+ C8 \4 g: N) h! Bprintf("Changing allocation with sbrk()\n"); : K% w% I& U" U: u' V6 d
printf("Before sbrk() call: %lu bytes free\n",
7 B. }) H/ g/ E: J- _9 a! q/ {) P(unsigned long) coreleft()); 9 B9 |7 v( F" `$ C. R; W! T0 Y6 ]2 ]
sbrk(1000); 0 E a+ Y" L# T& R$ f6 c9 b
printf(" After sbrk() call: %lu bytes free\n",
) [4 j3 v1 c( f; p% R& D" r) P8 `(unsigned long) coreleft());
7 u: X# c0 D! b' W, B; Treturn 0; - q9 D4 G& K4 A ^* W% R3 u
}
, U% o F# x# J0 S% Z
, ]! W5 A$ E+ I$ H" d4 h</FONT></P>5 g$ v# X6 S4 ]" \& j3 k# }5 E9 J8 T1 X
< ><FONT color=#ff0000>函数名: scanf </FONT>
$ q% j& M% T$ ]6 G7 g9 {功 能: 执行格式化输入
, D; c: ^% P. J# T( e, c/ u- B用 法: int scanf(char *format[,argument,...]); 5 d, O$ D7 B: m
程序例: </P>
/ q& O( N# @5 V/ q4 h4 H/ m< ><FONT color=#0000ff>#include <STDIO.H>
3 }& S. y/ H9 x% B% I6 c, z#include <CONIO.H></FONT></P># x3 B: `& h$ U* @. N. L
< ><FONT color=#0000ff>int main(void)
' S, \8 m6 M0 h- j/ {# g{ 1 q6 a! T: q, x1 \/ U! @, [
char label[20];
! w) { [5 k% j! V( Z& Uchar name[20]; 4 X' f2 N P0 t
int entries = 0;
" p0 v) r7 J$ |5 u ^+ \7 I* t4 n: Zint loop, age; # i9 t. c3 v; A4 ?+ j7 k) H Y1 b9 ?
double salary; </FONT></P>
7 e$ @' w3 }( F- n% x0 n* d" L2 v< ><FONT color=#0000ff>struct Entry_struct
* Q* r' q' m% [, {% l G, O p{ 6 `; F5 s2 M) \/ l% F
char name[20];
( S6 p) Z# a. D9 yint age; # N+ o% X! Z& @0 U, Q
float salary; - ?6 U M' J" x) l. f$ ~* q
} entry[20]; </FONT></P>4 K! r! f* ?8 g+ r3 j) d% ?7 K4 Z
< ><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */ 0 d/ f* b9 O2 g+ u+ Z3 q
printf("\n\nPlease enter a label for the chart: "); * R+ h' |4 C+ ^: U
scanf("%20s", label);
9 `% C( A+ b8 D) l: f1 `8 xfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
4 S9 Y7 ^% U3 v: m6 s- ?7 K< ><FONT color=#0000ff>/* Input number of entries as an integer */
8 U1 N6 s+ _" T: U2 F0 X; {1 ]printf("How many entries will there be? (less than 20) ");
: a/ z6 M0 M- S; R- C; \+ C3 nscanf("%d", &entries); $ ^ y- L) `7 v7 k0 b+ Q7 d" D
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
; \8 Y R' y/ U' C, Y< ><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ Q0 U, ^* A- l0 r6 v# s
for (loop=0;loop<ENTRIES;++LOOP)
) X5 X8 M. M1 P# S5 n {
8 `$ f# ^5 {+ L) E. _( Fprintf("Entry %d\n", loop);
9 ?6 N- @8 J, u3 j! e7 |6 N7 Fprintf(" Name : ");
. G0 ]- ?, v4 }6 nscanf("%[A-Za-z]", entry[loop].name);
9 i4 o4 {+ S0 m5 @& s& F pfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
0 M3 V W: @/ m+ b/ M< ><FONT color=#0000ff>/* input an age as an integer */ 0 ~# l, U/ R$ B" s$ u9 `
printf(" Age : "); / @2 ?% M F% o, ~
scanf("%d", &entry[loop].age); K% X$ G$ n$ s" C& v8 |( J4 e( }
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
$ P- [$ d# C9 u2 m0 e: [1 f< ><FONT color=#0000ff>/* input a salary as a float */ * o2 q/ z$ }' h
printf(" Salary : ");
0 ^6 G2 V; N# L" f9 s H& vscanf("%f", &entry[loop].salary); % l2 o9 {$ E# D3 M
fflush(stdin); /* flush the input stream in case of bad input */ / u( B6 O9 d8 X
} </FONT></P>
6 |6 C. x8 B3 ?1 `; e \% q< ><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */ 6 y4 @' Q# q A' }2 `" Y& X* K/ q
printf("\nPlease enter your name, age and salary\n"); , I+ ^- ?/ ]! c J( J- i, _
scanf("%20s %d %lf", name, &age, &salary);
" H$ N/ |* a* S. X</FONT></P>
1 k5 M- f; P' Z' F+ C: Z# D1 \< ><FONT color=#0000ff>/* Print out the data that was input */ 1 U- x4 w- w" N; T" y
printf("\n\nTable %s\n",label);
+ X, Z$ ~5 q3 y. V: ^printf("Compiled by %s age %d $%15.2lf\n", name, age, salary); : V4 @6 \/ _0 l
printf("-----------------------------------------------------\n");
2 H* A( A# i5 N8 t# qfor (loop=0;loop<ENTRIES;++LOOP) . H5 r! K G# o1 v& Y, s# p
printf("%4d | %-20s | %5d | %15.2lf\n",
! f$ Z R. r: K jloop + 1, 1 e& L& z0 G) o- C" a* R, v
entry[loop].name,
+ l9 j3 g3 G2 W. x' i- a- R) e. pentry[loop].age, / T1 Y1 x: z# p p4 Y- U: A6 j
entry[loop].salary);
7 w3 ^! s3 D9 s4 k! {- Hprintf("-----------------------------------------------------\n"); . M. f. c# t" I9 d/ j& w( H
return 0; ! s+ u/ M2 v6 I! M! t& E
} 2 e" R$ _) I- D O
</FONT>
. F* T+ d+ H0 e- D, u G</P>
" |" a$ Q: R( }, d< ><FONT color=#ff0000>函数名: searchpath </FONT>
# s, B1 \/ w# W, g s功 能: 搜索DOS路径 + {: _% f0 G( O; j
用 法: char *searchpath(char *filename); % b: D+ U* r( C7 k4 t
程序例: </P>
: d$ P$ h: {0 B# [# N0 n/ o0 a< ><FONT color=#0000ff>#include <STDIO.H>
0 v) V- _4 x& d4 R. ~#include <DIR.H></FONT></P>
8 z9 \/ r/ e7 h( }/ M& ]< ><FONT color=#0000ff>int main(void) " m, |6 w& x7 B9 y6 ]! t
{
- I# ]8 b* `$ [; x% {1 V) {) `char *p; </FONT></P>
3 @+ t2 W1 V! b" N< ><FONT color=#0000ff>/* Looks for TLINK and returns a pointer ; a6 Y3 @1 b5 d2 [) l! W* F
to the path */ 9 @+ `5 ~! V/ n) P2 k- }1 q
p = searchpath("TLINK.EXE");
7 f# |8 h' H* G/ H: Wprintf("Search for TLINK.EXE : %s\n", p); </FONT></P>9 t+ a8 E3 i9 Y0 W
< ><FONT color=#0000ff>/* Looks for non-existent file */
; t, P) p% T3 d4 `5 o4 A# p. |1 qp = searchpath("NOTEXIST.FIL"); # g; N) s- ]% K1 A* d3 D
printf("Search for NOTEXIST.FIL : %s\n", p); </FONT></P>; n7 O% q) d- ^) Q- z/ }
< ><FONT color=#0000ff>return 0; t/ j- B* j8 M; D" `3 i
} , y9 c0 X. k: t7 {2 h
?* r: Q3 A: R
</FONT></P>( w7 Q6 h2 J2 G1 n: Z
< ><FONT color=#ff0000>函数名: sector </FONT>2 r# S+ x* F# x4 Y* F# m
功 能: 画并填充椭圆扇区 % ~$ J' F4 }) V3 C% {" X% d6 [
用 法: void far sector(int x, int y, int stangle, int endangle); ! ~ U# S! {, t, p" y
程序例: </P>
3 z8 ]0 H& p& U% ?$ ~; u" c; k- [< ><FONT color=#0000ff>#include <GRAPHICS.H>% a2 D3 j) R4 a
#include <STDLIB.H>0 c) i( R8 v/ b0 u
#include <STDIO.H>
& Y8 J" X. Y( |& g4 t$ `#include <CONIO.H></FONT></P>
6 }* b, e9 o( A1 l7 {6 h< ><FONT color=#0000ff>int main(void) : N+ M" L8 B2 y8 v
{ ; q1 O" [6 f. h Q2 S( R& H. i1 x
/* request auto detection */ y0 j( @: s0 C/ k i: e I" b
int gdriver = DETECT, gmode, errorcode; 7 b! j/ @9 I* D4 B; `# _
int midx, midy, i;
& k; M0 G+ [3 N$ I) |7 r: m1 Fint stangle = 45, endangle = 135;
: L) p# j' H c7 k R6 ^% zint xrad = 100, yrad = 50; </FONT></P>- H$ Q7 R6 @6 z0 D, P8 S) Q
< ><FONT color=#0000ff>/* initialize graphics and local variables */ , H7 E4 w% |- G" H
initgraph(&gdriver, &gmode, ""); </FONT></P>
) z2 A+ b2 m2 k/ v0 E9 g< ><FONT color=#0000ff>/* read result of initialization */ Z4 j0 k6 {3 ]" y+ ^4 k
errorcode = graphresult(); , R+ o8 G$ S/ x2 {3 y/ F- V8 `6 e H
if (errorcode != grOk) /* an error occurred */
1 A% G2 p' l) z$ ^" z6 M{ ( X, s* t3 L9 B5 ?6 v" U
printf("Graphics error: %s\n", grapherrormsg(errorcode));
* v/ _9 U' I) Z9 t- v0 rprintf(" ress any key to halt:");
* \, X2 f8 [' ^/ Sgetch();
& {& j3 P/ Y2 G- eexit(1); /* terminate with an error code */
h& D9 e5 c# @} </FONT></P>
1 _+ _, a N3 e( T< ><FONT color=#0000ff>midx = getmaxx() / 2; ; U+ Q) E: f2 W) q& J2 s8 |
midy = getmaxy() / 2; </FONT></P>. x, u& l$ n; J7 P& b# C, ~
< ><FONT color=#0000ff>/* loop through the fill patterns */
" u8 G! `5 y: U4 ifor (i=EMPTY_FILL; i<USER_FILL; <br i++)> {
" W- b" c, R) l4 `' M/* set the fill style */
, d, B9 I4 y9 P4 D0 Q, `setfillstyle(i, getmaxcolor()); </FONT></P>
. O' b# J2 Y3 r! J6 X) D" @< ><FONT color=#0000ff>/* draw the sector slice */
, w$ i0 r4 M- J! @sector(midx, midy, stangle, endangle, xrad, yrad); </FONT></P>
; Y! z% x3 Y. b$ ~< ><FONT color=#0000ff>getch();
- S$ J8 c' w+ z2 T9 _} </FONT></P>* c' R) r' u7 D, i
<P><FONT color=#0000ff>/* clean up */
8 A P+ M* V; O pclosegraph(); 8 t' o. J; N8 G: x; E8 }7 Z! f& C2 Z
return 0; ' z5 K" h" J u
}
$ F9 W+ B# _3 f</FONT></P>
; G3 l4 Z! A6 @/ ]7 c* J+ \<P><FONT color=#ff0000>函数名: segread </FONT>+ M8 D( Y' g; q7 Z
功 能: 读段寄存器值
. b! G% y; i5 v' c: Q用 法: void segread(struct SREGS *segtbl);
* g& V# H# @- f2 Y) e程序例: </P>/ L' S5 k0 ~# K
<P><FONT color=#0000ff>#include <STDIO.H>
) Y/ ^5 Z: R2 m% ~ h9 a#include <DOS.H></FONT></P>
; K8 p* f! h4 j) F' p# ?<P><FONT color=#0000ff>int main(void) 7 @5 o, d+ i) G0 r4 F
{ ! N+ b: }7 H- `2 S. L
struct SREGS segs; </FONT></P>) S$ v0 P, Q# `; r6 N& ]
<P><FONT color=#0000ff>segread(&segs); % ]0 ~4 F( [. i. z
printf("Current segment register settings\n\n");
3 k; C, h, W9 S) U! s' dprintf("CS: %X DS: %X\n", segs.cs, segs.ds);
1 _9 Z' O2 |7 s6 m* J$ N8 Fprintf("ES: %X SS: %X\n", segs.es, segs.ss); </FONT></P>4 Y w' b( A0 J
<P><FONT color=#0000ff>return 0; ( q5 n' O( d* h& [* C
} </FONT>
5 S, ^7 e' C' b4 P7 W& C3 M) x, n% ?6 N( P
</P>7 q' c8 B' X& R& C: t6 {0 m
<P><FONT color=#ff0000>函数名: setactivepage </FONT>
. g& X6 F H0 a6 M' W" v8 z5 p功 能: 设置图形输出活动页 ~9 U# q6 S+ V
用 法: void far setactivepage(int pagenum);
- H8 ?4 y5 |' _4 b7 V9 {, }程序例: </P>
w/ `! Z/ B, S* w<P><FONT color=#0000ff>#include <GRAPHICS.H>* o. d* E% d0 _, M9 G1 P7 D; ]+ q
#include <STDLIB.H>! k6 P* o" p0 Z5 g" p3 g' W- ?
#include <STDIO.H>
/ l# ~$ o3 A3 R4 W! M- l2 O#include <CONIO.H></FONT></P>
3 H7 |" Q. Q. A \( R<P><FONT color=#0000ff>int main(void) 4 \) q5 W9 m( Q! t- R- W7 o
{
! U3 ^ \1 S# G1 J; _5 F/* select a driver and mode that supports */ * L' Y# U" l9 p' {
/* multiple pages. */ 0 q& s# Y- Q- R/ Z1 ]* r
int gdriver = EGA, gmode = EGAHI, errorcode; 2 b9 G1 N9 J9 q3 x* R. ]) k- |
int x, y, ht; </FONT></P>
8 v0 A( P- z0 {) G' W) S4 w! d<P><FONT color=#0000ff>/* initialize graphics and local variables */
4 _ @' G$ k, W, Rinitgraph(&gdriver, &gmode, ""); </FONT></P> O# `% M' p' u, v) [; f; E7 `
<P><FONT color=#0000ff>/* read result of initialization */ ) Z1 N) J# G" S
errorcode = graphresult(); 8 D/ B' D6 K) j. S( z5 [) g" M% L
if (errorcode != grOk) /* an error occurred */
K8 q/ c& a k& m S4 E% d{
2 ^0 o# u( t5 j6 K: [printf("Graphics error: %s\n", grapherrormsg(errorcode)); ) T' P. e# c* [: ?# Z
printf("Press any key to halt:");
( w+ R) V/ a: Lgetch(); 2 s! _ @. j0 U& H9 ]7 ]
exit(1); /* terminate with an error code */
4 q+ M; H) q! @" n8 K+ h0 t} </FONT></P>$ S+ P7 ~% s2 u$ z# l5 r+ i1 M
<P><FONT color=#0000ff>x = getmaxx() / 2; 7 t6 U3 F- d& e5 H. e
y = getmaxy() / 2; ; A; |& c1 B" k; [9 v5 Y- b& `
ht = textheight("W"); </FONT></P>7 F1 _8 v. ?# K. S9 c) I
<P><FONT color=#0000ff>/* select the off screen page for drawing */ & k. g. n+ l/ e" k+ L% l7 J6 ^
setactivepage(1); </FONT></P>- M# Y; F+ o0 _9 `" _" d8 U
<P><FONT color=#0000ff>/* draw a line on page #1 */
4 A6 ^. U' S r# hline(0, 0, getmaxx(), getmaxy()); </FONT></P>
/ |" i( J7 r. C1 e, u" z D<P><FONT color=#0000ff>/* output a message on page #1 */ 0 f2 G0 N8 h9 D% }) A% O2 i
settextjustify(CENTER_TEXT, CENTER_TEXT);
7 ?3 u4 ]( a ~8 a9 Xouttextxy(x, y, "This is page #1:");
6 @( p" r' q p* y5 }0 i$ N( pouttextxy(x, y+ht, "Press any key to halt:"); </FONT></P>
4 x8 b% u3 `# I0 |" K6 H0 Q7 Y<P><FONT color=#0000ff>/* select drawing to page #0 */ / w$ S+ q2 X1 Q, K; F
setactivepage(0); </FONT></P>; Y+ D; j& s' O; W6 U/ ]
<P><FONT color=#0000ff>/* output a message on page #0 */
. p- U/ ?5 t" eouttextxy(x, y, "This is page #0."); . l+ z9 ^8 ~/ F0 g3 J a
outtextxy(x, y+ht, "Press any key to view page #1:"); ; ]2 t; Z+ x/ \4 L N7 `" {# p
getch(); </FONT></P>3 [; u* \, n2 L+ ? G
<P><FONT color=#0000ff>/* select page #1 as the visible page */ , {0 K3 @' G( P8 A; H
setvisualpage(1); </FONT></P>* L- u8 m3 N# P3 Y
<P><FONT color=#0000ff>/* clean up */ % k* u7 b* t! g$ w
getch();
' j# ] S; Z4 }3 Hclosegraph(); 3 Z2 d7 w) z* E
return 0;
# Y$ [& O/ q& `* O}
& ^ J. d: h" @' T, f% {5 g% ^</FONT>
6 f+ _7 W; d2 a</P>
3 z, ?; g: @ Z; V2 r2 S1 @* e$ f<P><FONT color=#ff0000>函数名: setallpallette </FONT>3 d8 H+ h; h9 v" j
功 能: 按指定方式改变所有的调色板颜色
?$ U y) w5 g! n2 }$ C& H0 u用 法: void far setallpallette(struct palette, far *pallette); 7 x$ Z2 P4 t6 _8 Q9 A
程序例: </P>% u6 N K& ~* G* L1 k2 k
<P><FONT color=#0000ff>#include <GRAPHICS.H>! X2 D% R1 Z- r: k. g
#include <STDLIB.H>
) z4 _. _3 E P/ S8 `#include <STDIO.H>9 j% j7 Y5 e1 Q- Z: Y
#include <CONIO.H></FONT></P>
1 M4 ?7 N) G8 T+ K2 B<P><FONT color=#0000ff>int main(void)
6 Q/ J4 p. m% ~8 e: j y{
. J5 K+ t+ \% ~: e* t) A/* request auto detection */
3 q! o* T& g9 Z j* ?! I/ w& |int gdriver = DETECT, gmode, errorcode; $ c4 {' @' j& ~( v& [% L$ R
struct palettetype pal;
4 \/ S) }; n7 h& bint color, maxcolor, ht; : g1 B0 R. K6 Z1 M$ Y) ^4 q
int y = 10; " }2 i d, K( y( a* m
char msg[80]; </FONT></P>
* B% l r% M' P; s% m. M' v<P><FONT color=#0000ff>/* initialize graphics and local variables */
) j, j$ `8 z% _) B7 a) ninitgraph(&gdriver, &gmode, ""); </FONT></P>
. q# j: j8 {& J& i$ P& j<P><FONT color=#0000ff>/* read result of initialization */
/ y# O. `, u" `* merrorcode = graphresult(); 2 _# E8 {3 Y3 S8 i; B$ P
if (errorcode != grOk) /* an error occurred */ ( x' W' l' g' C: M4 g! E5 }
{
( p/ V9 a. w# B8 O6 ?printf("Graphics error: %s\n", grapherrormsg(errorcode)); % |1 L) V' q% O9 a, `
printf("Press any key to halt:"); % G: k; N3 ~" y o
getch();
; k; c$ a7 K# z( Eexit(1); /* terminate with an error code */ 0 K* j( s. W/ D) d
} </FONT></P>- E) S: q$ `- Y/ u' `3 X4 S& I7 k
<P><FONT color=#0000ff>maxcolor = getmaxcolor(); 5 v0 b- v! L, m8 o
ht = 2 * textheight("W"); </FONT></P>4 p E/ k. s7 n8 W* d7 H
<P><FONT color=#0000ff>/* grab a copy of the palette */
9 ~, w! |6 ]' f+ v* P6 {% a3 b7 qgetpalette(&pal); </FONT></P>
4 O H. a9 X* }<P><FONT color=#0000ff>/* display the default palette colors */ " o+ m5 @ v* {9 K& I9 k
for (color=1; color<=maxcolor; color++)
1 ?( D. ?* H& o& f{
5 d) e$ i, ` }- R% Rsetcolor(color);
4 H4 G6 I& T0 N$ Isprintf(msg, "Color: %d", color); 6 s5 P; |( p3 X- b# _
outtextxy(1, y, msg);
, m1 ~% K0 J1 U+ |+ e. \y += ht; " p8 g9 A( E% ]" t
} </FONT></P>3 B m6 c& `( i
<P><FONT color=#0000ff>/* wait for a key */ 0 D9 L% q* N1 h2 t; U4 S8 R9 S
getch(); </FONT></P>
( c3 h* g G+ y6 ~& ]2 T<P><FONT color=#0000ff>/* black out the colors one by one */ 7 y+ A4 D" S3 G. w; }$ M% [2 m
for (color=1; color<=maxcolor; color++) / B$ \2 N0 U9 t, S% h% p
{ ' n1 o; c8 E2 g& U3 i7 q6 ]
setpalette(color, BLACK);
5 q9 g+ g; h0 g( u2 C: Pgetch(); 9 N& ~& [' I' B, Z$ _ b- f
} </FONT></P>
7 ^* {# g1 }" k2 f1 P' j+ O<P><FONT color=#0000ff>/* restore the palette colors */ ! w$ R" ?# ^! e8 U$ e" ^! w! k% @
setallpalette(&pal); </FONT></P>
1 ]9 |7 l9 I+ W, m2 M0 }' \7 M<P><FONT color=#0000ff>/* clean up */
# A( q$ K' T8 ]getch(); ! H: u% q6 s& Z; \8 ]9 X0 k
closegraph(); 3 p) ]; s# s; k2 d! ?: q$ j
return 0;
' M* q5 }+ W% \& J}
' w) m7 c% a; h% j( `# Q( F) q' h3 F0 X/ z5 O, N5 s
</FONT></P>* t" y6 J& ?: P+ F# d' n+ p
<P><FONT color=#ff0000>函数名: setaspectratio </FONT>
; U8 O* t2 r+ _6 h8 Q* z- p功 能: 设置图形纵横比
4 b( m# X/ q: f7 b9 J l用 法: void far setaspectratio(int xasp, int yasp);
. a E$ J5 |+ N6 h, r [程序例: </P>7 U2 |* Z! x. W" D( }% c( \
<P><FONT color=#0000ff>#include <GRAPHICS.H>
2 Y8 D! B+ X. U3 X#include <STDLIB.H>2 g/ e7 Y O4 v6 o; w9 u
#include <STDIO.H>
+ e/ C) \! Q* P9 f/ b#include <CONIO.H></FONT></P> ]. `. P, o. L' ]3 }1 @
<P><FONT color=#0000ff>int main(void)
, U$ t8 Q/ C/ D9 u+ m" d2 Y- ^{ . K" j& I$ [% @) M
/* request auto detection */ & I/ a& y% }$ g: `- o
int gdriver = DETECT, gmode, errorcode;
- j1 n3 o! `( Jint xasp, yasp, midx, midy; </FONT></P>
% |$ l; [$ f( c0 J2 O, Q<P><FONT color=#0000ff>/* initialize graphics and local variables */
9 T$ U' z6 m+ ]+ P7 Z- \initgraph(&gdriver, &gmode, ""); </FONT></P>
a4 A6 j* q! n5 W3 |<P><FONT color=#0000ff>/* read result of initialization */
* m% |/ F0 t/ f5 c; v7 S" r& v5 Yerrorcode = graphresult();
4 |& g3 Y# b1 uif (errorcode != grOk) /* an error occurred */
5 t7 w. m* q' L: P- T) l{ 0 i: S! U+ l, w. A5 O }
printf("Graphics error: %s\n", grapherrormsg(errorcode)); % W$ P# w! `% W1 c$ w0 d
printf("Press any key to halt:"); ' r0 j [. P6 q, ^9 F* P( E. O
getch(); 7 S2 R4 o& g( v) N6 j% p
exit(1); /* terminate with an error code */ % W. N1 g7 {% W6 [$ G- k. ], [9 Y6 G
} </FONT></P>
. c2 u v8 Q7 s3 O& h; k3 [<P><FONT color=#0000ff>midx = getmaxx() / 2; / o. p4 p w# M% R: E
midy = getmaxy() / 2;
9 P R2 \- ^, V+ a* ?' {. Y9 p1 Rsetcolor(getmaxcolor()); </FONT></P>6 G3 s! g+ m2 M) D- r! {" c
<P><FONT color=#0000ff>/* get current aspect ratio settings */ & e* B) h G0 Z/ X% z* \4 _( }3 [
getaspectratio(&xasp, &yasp); </FONT></P>
: S1 P' F' ^- o! U2 E$ O<P><FONT color=#0000ff>/* draw normal circle */
0 D* Y0 x/ H% a2 P' v2 ~circle(midx, midy, 100); : `; J- | @, x$ [1 i" {5 X( _& ~
getch(); </FONT></P>' `& A9 T1 |9 k( ?9 n4 H5 u( L4 m4 S
<P><FONT color=#0000ff>/* claer the screen */ ; D% R' Q1 c/ o0 M/ h
cleardevice(); </FONT></P>
5 _1 \8 v, u! o* {<P><FONT color=#0000ff>/* adjust the aspect for a wide circle */
8 T* D5 z% c; l6 Y$ Q/ \setaspectratio(xasp/2, yasp);
}! a) ^3 P3 A. J4 d" ^& p% A: Zcircle(midx, midy, 100);
2 u& H' C4 x, d! ]" \1 hgetch(); </FONT></P>
) u& M0 r5 [8 @' q8 E<P><FONT color=#0000ff>/* adjust the aspect for a narrow circle */
: N3 ~2 ^3 s5 L$ y1 @* L, Tcleardevice(); % [: [+ F( ~! r! J
setaspectratio(xasp, yasp/2);
Y Z3 H' `% Y, X9 v/ vcircle(midx, midy, 100); </FONT></P>
]% ~. r+ |, x& }* A# L" e<P><FONT color=#0000ff>/* clean up */ 9 H6 t& d' R9 F& Y3 J0 L
getch();
6 c% p( N. U0 F' _closegraph();
6 u$ T! o Q! F$ g" G- Creturn 0;
9 S' E& _2 @4 c, r$ Z+ j) S} 6 J2 @) u, i5 j
</FONT>( G, v' K% m+ y% k( m# t( E4 b
</P>3 |6 l4 P( t6 B: y0 N
<P><FONT color=#ff0000>函数名: setbkcolor </FONT>2 c# g" m& h( b f$ \
功 能: 用调色板设置当前背景颜色
) k8 m4 o/ X6 z; M% g用 法: void far setbkcolor(int color); 2 z( G% l6 k8 ?
程序例: </P>! n7 h3 f! O$ Y. d; U* B
<P><FONT color=#0000ff>#include <GRAPHICS.H>( k8 t* q ^& D& Y8 G/ O" B+ a
#include <STDLIB.H>/ [" A- F8 ?* d* w
#include <STDIO.H>
7 i4 [* e5 l1 q% g% V& _+ X#include <CONIO.H></FONT></P>- E* a% q( U$ [/ W+ S t
<P><FONT color=#0000ff>int main(void)
# B& K: e/ j3 _{
( i' F6 L. P! \" O2 S) E7 y& k/* select a driver and mode that supports */
8 [4 ]) R. C, e" ?% [/* multiple background colors. */
' T& D# s+ O! s, J* m# rint gdriver = EGA, gmode = EGAHI, errorcode; + Q; H9 x0 t% T" `/ L5 ^& m; K7 @
int bkcol, maxcolor, x, y;
( Q; M% ]2 J. k6 t6 @2 b7 ~& achar msg[80]; </FONT></P>) T8 ~2 j! g3 ]# n+ |; |
<P><FONT color=#0000ff>/* initialize graphics and local variables */
" w. g$ x/ \4 E4 _+ I& x/ C9 zinitgraph(&gdriver, &gmode, ""); </FONT></P>3 l0 v1 W) U6 i T* e
<P><FONT color=#0000ff>/* read result of initialization */ 6 R- C6 K$ Q; G
errorcode = graphresult();
" V* {% w5 ]. v- e' Q0 z9 j/ ~if (errorcode != grOk) /* an error occurred */
6 q" K8 Q1 @3 h2 L% N9 @6 n8 x+ H{
' U3 J8 z" z# dprintf("Graphics error: %s\n", grapherrormsg(errorcode));
7 _5 q/ ^: { ~9 G- w# D; |0 Xprintf("Press any key to halt:"); . u9 F* d0 W! w2 E% y- F
getch();
" R1 G5 q, s1 { pexit(1); /* terminate with an error code */
1 K$ i0 U* k4 r* {6 H) I3 W} </FONT></P>
; a2 m) b% J, A) {, I: e5 p<P><FONT color=#0000ff>/* maximum color index supported */ 2 H( b9 x) {0 |6 F3 s
maxcolor = getmaxcolor(); </FONT></P>9 P3 u% B/ P# ]& }. [
<P><FONT color=#0000ff>/* for centering text messages */
U7 B& [* ~. d& C Msettextjustify(CENTER_TEXT, CENTER_TEXT);
$ P! x! l9 R1 d- [9 q7 yx = getmaxx() / 2; 5 O& J/ h( T( Z- A3 Z" L5 {
y = getmaxy() / 2; </FONT></P>
' k, K, M. h& q3 n: i @<P><FONT color=#0000ff>/* loop through the available colors */ * J* }. e9 X2 d( u5 g7 E* R9 l
for (bkcol=0; bkcol<=maxcolor; bkcol++)
9 X0 z6 s* `( c{
5 `! f$ C4 r; Z* w/* clear the screen */
8 }( u7 g7 s& Y4 Ncleardevice(); </FONT></P>) [! ~ V: M, z1 a2 x4 y5 t* ]/ t
<P><FONT color=#0000ff>/* select a new background color */
& _7 F. _$ h3 f! |) Dsetbkcolor(bkcol); </FONT></P>
' W2 y7 S% d) l0 e' T+ e! l4 t W<P><FONT color=#0000ff>/* output a messsage */
7 x7 C( ?/ t! G6 o0 N& Aif (bkcol == WHITE) : e, J- \% a; d( Z; e& o, Q
setcolor(EGA_BLUE); ; V7 ? o. c7 ~& Z* `
sprintf(msg, "Background color: %d", bkcol); ; W" S1 Z2 }! L) s) ^
outtextxy(x, y, msg);
2 b; o5 S J+ v8 Zgetch(); 6 r( [& F0 g" v: Q
} </FONT></P>8 L: X0 y/ [ M) n6 |
<P><FONT color=#0000ff>/* clean up */ 6 l; a8 _7 {/ k% r. S$ A9 Q# F
closegraph();
9 U8 W. W8 a4 b7 B) R Jreturn 0;
* R7 O7 G$ r# t7 m} </FONT>7 ?+ k4 b: `9 Q5 `$ {! Z* }. B3 Y1 I
9 {$ r1 u' A( R* {. S</P>
# L$ e7 g" e/ s& \<P><FONT color=#ff0000>函数名: setblock </FONT>* f0 U) L9 g6 L# X1 E* D
功 能: 修改先前已分配的DOS存储段大小 5 R# J6 Y0 @1 B+ V* k
用 法: int setblock(int seg, int newsize); 3 s% Q7 v. O( @+ T/ I
程序例: </P>
3 g) d( z& D2 l. }, h1 {& J7 l<P><FONT color=#0000ff>#include <DOS.H>
5 C1 \4 @) ~+ M5 ~+ E$ o#include <ALLOC.H>
$ t* j3 ?' E3 L5 @6 z9 s6 u. x#include <STDIO.H>+ R i2 @- F% _0 t4 s [! x
#include <STDLIB.H></FONT></P>% ? C: \8 I; ~6 s6 f7 |
<P><FONT color=#0000ff>int main(void) 9 K0 r1 w2 S7 |2 z9 q& I: r; y
{ 1 Q2 d5 A$ |3 R+ Z# L$ Q# ]
unsigned int size, segp; " {) \8 M6 d' p! Z0 U7 S. x' V, a
int stat; </FONT></P>0 N* j8 X S9 o8 x* [
<P><FONT color=#0000ff>size = 64; /* (64 x 16) = 1024 bytes */ , F; p; z' _% ]0 C
stat = allocmem(size, &segp); , o: e" W5 X% W) M
if (stat == -1) 0 b, N# p, |: \8 ?, Z9 g
printf("Allocated memory at segment: %X\n", segp); & N) j8 }& P6 K
else ) m9 t) X* |# |% h* X' g
{
' Z! v3 V8 V! P& R/ U. o0 Bprintf("Failed: maximum number of paragraphs available is %d\n",
: b, k% |, [; ~stat); 1 b8 g- q$ Q( ]
exit(1); $ [( [; J- T' @
} </FONT></P>
4 X; C$ U% {- e5 l" z. A<P><FONT color=#0000ff>stat = setblock(segp, size * 2); 7 I+ c; A6 D- n6 Z: N% V
if (stat == -1)
+ p0 d+ e9 b0 [printf("Expanded memory block at segment: %X\n", segp); . E. ~" g+ p$ x9 u: U% g3 [
else , n& g& @! V, |) A5 `) ~/ v
printf("Failed: maximum number of paragraphs available is %d\n", 8 K7 G3 E. k; ^: @- u1 u2 f
stat); </FONT></P>- G9 h0 A \7 K9 d a
<P><FONT color=#0000ff>freemem(segp); </FONT></P>
% G) x1 _, U8 V! i2 s2 K+ U- B<P><FONT color=#0000ff>return 0; . ?2 c4 H. q8 @
}
2 ]+ n+ O0 E3 ?6 q</FONT>
* u; k) E! M s</P>
0 I$ \3 r9 }8 t; l: L2 W* H6 ]<P><FONT color=#ff0000>函数名: setbuf </FONT>, A# h6 P3 Z3 R; d' I$ S
功 能: 把缓冲区与流相联
6 I3 o n; S& Q: M& A/ D用 法: void setbuf(FILE *steam, char *buf); ( c' m5 a! o: }- D9 |0 p
程序例: </P>
2 F; p- X7 ?6 m, U' _% P<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
: ]$ _. G( w7 A+ W0 K4 `<P><FONT color=#0000ff>/* BUFSIZ is defined in stdio.h */ 9 j4 s5 p. r ] h3 s
char outbuf[BUFSIZ]; </FONT></P>9 V, [, s/ T7 j, d% {8 H5 L
<P><FONT color=#0000ff>int main(void) 9 J& z6 G$ ?! m! t
{ . F; H4 L7 j7 j$ h- e1 j8 }0 Q
/* attach a buffer to the standard output stream */ 3 Q3 w: Q* {& z: [
setbuf(stdout, outbuf); </FONT></P>
0 |. p. Y+ X$ |+ g<P><FONT color=#0000ff>/* put some characters into the buffer */
& o: T& l# `' A6 n( I% S" V8 F; iputs("This is a test of buffered output.\n\n"); ; C* C% O" o3 {& M5 u' y
puts("This output will go into outbuf\n");
% N, w0 m5 I9 u* Q% K* Iputs("and won't appear until the buffer\n"); 6 E+ q6 B v+ R/ |) w
puts("fills up or we flush the stream.\n"); </FONT></P>
3 D7 P. b% O# I, f! \7 E. `<P><FONT color=#0000ff>/* flush the output buffer */ ' o$ [4 Z) z' V# S% Y0 Q
fflush(stdout); </FONT></P>
1 R' E& G1 W9 X* u6 A4 ?' v \ |<P><FONT color=#0000ff>return 0;
5 ~/ d. D# k5 n$ e2 x% S}
7 c [3 K5 D* j$ @0 A/ P</FONT>+ a! X; ?9 n# _( _& n
</P>& U$ E& |, t0 n! A5 \ |
<P><FONT color=#ff0000>函数名: setcbrk</FONT> 2 ?. _2 f! T3 ^* h, G% U
功 能: 设置Control-break , Q V( X6 I* F- D5 x' y6 Q, c4 |
用 法: int setcbrk(int value); 3 z7 |! ?3 x7 g) i8 _( V
程序例: </P>
8 _: ^+ K9 D- |) y; C; h<P><FONT color=#0000ff>#include <DOS.H>$ `5 N* ~/ Q3 v8 n1 a, l+ t; b
#include <CONIO.H>
7 e1 q& C9 x; D% R1 u) [#include <STDIO.H></FONT></P>
9 @! K: z2 ~/ j) \<P><FONT color=#0000ff>int main(void)
, }7 w. A/ ^/ D5 U{
5 I3 G- a a; I. b" Yint break_flag; </FONT></P>
2 y) c2 k' N& U8 Y- \6 d<P><FONT color=#0000ff>printf("Enter 0 to turn control break off\n"); ; v" V; H/ d9 J0 J% a
printf("Enter 1 to turn control break on\n"); </FONT></P>
% n$ J1 z6 P9 {5 M: d) b2 Z<P><FONT color=#0000ff>break_flag = getch() - 0; </FONT></P>% r2 ], Q# Y& N1 w \, V/ [
<P><FONT color=#0000ff>setcbrk(break_flag); </FONT></P>) B- n6 s# |1 ^$ Y F- W K u2 O6 j
<P><FONT color=#0000ff>if (getcbrk())
; e* N/ e! h9 k1 r0 ]& W) O( ^6 rprintf("Cntrl-brk flag is on\n"); ]' ~+ ]* k( C$ N8 ~
else $ l1 ]- ?- h/ t. |9 o: |/ D
printf("Cntrl-brk flag is off\n");
* A2 ` X; _3 B: {+ n1 l4 S9 Jreturn 0; + n+ e4 Q5 l* K' B
} 5 v7 ~) i% R; O# H/ U, @
# ~3 T4 Y+ H: ~4 y4 R- A0 z2 G</FONT>
( u, ?8 r* A6 ^/ N* y# D& G4 b5 K# B0 }</P>
( i( }- _$ }! Y* y c# s<P><FONT color=#ff0000>函数名: setcolor</FONT>
" x8 n# W5 B4 L( o5 _* O功 能: 设置当前画线颜色 7 z/ L" z7 C# V3 o
用 法: void far setcolor(int color);
1 U( R- A4 B. p/ A C3 X: n程序例: </P>. n6 _ _- c8 k: j
<P><FONT color=#0000ff>#include <GRAPHICS.H>" f- z2 ^# R6 U" P
#include <STDLIB.H>/ A" C" Y1 F1 F3 @
#include <STDIO.H>; T B4 q. h5 e; |
#include <CONIO.H></FONT></P>
# r4 y* l) P) {8 K, B4 q8 A3 `6 x<P><FONT color=#0000ff>int main(void) 9 Y, {% h4 Z8 ]- P$ B
{
/ v* k: u3 Y' P7 g6 f2 v8 L! q' W/* select a driver and mode that supports */
. X& B: z* ~$ {8 i# U# Y/* multiple drawing colors. */
1 D/ \0 ~, }8 ^$ w/ }' {int gdriver = EGA, gmode = EGAHI, errorcode; $ ~" Y z- r5 k: D% f* x2 T. [
int color, maxcolor, x, y;
& R9 |8 ~$ H0 o. Kchar msg[80]; </FONT></P>0 H$ A* w) q9 {9 M
<P><FONT color=#0000ff>/* initialize graphics and local variables */
' H' ?1 j+ z4 \- D1 Binitgraph(&gdriver, &gmode, ""); </FONT></P>
5 A7 Y- Y( A% V/ i<P><FONT color=#0000ff>/* read result of initialization */
) O% @% Y5 }: M2 W) c Cerrorcode = graphresult();
3 R. e( Q, X B& k! m! Z! W+ ]" mif (errorcode != grOk) /* an error occurred */
& c6 K6 u3 A# i{
$ i0 a( R0 n# y4 Bprintf("Graphics error: %s\n", grapherrormsg(errorcode));
# Z& a }5 z+ _1 G) Gprintf("Press any key to halt:"); $ h0 O' C L4 X1 l* \( T
getch();
7 I- d$ j+ K+ k e3 N3 eexit(1); /* terminate with an error code */ 4 @6 d; S- S$ O, o& h% A4 t
} </FONT></P>: `3 B% S) N- Q& ~1 f% a' ]- W! I
<P><FONT color=#0000ff>/* maximum color index supported */
F7 B0 @) x% w; j/ p4 {* umaxcolor = getmaxcolor(); </FONT></P>
7 \; z: \. ~5 p! w8 ?& W" O1 a<P><FONT color=#0000ff>/* for centering text messages */ . _6 p8 c* { P7 c: [3 @
settextjustify(CENTER_TEXT, CENTER_TEXT);
% Z C$ x: L1 X6 H# l7 Ax = getmaxx() / 2;
# G/ |' y! \% Gy = getmaxy() / 2; </FONT></P>
( S* c/ W u; e4 @<P><FONT color=#0000ff>/* loop through the available colors */ . o; e4 C2 c5 C& c$ i3 s# x% E
for (color=1; color<=maxcolor; color++)
0 w* R2 n$ k* e; x: A3 p8 \# c! K{
9 C* M( L1 J% a, M' B1 g) n/* clear the screen */ ) N4 J5 h1 s4 W) |; }: X7 R% y
cleardevice(); </FONT></P>
4 i+ Z* F$ @3 @- E<P><FONT color=#0000ff>/* select a new background color */
7 n+ d8 }% b$ ^setcolor(color); </FONT></P>
$ g- s: _6 k' H/ D1 G' }<P><FONT color=#0000ff>/* output a messsage */
: s. {5 Y' f7 E: g" a; asprintf(msg, "Color: %d", color);
& Y6 u4 @4 P0 G& Souttextxy(x, y, msg);
8 k5 b* b( C4 k& w3 bgetch();
# v7 P# T9 B7 J} </FONT></P>. v6 Y, R H2 l5 J5 Q( K
<P><FONT color=#0000ff>/* clean up */ ( ?1 s! q( H* E' [" @
closegraph(); 1 @: n; Q1 ]. h( {. C6 a( D" m
return 0; 9 E+ `4 J: k. j
} ' m) ` {7 i* D5 x+ ` @
</FONT>- `* ^9 M' h$ A2 \
</P>% x. j7 V, ~8 f* F6 b
<P><FONT color=#ff0000>函数名: setdate </FONT>; u' s' O1 P7 _( l2 y6 V
功 能: 设置DOS日期 # N7 u) q1 S4 G- \6 t8 |
用 法: void setdate(struct date *dateblk);
: R8 L0 k* Z/ P( d! L% ~程序例: </P>( i3 k. q( X$ u
<P><FONT color=#0000ff>#include <STDIO.H>: F' ~$ e) H; w2 i X% V b8 i
#include <PROCESS.H>
5 h2 f* `& Z% Y: \7 b' n6 s#include <DOS.H></FONT></P>3 p4 o5 h8 O, M: r8 @6 D
<P><FONT color=#0000ff>int main(void) , a4 i7 [; {+ C0 _( x0 Z
{ 5 b: f3 c3 A2 p! c u7 P+ \- |
struct date reset; ( X' V# o7 J1 i/ s( w" c; m' N
struct date save_date; </FONT></P>
( n7 D! G8 a5 A1 A, n3 N2 B<P><FONT color=#0000ff>getdate(&save_date); 0 Y1 Z# a) W3 L8 D7 ]; E r
printf("Original date:\n"); 4 I) |7 t4 T! {
system("date"); </FONT></P>
2 i4 r! P8 I% S; Z# z<P><FONT color=#0000ff>reset.da_year = 2001;
f1 N( ~: Q. \reset.da_day = 1; % F1 d8 B& R- F# r: L8 `. ]# ^% j% Q
reset.da_mon = 1;
" ?% Z4 ^ a Q3 S; Wsetdate(&reset); </FONT></P>& K' `$ c8 J4 P* }) T# r( p
<P><FONT color=#0000ff>printf("Date after setting:\n");
1 u0 ^2 R- W) x2 p% | tsystem("date"); </FONT></P>- V, u, d6 h2 m4 x8 j7 P
<P><FONT color=#0000ff>setdate(&save_date);
9 ?9 e& I$ m3 v! l D! @& [printf("Back to original date:\n");
2 Y6 ~, ~/ H* psystem("date"); </FONT></P>7 v- x5 m" J* V
<P><FONT color=#0000ff>return 0; + j7 ~0 n' |2 k
} 1 l; I" P$ E c. Y! X' V
</FONT>
* ]3 ~' i0 h) h# l- K</P>
8 W( ^0 I ~ I2 K<P><FONT color=#ff0000>函数名: setdisk </FONT>4 h& x, M% @8 e! L S N) l; a7 G! G
功 能: 设置当前磁盘驱动器
+ s$ x* K8 g% j$ \2 F+ k用 法: int setdisk(int drive); ' n! L' ^2 M# V; z+ q) D
程序例: </P>
) `0 {! \2 m$ y/ m* y<P><FONT color=#0000ff>#include <STDIO.H>
8 i9 b7 }2 E$ Q( S" h1 b#include <DIR.H></FONT></P>9 M" V9 r3 Y( V0 w
<P><FONT color=#0000ff>int main(void)
& a# j' E: H$ j# S1 N{ % n8 F8 U' I- H p5 E
int save, disk, disks; </FONT></P>
- U' m0 | I2 S<P><FONT color=#0000ff>/* save original drive */ $ @1 V, m! j% i$ W/ p
save = getdisk(); </FONT></P>
+ a& K6 F1 ^. a1 x1 k; O4 {9 r# ^' U<P><FONT color=#0000ff>/* print number of logic drives */ 1 O/ m+ F8 B* q; Q
disks = setdisk(save);
; {0 m( @- p- q# _- \2 \% Cprintf("%d logical drives on the system\n\n", disks); </FONT></P>
' s6 X$ ^6 ]" |& U8 K<P><FONT color=#0000ff>/* print the drive letters available */
# r+ a+ ?, ^9 Jprintf("Available drives:\n");
! R. M/ V7 e P" n. qfor (disk = 0;disk < 26;++disk)
' ?7 G) {9 R9 E{ j# Y* X9 ~. b7 h
setdisk(disk); 2 _9 U0 L" k5 E5 d/ i* |5 ?& Y' i, K
if (disk == getdisk())
( u" L& y! Q2 v' m2 P* `printf("%c: drive is available\n", disk + 'a'); . ]/ N4 |. H5 Z1 K) D5 u
}
( R0 F9 m2 t3 Q2 _setdisk(save); </FONT></P>
) X" c5 C; S3 m1 Q/ x<P><FONT color=#0000ff>return 0; ! D+ A9 n1 c1 j9 `# a# N
} </FONT>. ?) e$ U7 h5 i1 h* J7 U
# n; b/ k* w! L/ l6 j3 X6 r- w" T2 z
</P>
& Q6 N" P1 y' g) F4 K) v' p& [<P><FONT color=#ff0000>函数名: setdta </FONT>
! F; O% I6 K# [) O功 能: 设置磁盘传输区地址 9 t! r% E5 x$ }6 ^+ @+ I
用 法: void setdta(char far *dta); 9 r1 A" r9 }, I! R
程序例: </P>8 O1 q! R5 X) @6 y( C' s) `
<P><FONT color=#0000ff>#include <PROCESS.H>
! G8 S& d" b8 \+ O! {- F; g- Z#include <STRING.H>! {" E7 V% ^" d' Y% @
#include <STDIO.H>
, h+ j2 x$ Q' G#include <DOS.H></FONT></P>
/ V9 p7 J( c+ n9 I8 |<P><FONT color=#0000ff>int main(void)
" P+ @) v8 n9 ^, D2 } y{ 0 ~ c: H2 K6 A
char line[80], far *save_dta; 4 u0 w& Y5 L0 R
char buffer[256] = "SETDTA test!";
& f# k# \) Y, S* Dstruct fcb blk; 5 m) x8 f; H! I" ?0 x
int result; </FONT></P>, g S9 t6 N$ z/ A; i
<P><FONT color=#0000ff>/* get new file name from user */ & W9 L; G' T+ y9 P. s
printf("Enter a file name to create:");
! \7 s7 s4 T! @, u" h- Egets(line); </FONT></P>9 E! o, k; w: n7 Y' A& j0 X
<P><FONT color=#0000ff>/* parse the new file name to the dta */ 7 T4 M& L) r9 f h# _
parsfnm(line, &blk, 1);
0 C. h$ M2 i& v& S8 M0 N$ C/ Aprintf("%d %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>
6 L6 e3 _+ p% |' S) E( }! s' O<P><FONT color=#0000ff>/* request DOS services to create file */
: L& W' O# F- M! Wif (bdosptr(0x16, &blk, 0) == -1) & h; ?3 z3 M+ [3 v5 q( L4 r( M
{
" q% p0 I2 Y9 R* N% Q# c$ cperror("Error creating file");
& [1 f) v- q, [3 N3 D6 m& j% _exit(1); 6 N1 L V' v; ]% I: d
} </FONT></P>) [8 |* |0 r+ i4 f* s
<P><FONT color=#0000ff>/* save old dta and set new dta */
& Y# |) r! {( y- ]3 Y9 N7 H+ csave_dta = getdta(); + ^1 N7 B& _) d# X* Q2 J
setdta(buffer); </FONT></P>
* u6 i4 n6 ?8 U4 V) F<P><FONT color=#0000ff>/* write new records */ 0 D9 {$ n3 A, {# U' p+ O
blk.fcb_recsize = 256;
9 m3 h: N) [ t8 v" @blk.fcb_random = 0L; ' L( q6 Q! U* F9 ]# {8 O) [
result = randbwr(&blk, 1);
) { p+ ~: `4 [1 q& X/ ]( Zprintf("result = %d\n", result); </FONT></P>- s, R7 F0 a0 A% N, k! W: P: Z
<P><FONT color=#0000ff>if (!result)
, j; f1 N3 q& n: J! Z1 H& M3 v sprintf("Write OK\n"); 9 G. J) T; p8 y L$ ~5 i
else
# H: i- e1 @; v- f( T# d$ p{ - p4 J0 R9 o( M' U) C( Q( `
perror("Disk error"); + F7 @+ |( r' M n6 Y2 \* |
exit(1);
3 Q4 T5 ?- }3 t3 R/ N} </FONT></P>! o7 j: O) D: J) R* f% L4 ?
<P><FONT color=#0000ff>/* request DOS services to close the file */ - n7 b \$ a( c* b
if (bdosptr(0x10, &blk, 0) == -1) 8 j' s9 v! v6 O! y& m7 D
{ - F! H) v6 Y/ M2 n
perror("Error closing file"); # W% T9 c8 y: V& w; f
exit(1);
3 R9 f" D4 y+ X: O( @8 s} </FONT></P>
8 w% m+ I4 V. n& X- O<P><FONT color=#0000ff>/* reset the old dta */
; L; G T- P6 z! O! e- Q2 s+ tsetdta(save_dta);
# m r6 a8 A& w1 {. h8 C2 [return 0;
% j) N5 I, Q6 h}
+ Q9 y1 M8 i2 @/ v1 z0 z</FONT>
: `4 T" W- g% M _( t% J( u' W</P>3 C1 U9 u* d/ ]. n _
<P><FONT color=#ff0000>函数名: setfillpattern </FONT># M6 Z3 x( f% [' E3 l
功 能: 选择用户定义的填充模式
. f( }7 w# O' t用 法: void far setfillpattern(char far *upattern, int color); - i+ p" z: N1 l; {+ B. p
程序例: </P>. {$ }6 b3 X% g% w9 Y
<P><FONT color=#0000ff>#include <GRAPHICS.H>' @. z; L7 v( U% Y0 U t
#include <STDLIB.H>2 d i+ {4 G* s9 r
#include <STDIO.H>: b5 b( P% ~; z: d
#include <CONIO.H></FONT></P>6 a u( a8 Z( ]
<P><FONT color=#0000ff>int main(void) 8 D8 P) Q0 ]1 G- ?; [
{
) [3 ~4 n' a! X8 H! A9 O1 E( s0 U4 I/* request auto detection */ ! C# I' \3 M1 k; F6 p
int gdriver = DETECT, gmode, errorcode;
& D% [5 A2 T4 X+ R- H2 y6 V; Cint maxx, maxy; </FONT></P> y; u6 I. G' a
<P><FONT color=#0000ff>/* a user defined fill pattern */ & ]3 ~/ Q8 n; M! R
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00}; </FONT></P>9 q( h) A+ A7 p" c, r# H# w: R
<P><FONT color=#0000ff>/* initialize graphics and local variables */
! Q0 L8 ] H# }: S& W* j, @+ G3 `, }initgraph(&gdriver, &gmode, ""); </FONT></P>. p# r3 N3 }9 O/ g
<P><FONT color=#0000ff>/* read result of initialization */
$ |- Y! k" b: ^errorcode = graphresult();
. z* z5 i3 s* x: i( Cif (errorcode != grOk) /* an error occurred */ 3 Y3 U; m: h2 g* `! d7 u4 D
{
: C/ y: T# V9 u2 x7 Mprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ' N$ q. k) C5 Q3 ^6 i5 e7 A
printf("Press any key to halt:"); ; P$ ^4 K: b6 e/ B% R7 W6 x
getch();
, a4 l! ^* ]1 Xexit(1); /* terminate with an error code */ ; ^- y7 u. P }, o7 |
} </FONT></P>
. v. @. V; [2 \0 e<P><FONT color=#0000ff>maxx = getmaxx(); L* T' k& O+ h
maxy = getmaxy(); $ M! K7 D4 X4 t+ K! A6 `
setcolor(getmaxcolor()); </FONT></P>6 L7 X, l+ r8 c. u7 h% ^+ l
<P><FONT color=#0000ff>/* select a user defined fill pattern */ 0 c0 R6 z/ n5 s, @
setfillpattern(pattern, getmaxcolor()); </FONT></P>6 R) u2 T) v7 B& H# m$ R
<P><FONT color=#0000ff>/* fill the screen with the pattern */ . S$ E7 e3 t. N
bar(0, 0, maxx, maxy); </FONT></P>. [; |1 T2 X; f4 b
<P><FONT color=#0000ff>/* clean up */ + P+ A* `+ P1 W$ Q0 W" K* s: c$ R
getch();
+ s+ h+ v" E$ z) u7 o6 X' }closegraph();
$ f; Y2 k) B& U5 ireturn 0;
% w% ]- m+ b6 Q4 E( W/ @2 F4 A6 V} ' Y5 O2 c/ E9 M3 c, ?0 a$ C
</FONT>
- Y8 @* l! W0 P</P>2 D, \ f: F5 T- s$ j7 N
<P><FONT color=#ff0000>函数名: setfillstyle </FONT>8 N2 N& X/ J4 g( i; p) Y0 w! `
功 能: 设置填充模式和颜色 7 F6 g& h: f0 G. p
用 法: void far setfillstyle(int pattern, int color);
0 n1 M4 { Y* h$ C: ~4 t+ E8 K程序例: </P>
) c9 c( C6 L& \: [8 i, y% Q2 s<P><FONT color=#0000ff>#include <GRAPHICS.H>2 b4 ~9 C h9 h7 i" M
#include <STDLIB.H>
0 F5 G/ L, u5 Q6 n9 Y8 i: D8 h#include <STRING.H>; X# C9 s- d8 V5 X5 K% b* g: Y1 Q9 e; W) h
#include <STDIO.H>
; r3 R1 C% q2 G0 y* d#include <CONIO.H></FONT></P>
, r/ S# W# ~& z+ T" y<P><FONT color=#0000ff>/* the names of the fill styles supported */ 1 ?( e; Q6 f" V: D5 T
char *fname[] = { "EMPTY_FILL",
+ P/ o$ I! H! [( `/ m, h/ g, D"SOLID_FILL",
, _$ D( ]# ?' @8 W"LINE_FILL",
) H8 w7 W8 f' k: v"LTSLASH_FILL", " j3 K, ~& S& U
"SLASH_FILL", , B6 j+ d* K% c. i+ T, g7 y$ f3 Q
"BKSLASH_FILL",
$ M, _# X9 E5 [6 u1 c5 }"LTBKSLASH_FILL",
* i: w& I, b4 R, A c* k. Z"HATCH_FILL", 4 `; F0 w2 n: J
"XHATCH_FILL", 7 M" `) g% W. A: }3 J
"INTERLEAVE_FILL",
- `; n$ q- A- }4 k: o0 J"WIDE_DOT_FILL",
( l( W% ]/ }' M4 e8 Y"CLOSE_DOT_FILL",
* @2 | u7 |5 T' f* c7 c- J6 W- {"USER_FILL"
2 F2 @! J2 u, O, i! s) {}; </FONT></P>
& f F5 V8 m$ C0 R<P><FONT color=#0000ff>int main(void)
$ y& L+ s6 B6 w0 ^1 v6 Y{
3 R3 Q4 ?% S/ T: T) R* t; ]/* request auto detection */
* n' p( Y7 n# R0 r3 oint gdriver = DETECT, gmode, errorcode;
6 d% E8 I/ F8 @9 G' t3 J/ H, Iint style, midx, midy;
' d1 i7 U' ~* H: a& @char stylestr[40]; </FONT></P>
$ @, g6 G# }! G9 h* ?: u5 J<P><FONT color=#0000ff>/* initialize graphics and local variables */ * d/ _3 e7 e# A- Q2 J$ ?" f/ A
initgraph(&gdriver, &gmode, ""); </FONT></P>
( z! b! R9 m, M" N" g<P><FONT color=#0000ff>/* read result of initialization */
" W" I' E8 y- cerrorcode = graphresult(); - \7 p% U/ |! e) f3 O. k
if (errorcode != grOk) /* an error occurred */ , `( Q, i f8 [6 z) p
{ + \, K& \7 A8 {( S4 A) c8 k
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 7 }+ e' a5 x/ ~! e- a
printf("Press any key to halt:"); 9 I0 Z7 f: y1 L8 w
getch(); ' ? X: [, v% N9 g' ?: R& k
exit(1); /* terminate with an error code */ ( V1 @ [4 r. T* a
} </FONT></P>6 w0 ~0 K; b" g, v) E
<P><FONT color=#0000ff>midx = getmaxx() / 2; I1 r. M3 M( T( B$ U/ I
midy = getmaxy() / 2; </FONT></P>
: }' k; e; @6 o) ?) G# s# w<P><FONT color=#0000ff>for (style = EMPTY_FILL; style < USER_FILL; style++) I( V- R9 ?0 C% I0 y: ?" M
{ 8 {5 V3 {7 P8 i8 s: e8 {! t
/* select the fill style */ 6 z; d' y; K; k0 a
setfillstyle(style, getmaxcolor()); </FONT></P>% Z: ^6 K0 x5 F G4 i
<P><FONT color=#0000ff>/* convert style into a string */
2 G; i& C2 ?6 ?3 v. f# G7 vstrcpy(stylestr, fname[style]); </FONT></P>% i4 ^, F+ ~& h' ~: d
<P><FONT color=#0000ff>/* fill a bar */
9 p- m2 n N5 e8 sbar3d(0, 0, midx-10, midy, 0, 0); </FONT></P>' U5 h$ _, m2 {' p3 P& @
<P><FONT color=#0000ff>/* output a message */ 0 {% T* ~- T8 h' ^( P
outtextxy(midx, midy, stylestr); </FONT></P>
* h* A( W6 D: v& |7 F4 d! O/ v<P><FONT color=#0000ff>/* wait for a key */
, f, p' h9 Q( V( _- egetch();
& v+ a. Z9 {* @7 J' ^/ ^' q1 u; Mcleardevice(); 5 j; G% g+ m9 i" Q: T7 i9 ?
} </FONT></P>& n% D4 F f2 Q
<P><FONT color=#0000ff>/* clean up */ 0 h4 ]& M( Q0 e4 ~5 \5 K
getch(); 5 P2 H. ~) C) w
closegraph();
, s" b& c% V7 d# Xreturn 0;
" j% e6 S W* x} </FONT>, Z5 E- y; }1 }! v6 q$ T
2 K' W& ~- P n9 r& v( n* Q. w3 T
</P>9 M+ J4 r v, C& V# Q5 X; l
<P><FONT color=#ff0000>函数名: setftime </FONT>+ U' V7 G8 z' c- V! U
功 能: 设置文件日期和时间
2 V: z0 V- A& e- W3 J) H用 法: int setftime(int handle, struct ftime *ftimep);
N. D2 R8 d5 e0 a程序例: </P>
# W; m4 A' ~; i, ]9 v<P><FONT color=#0000ff>#include <STDIO.H>
6 D2 s$ Z! }9 S: N% K" B& H$ \$ f( z#include <PROCESS.H>! B0 {( w) F2 g; f1 p( l% e0 R& f
#include <FCNTL.H># o4 m: P" ?0 i1 M6 V
#include <IO.H></FONT></P>
# ]6 R7 c5 n$ |. @6 S$ {# M2 D<P><FONT color=#0000ff>int main(void)
2 ]. h; ^# l# n5 Z' y# y, j{ ) x: u Z* g* p
struct ftime filet;
: f4 V2 |3 \0 f7 a: }FILE *fp; </FONT></P>
4 x+ K+ u' |6 W4 y* O7 ~) \: d<P><FONT color=#0000ff>if ((fp = fopen("TEST.$$$", "w")) == NULL)
0 z) I0 r4 K; u9 ]) V' q{
" @' Y) k! o4 T/ j9 _) v+ m6 E Cperror("Error:");
$ [0 d1 ?# e5 Eexit(1);
, R. `; e6 D% A" y- s} </FONT></P>$ |6 m9 z- Z; n6 _/ w9 A- z6 e
<P><FONT color=#0000ff>fprintf(fp, "testing...\n"); </FONT></P>
- \( Y# _1 l( d! l: u<P><FONT color=#0000ff>/* load ftime structure with new time and date */ - s; m! {" `& q, p
filet.ft_tsec = 1;
1 `1 O1 f. D; }' n1 zfilet.ft_min = 1;
5 q4 x( t: F2 j# l) W$ tfilet.ft_hour = 1;
$ f1 K; l# x6 Y4 l" [* R2 L' [filet.ft_day = 1;
& g# k: g& f9 b K8 Gfilet.ft_month = 1;
* \, e9 Z+ u' m, lfilet.ft_year = 21; </FONT></P>
2 Z+ k: Q/ o4 ?- D; a; M1 d" C" Q c<P><FONT color=#0000ff>/* show current directory for time and date */ 1 Y( j8 u; D& k. B0 _
system("dir TEST.$$$"); </FONT></P>
, E7 W+ q+ @' t<P><FONT color=#0000ff>/* change the time and date stamp*/ . h8 o3 X7 h; q9 _! `
setftime(fileno(fp), &filet); </FONT></P>
! ^8 e; j7 p8 y( c* D<P><FONT color=#0000ff>/* close and remove the temporary file */
4 i# v$ n. a& F0 U) }1 s7 ~8 Sfclose(fp); </FONT></P>) C8 X0 M/ B5 Q* u: U- y
<P><FONT color=#0000ff>system("dir TEST.$$$"); </FONT></P>8 V. {- S7 q5 S/ S$ |* C
<P><FONT color=#0000ff>unlink("TEST.$$$");
8 r! k& ~" L/ a6 I0 j5 d4 Z* |return 0;
. x; \" a# e7 s' j' Z}
5 ]/ `( U4 K2 u3 z% M# }</FONT>/ _& q3 G8 N9 n" V! ~
</P>7 y, o& q: p" ] S8 B
<P><FONT color=#ff0000>函数名: setgraphbufsize </FONT>
4 s: X# O5 C7 F {& s8 Q7 s功 能: 改变内部图形缓冲区的大小 M, Y4 a1 z: K$ X6 g1 o+ K
用 法: unsigned far setgraphbufsize(unsigned bufsize); 7 O7 _7 V& {" _% H) T+ l
程序例: </P>$ v" \0 D: Z6 v# T4 r- V% f
<P><FONT color=#0000ff>#include <GRAPHICS.H>+ Z+ _1 n z' C* h
#include <STDLIB.H>
% D/ I6 F/ g( j$ } B#include <STDIO.H>% o$ w( ^; x6 T
#include <CONIO.H></FONT></P>, K5 ~" H( g# s( {4 Z2 f; w/ y
<P><FONT color=#0000ff>#define BUFSIZE 1000 /* internal graphics buffer size */ </FONT></P># Y: e4 a E7 x+ F
<P><FONT color=#0000ff>int main(void) # S2 I. T' z6 d- P) P; W! g
{
- g4 V2 M9 M7 q/ p" m7 [/* request auto detection */
" |6 q# t0 U% H' B( Kint gdriver = DETECT, gmode, errorcode; ) z! q9 m; I" d( c4 a% x
int x, y, oldsize;
9 \# ~( m" `1 Jchar msg[80]; </FONT></P>
5 @! K5 U/ W7 @( s9 ~: ]<P><FONT color=#0000ff>/* set the size of the internal graphics buffer */
5 ~3 M( w! \. j! C; k, K/* before making a call to initgraph. */
; n* Q; D1 e9 joldsize = setgraphbufsize(BUFSIZE); </FONT></P>
' \. Q, m$ g" m# U1 F! C<P><FONT color=#0000ff>/* initialize graphics and local variables */
6 |5 x" Y' w; C' dinitgraph(&gdriver, &gmode, ""); </FONT></P>6 u/ S& [9 n) V1 c7 F- D
<P><FONT color=#0000ff>/* read result of initialization */ 2 ~; W7 c6 V) D! B" K
errorcode = graphresult();
& T- |. P% T3 \* }if (errorcode != grOk) /* an error occurred */ 3 U$ y4 j, x# i, Y& N' i( x
{ w+ t1 v- s6 g* o- d$ N% o
printf("Graphics error: %s\n", grapherrormsg(errorcode));
3 ?$ @% w3 U' t& Q' Z" [ V4 Qprintf("Press any key to halt:");
9 u" t, x. w* m/ [getch();
- v: y/ a; V9 d% F. b9 gexit(1); /* terminate with an error code */ 4 f7 k: B7 ~2 r* e; _0 n
} </FONT></P>
) b* ^/ u& N' i4 Y# ~& V<P><FONT color=#0000ff>x = getmaxx() / 2; ' y3 z/ O8 e5 D8 h
y = getmaxy() / 2; </FONT></P>& V$ N) l( y y; n# k/ j5 A& ]
<P><FONT color=#0000ff>/* output some messages */
D* h3 p- e+ ssprintf(msg, "Graphics buffer size: %d", BUFSIZE); 0 ?5 K' K6 e2 L' ?) R0 ? `9 U7 U
settextjustify(CENTER_TEXT, CENTER_TEXT);
9 T+ A( S5 D4 p7 }9 ^8 Aouttextxy(x, y, msg);
4 A/ e" G/ c" `/ @0 j- e9 dsprintf(msg, "Old graphics buffer size: %d", oldsize);
) I8 ^8 A- q! Louttextxy(x, y+textheight("W"), msg); </FONT></P>
# n- m4 |' q) I, w<P><FONT color=#0000ff>/* clean up */
: F5 P3 @& `% }2 B0 Q# a1 Sgetch();
) i. ?3 Z2 d- Xclosegraph(); 5 L j, |6 G" `5 d
return 0;
! _2 {- C1 R* J- l7 ?9 H. r5 ?& k} ' E9 e; M/ Y$ V% [
5 [" I6 L1 n0 p% F) m- r! x
L% ], h; c, _# W4 }% X/ L
</FONT></P>6 ?; X% _' \/ E3 {5 G- ^# z- y
<P><FONT color=#ff0000>函数名: setgraphmode </FONT>1 n) h9 I( u; s) Y% A
功 能: 将系统设置成图形模式且清屏 , k9 }6 Z0 V1 r+ J% Q
用 法: void far setgraphmode(int mode);
5 m8 a% w# ~, Q/ O! [: A程序例:
# E& R9 m+ h, W0 {* W& F) Z5 e+ `! L+ v. n& j' I7 n; p
<FONT color=#0000ff>#include <STDLIB.H>- Q. e' I+ o" Z* E! k
#include <STDIO.H>7 _. W, E: y9 e; c
#include <GRAPHICS.H>#include <CONIO.H></FONT></P>
8 b) ?' ]7 Y) n9 d<P><FONT color=#0000ff>int main(void) 1 N& x+ P4 Y: S) D8 R4 t# o, ^
{ , L- Z1 }/ }0 I O* `
/* request auto detection */ 1 U( r! R4 h7 m4 @( m1 p0 y
int gdriver = DETECT, gmode, errorcode; 7 m5 W3 r X% e5 w5 S
int x, y; </FONT></P>
$ A- q# B3 i( ~& {<P><FONT color=#0000ff>/* initialize graphics and local variables */
7 k2 A2 A; d, S: S2 @initgraph(&gdriver, &gmode, ""); </FONT></P>1 x+ }! `1 c& K& o: _
<P><FONT color=#0000ff>/* read result of initialization */
5 k5 j5 a- U# Q0 \- \errorcode = graphresult(); 8 L0 H% k4 b, X( f
if (errorcode != grOk) /* an error occurred */
1 }3 p! h1 w6 f" }# B7 \1 f{
! X- S( d( @6 S$ R- [printf("Graphics error: %s\n", grapherrormsg(errorcode)); + X/ o3 }# O6 D, V% z- Y; S
printf("Press any key to halt:"); 5 J( x2 d% w5 i* e, M5 |
getch(); ( j& |6 ]5 t0 |3 R4 e7 D* M; r+ M
exit(1); /* terminate with an error code */
' _2 A9 {$ C' e" G% h8 v} </FONT></P>
' C8 @! I+ q6 h8 t! D<P><FONT color=#0000ff>x = getmaxx() / 2;
4 w7 y% U- w# a" cy = getmaxy() / 2; </FONT></P>
( e* _2 _% X6 f2 m/ k" E' K<P><FONT color=#0000ff>/* output a message */
* k& F+ C; |7 v9 rsettextjustify(CENTER_TEXT, CENTER_TEXT);
& C. ^0 [9 i( ~% A4 Fouttextxy(x, y, "Press any key to exit graphics:"); ; n) Y% K1 i" L- \
getch(); </FONT></P>
+ ~! M4 Y; n& m: S* q. P2 }9 Y<P><FONT color=#0000ff>/* restore system to text mode */ ' @2 I2 v; ? V/ D' T
restorecrtmode();
- m6 ? W" O" F! @6 Fprintf("We're now in text mode.\n");
7 m6 i! p. ^+ ~# f! h# M lprintf("Press any key to return to graphics mode:");
& O# X/ z C# I4 ?getch(); </FONT></P>; j; h0 d1 C/ Q6 x( n
<P><FONT color=#0000ff>/* return to graphics mode */ ! d: O/ G+ o: \! @/ B V
setgraphmode(getgraphmode()); </FONT></P>5 Z2 p9 X% d7 C# M) j/ Q# w
<P><FONT color=#0000ff>/* output a message */ , I, @3 ]1 N" s1 i8 R
settextjustify(CENTER_TEXT, CENTER_TEXT); 5 D, E4 h X ~
outtextxy(x, y, "We're back in graphics mode."); 7 o2 I$ z, B5 g) T( G
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>
8 N% m0 s) f% i$ \- T8 v( ]" Z<P><FONT color=#0000ff>/* clean up */ 2 ?9 H5 G; H2 p) O( |' Z. A
getch();
. O( o* \3 H$ g, f$ z6 u& rclosegraph(); ' x* R. ?$ g9 h; r1 E2 R
return 0; : k: `2 Z0 v: O3 I. {& d
}
h$ V4 O' d7 ^+ {. M
* u4 y; t* s( p" r, p# ?( W* F; F9 {, `' }/ }. X4 Z! ?6 t3 ]! u
</FONT><FONT color=#ff0000></FONT></P> @: b( {: c, Z0 H0 s& T2 I% O
<P><FONT color=#ff0000>函数名: setjmp </FONT>
, v" z! l3 B4 |% L: P% m功 能: 非局部转移 * W- [+ n1 q/ ?. [3 g; l( D
用 法: int setjmp(jmp_buf env);
0 s) x T _+ Y, g( M& B+ X& |8 Q" K程序例: </P>) x, w8 [9 Z/ W+ F/ j7 ]3 F
<P><FONT color=#0000ff>#include <STDIO.H>
, U5 I' w% {& v0 c/ t. v$ Q9 d#include <PROCESS.H>7 F! q/ P5 a+ t* n7 P$ M, l
#include <SETJMP.H></FONT></P>
( i2 T7 y, L g; C6 T* j1 I3 d: a<P><FONT color=#0000ff>void subroutine(void); </FONT></P>
- k, n$ v) r, v9 p<P><FONT color=#0000ff>jmp_buf jumper; </FONT></P>
% b3 M3 B! S7 Z" ^/ n* G& a$ s<P><FONT color=#0000ff>int main(void) ( |* `0 i) ^) v, ]/ x/ U, E7 u
{ 7 i" a0 D$ X7 C3 w2 \
int value; </FONT></P>) K8 D* O1 k1 j+ e& c c6 h9 _. m W6 `
<P><FONT color=#0000ff>value = setjmp(jumper); ; C4 V3 h* s: N% u
if (value != 0) 8 @$ \7 R. w- W+ @, f
{ # I! u& H K7 I9 z
printf("Longjmp with value %d\n", value);
, a! n$ p+ ]4 x; h+ ?& J) gexit(value);
% o+ Z: X/ L: X; C+ d8 r+ n} ) ?/ {. a* L* w/ J3 g
printf("About to call subroutine ... \n"); ) E3 g1 {1 G. x
subroutine(); 8 h, z2 J5 H" }3 m# B
return 0;
" X4 {9 p& L: i} </FONT></P>4 i9 v; u3 h" S" T$ g! j0 o
<P><FONT color=#0000ff>void subroutine(void) 7 z6 m: S) t9 {: `
{
# V# g$ R5 @2 _longjmp(jumper,1);
, G T! R7 g, r6 F} 0 ?6 g9 [8 ]1 R4 ^
</FONT>" \ L4 X& |, R2 X2 n( {
</P>6 N9 _/ I" z, G8 m
<P><FONT color=#ff0000>函数名: setlinestyle </FONT>- `. R( H3 r# b) X, }# H% O, z
功 能: 设置当前画线宽度和类型 ) R3 U; f$ X4 ? \ Z
用 法: void far setlinestyle(int linestype, unsigned upattern); $ T6 o$ s! D7 M
程序例: </P># x+ p1 G( C% V+ w- t) u, A, }, g. v
<P><FONT color=#0000ff>#include <GRAPHICS.H>
: W' k( k0 G, |! g% X% S#include <STDLIB.H>
7 v/ h, \" Z ^! `! ^6 \#include <STRING.H>
$ y$ s& j1 e8 u# P# U#include <STDIO.H>
i2 F6 T n& T, B2 |, L' T#include <CONIO.H></FONT></P>
4 m! U+ ?( h: q' D- y* o<P><FONT color=#0000ff>/* the names of the line styles supported */
1 B# W# m. F: q# C& I, ^2 Jchar *lname[] = {
' m9 i [) H$ y% ^"SOLID_LINE",
/ l+ J; L! c3 l: F9 y& s5 l& I4 e"DOTTED_LINE", # |5 M2 H ]# P& \
"CENTER_LINE",
% r; l2 R6 y; B! p) a( O"DASHED_LINE",
% D0 f+ t1 ^8 s0 z"USERBIT_LINE"
/ D# ]5 O2 \6 e' d0 m}; </FONT></P>
# L- }+ \+ `- k& |<P><FONT color=#0000ff>int main(void) 7 ?1 Z2 r' `0 r/ F- c/ a
{
# i, ^2 N, E3 }5 Q/* request auto detection */ 5 a. e- e2 e4 {( u; B- D
int gdriver = DETECT, gmode, errorcode; </FONT></P>0 t1 ^0 n4 M& q
<P><FONT color=#0000ff>int style, midx, midy, userpat; ( g- g3 N( s3 g' v2 x
char stylestr[40]; </FONT></P>
0 ?4 k8 |) n" t5 l# J e4 I<P><FONT color=#0000ff>/* initialize graphics and local variables */
: V: c# ]$ Y, o- k+ S; |initgraph(&gdriver, &gmode, ""); </FONT></P>
+ G# K/ A q% }* G+ z/ q# D<P><FONT color=#0000ff>/* read result of initialization */ 5 o, l9 w+ Y9 |; I, X
errorcode = graphresult();
. t8 G: O6 O3 H! a# \8 P0 dif (errorcode != grOk) /* an error occurred */
3 U# {' Z* p$ ?# I6 {0 [! S{ g1 R3 T% \: |; H
printf("Graphics error: %s\n", grapherrormsg(errorcode));
: F( P4 ?' K1 j; S2 u1 S0 o/ Iprintf("Press any key to halt:"); 3 O5 c( o7 ~' U. C: L+ d# @$ i
getch(); , [! b( b3 e6 X0 A. Q, S9 r
exit(1); /* terminate with an error code */
# w2 h9 r0 V2 G/ x} </FONT></P>- m/ Z- v& W) z( O+ ~% i5 |, p
<P><FONT color=#0000ff>midx = getmaxx() / 2;
( }* f- l d" N4 S- |# wmidy = getmaxy() / 2; </FONT></P>3 e; [6 t0 S" g% Y1 P$ W
<P><FONT color=#0000ff>/* a user defined line pattern */
8 ]: n& e* c+ Q: C2 a/* binary: "0000000000000001" */
7 D- m4 y2 [2 Puserpat = 1; </FONT></P>
$ _/ N; s: [8 s: U<P><FONT color=#0000ff>for (style=SOLID_LINE; style<=USERBIT_LINE; style++)
/ Y) u0 K; p. r B: i{
9 y* f B- i2 S/ ~/* select the line style */
( o+ V1 H- X! @setlinestyle(style, userpat, 1); </FONT></P>) d' C; x9 Y9 b8 R, x0 C! f
<P><FONT color=#0000ff>/* convert style into a string */ ) T+ ]: ~/ W3 N U
strcpy(stylestr, lname[style]); </FONT></P>1 ?9 j" ~9 D1 o6 R
<P><FONT color=#0000ff>/* draw a line */
# C1 l- D5 b2 N- Uline(0, 0, midx-10, midy); </FONT></P>
% O& l* `2 |7 J& ^7 G) f- c<P><FONT color=#0000ff>/* draw a rectangle */
4 n4 z6 M! y# Q* F) m( V, f( qrectangle(0, 0, getmaxx(), getmaxy()); </FONT></P>! ^" |1 U& A" {6 L" V! q
<P><FONT color=#0000ff>/* output a message */
6 }. s: v- C# douttextxy(midx, midy, stylestr); </FONT></P>" L4 T8 F' t, {! p' _3 k. c
<P><FONT color=#0000ff>/* wait for a key */
1 a; _8 u9 L3 Y8 u, dgetch();
( C: K) _) R3 G( scleardevice();
2 g- c6 N+ I: q, F% U} </FONT></P>
5 m& m/ p* z: L3 M$ p<P><FONT color=#0000ff>/* clean up */
& ]7 x. ~9 ^. j5 y+ N P7 M: p: vclosegraph();
1 Y$ h# _. q# ~! {0 G* Freturn 0;
/ D. n% \) W8 Q& H1 g: f} </FONT>
?& I# r ^* g# s' U7 F+ t: {+ j
. ]) c/ t) ]8 v4 ^5 S- D& {4 O/ T, P5 v! ? G, X
</P>
/ X6 D1 Q- x( N9 s1 X<P><FONT color=#ff0000>函数名: setmem </FONT>
, P! W, f1 Q1 T, J% t' Z功 能: 存值到存储区
% |! c, X6 S# t7 k9 U; W7 r6 n0 |用 法: void setmem(void *addr, int len, char value);
( V0 ]* J5 b# @; E6 O. R程序例: </P>
. n- u* t& H: \& Z1 t8 ?5 L5 P<P><FONT color=#0000ff>#include <STDIO.H>; o& B! C6 Y* a
#include <ALLOC.H>
" V" Q ]2 N5 a) @ Q# Z0 n3 @#include <MEM.H></FONT></P>
$ j! N2 I& ]' I4 w<P><FONT color=#0000ff>int main(void) - d* i1 v0 S$ Z7 _+ [3 d; ~
{
& P* X6 _$ {' ~7 `- M/ t. Fchar *dest; </FONT></P>
" a/ b8 I4 y. G; m/ b! `3 V<P><FONT color=#0000ff>dest = calloc(21, sizeof(char)); * {9 ^/ h: I9 x3 n
setmem(dest, 20, 'c'); . ~8 e7 X! M7 X5 ` g
printf("%s\n", dest); </FONT></P>
' X! C7 t7 Q9 p7 I1 v1 }7 A<P><FONT color=#0000ff>return 0;
' T6 x. T0 B% |* ?7 T1 B! c4 M} </FONT>* G3 M, q7 f6 _4 Y/ y
9 k# @# h% d7 y
4 T- ~$ p: m4 X) Z2 T</P>
2 Q- J9 P. {( L! b<P><FONT color=#ff0000>函数名: setmode </FONT>
+ v0 H. ?0 ~0 E+ U ^& c3 {功 能: 设置打开文件方式 ! f( o: m0 A! e: X" w5 f: o
用 法: int setmode(int handle, unsigned mode); * M( C% f, R+ B( S
程序例: </P>2 a! n. [& F% Z7 J K+ r
<P><FONT color=#0000ff>#include <STDIO.H>
9 }+ L2 c' j, F#include <FCNTL.H>
+ Y$ v* ?$ k# C* S% D#include <IO.H></FONT></P>
0 T3 A( p$ X; }<P><FONT color=#0000ff>int main(void)
5 @. f9 q$ d; Q* B{
% v4 y9 Z, k& n+ H- t! Fint result; </FONT></P>$ U9 q# R% u# `
<P><FONT color=#0000ff>result = setmode(fileno(stdprn), O_TEXT);
7 K" Y+ I0 ?! I) }if (result == -1) 4 S( E1 M9 o% t M0 C" G0 O
perror("Mode not available\n");
$ _2 H( Q' \ E* V. j( e: u. } Belse % Q0 D5 r) d' ^) r& q7 m3 o
printf("Mode successfully switched\n"); h. C6 |: x' I& x
return 0; $ m; [& c$ D4 y# z9 v/ L# ]$ ?
} # j& n+ q* q# e; T
; D! V+ G! `6 W4 m$ y- t4 c- X W</FONT>
4 r# V$ j3 S6 w5 @0 O+ P: z- r. f</P>
T) T$ [+ Z C4 {<P><FONT color=#ff0000>函数名: setpalette </FONT>$ j9 i; s, @9 }3 A: C6 f
功 能: 改变调色板的颜色 : V: ^: \. \, w. Z, G. _
用 法: void far setpalette(int index, int actural_color);
7 E* E+ Q/ b! s! E+ E) `0 J程序例: </P>
" Y+ s7 I% p) a9 N; P( Z" M7 q<P><FONT color=#0000ff>#include <GRAPHICS.H>! B* S$ P0 F# ]6 x6 @8 [
#include <STDLIB.H> l4 V& O5 j! m+ y* |5 _$ o; ?# O
#include <STDIO.H>
4 s' R1 g3 }( X: s#include <CONIO.H></FONT></P>6 G/ J2 ]& g B% m q/ {
<P><FONT color=#0000ff>int main(void)
7 q" v* K6 ?3 H9 |{
( z' B# U9 O j* E5 x/ {/* request auto detection */
5 q I' t/ x+ \1 g8 xint gdriver = DETECT, gmode, errorcode; 7 p$ G# z5 }$ U* \
int color, maxcolor, ht; & X5 f. @& O6 N0 W4 G+ ^% y8 [ _ K* t
int y = 10;
2 c* K) W) q+ `# q( Z Z1 y( J8 tchar msg[80]; </FONT></P>
* Y" c1 I- C% a( w2 o; y' H% E<P><FONT color=#0000ff>/* initialize graphics and local variables */ 6 n2 P; Z# H% i1 _& m
initgraph(&gdriver, &gmode, ""); </FONT></P>+ w1 N6 }+ N1 W# c& `* i
<P><FONT color=#0000ff>/* read result of initialization */
; F* S& C# @* W: u- {7 werrorcode = graphresult();
2 c% c2 F9 N# u5 t1 v; Eif (errorcode != grOk) /* an error occurred */
8 `! y( Q* G9 I0 D{ - H- x% U- W" B
printf("Graphics error: %s\n", grapherrormsg(errorcode));
& v* g3 J9 a' n' pprintf("Press any key to halt:");
% J u* J. t( }% n3 h+ y0 c/ Lgetch();
+ X! Q* K* i, N! w/ kexit(1); /* terminate with an error code */
6 v6 k( h! u$ m7 \ {! B5 Z; `, `} </FONT></P>: A/ @( \+ l! G( x6 ~' c
<P><FONT color=#0000ff>maxcolor = getmaxcolor(); / N* T7 D( k' B, S, J9 @
ht = 2 * textheight("W"); </FONT></P>2 h# H9 R* k, b [* x, |
<P><FONT color=#0000ff>/* display the default colors */
& G$ u2 R4 G" jfor (color=1; color<=maxcolor; color++) 9 [8 v# v8 \0 r' X' @1 ^% F
{ 8 N4 O8 X4 u9 H# \$ `) S
setcolor(color); % C& l7 C9 o! D
sprintf(msg, "Color: %d", color);
6 ~+ P) L1 p$ n9 S2 h, D- A& o" C4 @outtextxy(1, y, msg); ! j: |- y7 M8 g( K4 p
y += ht; ' g: R) {9 Z. R. [
} </FONT></P>
% V, H6 Q+ _6 f<P><FONT color=#0000ff>/* wait for a key */
4 p5 i) y0 K" Ngetch(); </FONT></P>
# j3 F" v7 u8 n2 O2 k8 `9 E<P><FONT color=#0000ff>/* black out the colors one by one */
$ A3 G, n) v, y/ I+ g w7 f2 @for (color=1; color<=maxcolor; color++) 8 ?. _; F& x4 R. e9 P8 |; \
{ 6 g5 ~0 {0 D1 M( ^# V
setpalette(color, BLACK); * E5 o- u( X( E3 j1 \' K
getch(); # z' f( x1 m$ z
} </FONT></P>+ A5 c. l0 W. y3 s& Q2 v' v4 r! g
<P><FONT color=#0000ff>/* clean up */
! N5 W/ |( C- m/ w% s. Aclosegraph(); " h I7 X# k8 ^( |) \! {, ~$ f
return 0; 4 s6 _, A, _% c+ X Q4 e$ g4 j; h
}
; ~* V; ~( N3 U4 G</FONT>" j8 u7 \+ ^/ c
</P>% ~$ z& F7 }8 ~" A! x: \
<P><FONT color=#ff0000>函数名: setrgbpalette </FONT>- C/ H8 I1 }) o& \
功 能: 定义IBM8514图形卡的颜色 & V/ ?) F) R6 s' \
用 法: void far setrgbpalette(int colornum, int red, int green, int blue);
" a+ o/ X& ]6 _" o7 L0 {程序例: </P>
- E/ P6 F4 p+ L" M g# t<P><FONT color=#0000ff>#include <GRAPHICS.H>
/ s% i* p3 R" P9 t#include <STDLIB.H>) y/ Y7 w8 W# B4 u) a
#include <STDIO.H>' e1 {, {, w) c4 U
#include <CONIO.H></FONT></P>) H& m) L' g2 q% ?2 K
<P><FONT color=#0000ff>int main(void)
% t! k# a5 r' t3 R- S9 s{ 4 a- k7 J1 k9 Q* t- E" f" Z: z
/* select a driver and mode that supports the use */
7 `) u) u K9 r0 @3 w9 J. x! e4 z/* of the setrgbpalette function. */ ) U6 j9 J' J7 i0 r2 _5 j
int gdriver = VGA, gmode = VGAHI, errorcode;
! L6 K) A* l& f O5 w8 O+ `% ~struct palettetype pal;
5 r3 `/ B) G; j2 p. ~, q9 y7 mint i, ht, y, xmax; </FONT></P>) q) C6 M0 j i$ ~8 H" a
<P><FONT color=#0000ff>/* initialize graphics and local variables */
7 }4 G/ O8 _9 c9 K- cinitgraph(&gdriver, &gmode, ""); </FONT></P>: z$ e0 S0 t( k
<P><FONT color=#0000ff>/* read result of initialization */ & K( o3 I0 S5 }
errorcode = graphresult(); 2 H/ L9 X( k& ^! H9 l, h+ P+ ]
if (errorcode != grOk) /* an error occurred */ J4 K0 T( `( N- t( \ R
{
! n. \; V) x6 W, }printf("Graphics error: %s\n", grapherrormsg(errorcode));
0 H( k. x+ D9 A$ K! c7 b( [& Mprintf("Press any key to halt:");
8 }' R) V0 e+ A$ d. i) qgetch();
" a) {4 f: i) o) pexit(1); /* terminate with an error code */ 8 k3 J$ Q/ G# X# j" _$ y! u
} </FONT></P>
# r' _0 j( ~$ E' d& T" A<P><FONT color=#0000ff>/* grab a copy of the palette */
' r5 w: t5 z' b0 q! B' }getpalette(&pal); </FONT></P>
r8 L+ f8 n9 A8 C; t" Q<P><FONT color=#0000ff>/* create gray scale */ - L5 \9 j @5 c6 Y. ^4 z
for (i=0; i<PAL.SIZE; <br i++)> setrgbpalette(pal.colors, i*4, i*4, i*4); </FONT></P>) E7 i" o3 Y! l* n, ]1 y
<P><FONT color=#0000ff>/* display the gray scale */ ; B( {. q8 C2 u3 P
ht = getmaxy() / 16; - p9 n! u1 ~$ r2 ^( [% B% b
xmax = getmaxx(); 6 Q( v+ A6 w5 F( a
y = 0; 1 l; H- `$ u8 w
for (i=0; i<PAL.SIZE; <br i++)> {
) ^: g" Y$ j' T, U b+ R6 Z3 m" Dsetfillstyle(SOLID_FILL, i); 3 ]# S0 h6 n% [( R! H0 R* R, c- u
bar(0, y, xmax, y+ht); $ k: ~- @+ O2 ~/ G
y += ht; - l( k3 P5 x& D a! t
} </FONT></P>8 g9 E# [5 G7 u5 g$ u. B7 K
<P><FONT color=#0000ff>/* clean up */
0 b5 H( V4 g0 ]2 n6 |. {getch(); ! H/ W% t: i% Q; v% z6 y
closegraph();
( H: H1 m& D/ r# K1 c5 {return 0; 3 P+ W' [3 p5 |1 W1 [$ d& |. |
}
4 L5 ]9 z. i; O</FONT>+ \ F6 y% a! ^7 p- n' U; }/ I/ K! K
- T8 D+ Q0 W& V) ?2 G5 G/ W) }</P>3 D+ }% t, U6 I1 R; @* v) J
<P><FONT color=#ff0000>函数名: settextjustify </FONT>
q* X6 N) N) ], g. p功 能: 为图形函数设置文本的对齐方式 " s' G' [3 K% O& d: W
用 法: void far settextjustify(int horiz, int vert);
' c. C6 Q+ l+ q# }1 a q程序例: </P>' K& |2 E- N, o) F$ t, y( _: l) N
<P><FONT color=#0000ff>#include <GRAPHICS.H>: ]5 x+ T' F1 a& Y/ J
#include <STDLIB.H>
" Y K7 _1 C# H' v#include <STDIO.H>
w8 _5 `( T( k0 d c#include <CONIO.H></FONT></P>
9 T' [$ P8 F' ]+ u% K<P><FONT color=#0000ff>/* function prototype */
1 G$ {( _/ ]- B& g2 x- z& Y) X: Ovoid xat(int x, int y); </FONT></P>- y4 w* j' i1 l$ Y) |) _, y
<P><FONT color=#0000ff>/* horizontal text justification settings */
5 J/ g" p- ]7 S0 x5 v, ]char *hjust[] = { "LEFT_TEXT", ) D9 ? D% ^6 e! t" L
"CENTER_TEXT",
; y& }& q$ _ b0 g' a0 M- t"RIGHT_TEXT" : E K4 `, @+ z, a# M
}; </FONT></P>
4 t) ~$ | c. Z; E<P><FONT color=#0000ff>/* vertical text justification settings */
+ I* i+ b0 X. v6 e) o% a5 L6 z! Rchar *vjust[] = { "LEFT_TEXT", + I0 m% Z* S+ b3 ~! q: E
"CENTER_TEXT", % `0 N1 u. K, W) c( E% S% Y+ \
"RIGHT_TEXT"
& e0 x0 @' N- Y/ a6 W# [8 _}; </FONT></P>
1 e% z$ @9 L! g) x9 q<P><FONT color=#0000ff>int main(void)
0 I: d0 z2 T3 g- n# u% Y! k{ ' i0 F1 {( N; H5 C* ?
/* request auto detection */ 2 {, R0 g# {5 ?# ^ R
int gdriver = DETECT, gmode, errorcode;
4 U, ^" l4 y' T5 Y; V% gint midx, midy, hj, vj;
6 w3 i) B: q+ z" G- q* echar msg[80]; </FONT></P>
& S$ L4 b( w" ^) c<P><FONT color=#0000ff>/* initialize graphics and local variables */
9 E! s0 r6 X" A% Kinitgraph(&gdriver, &gmode, ""); </FONT></P>
$ K& k9 Q1 V+ B9 @<P><FONT color=#0000ff>/* read result of initialization */
8 V0 c: c8 S- N: [0 terrorcode = graphresult(); - R! A& N$ ]+ j, ^
if (errorcode != grOk) /* an error occurred */ # x! H' d' m1 ]$ I% X% D
{
9 H' Z9 J: l3 ]' \0 Pprintf("Graphics error: %s\n", grapherrormsg(errorcode)); . v( z( O4 C* L [, Y
printf("Press any key to halt:");
+ [9 g7 ^, @% z9 lgetch();
4 o3 x; i7 s6 ~exit(1); /* terminate with an error code */
7 c: i* z9 k- P: X} </FONT></P>! e1 V9 Z7 @ o& t$ c1 l
<P><FONT color=#0000ff>midx = getmaxx() / 2; ' B* }& \, _- N0 x0 e
midy = getmaxy() / 2; </FONT></P>
7 A V% G, p( A, a2 r<P><FONT color=#0000ff>/* loop through text justifications */ & Z3 q, G+ X8 K5 ]( E8 |
for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++) ! z1 J9 Q. \- L
for (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++) " H+ x3 {" U0 d }+ P' l
{ 4 r/ C8 H4 p6 ?9 I! m, Z/ V
cleardevice();
! C ^. c+ i! g5 U# p H- i$ c3 d/* set the text justification */ # u4 B# T" C) e' o W8 E4 V( T
settextjustify(hj, vj); </FONT></P> ]# \# w; S( s6 M0 u8 \
<P><FONT color=#0000ff>/* create a message string */
* w! a/ a& f) h: h0 a! Csprintf(msg, "%s %s", hjust[hj], vjust[vj]); </FONT></P>- e# P; i. L- V" ?& W, N
<P><FONT color=#0000ff>/* create cross hairs on the screen */
$ S/ _; W) p4 Jxat(midx, midy); </FONT></P>
y+ O X) Z; F<P><FONT color=#0000ff>/* output the message */
# w; w S, B% T8 Routtextxy(midx, midy, msg); 2 r# h. }# |4 s9 U
getch(); : N2 Y j- H5 A7 ~* ]
} </FONT></P>, H% _7 y9 p1 t2 f
<P><FONT color=#0000ff>/* clean up */ 3 q& ]+ o) n! U
closegraph();
/ g. o- K) m) vreturn 0;
3 _, G" a4 p7 M1 E} </FONT></P>
, k0 P( w1 h7 B* E/ c<P><FONT color=#0000ff>/* draw an "x" at (x, y) */ $ r0 D3 {! \0 B u% k
void xat(int x, int y) * y8 y# ^. d, Q5 n k' s
{ - S2 i# a9 h& ]0 a' h
line(x-4, y, x+4, y);
* E. S# D# L3 ]& Xline(x, y-4, x, y+4);
6 I8 j, m* y* C( H7 ~7 n} </FONT>5 n) u8 p! ]' Z8 J2 j% t% A
3 x; P. L- z T1 O: w" k) F</P>* z' S6 X1 S" v6 e1 F
<P><FONT color=#ff0000>函数名: settextstyle </FONT>
/ T5 A5 W+ M3 n, P f/ F功 能: 为图形输出设置当前的文本属性
' _; N$ L+ i G& u5 P' _! k1 {用 法: void far settextstyle (int font, int direction, char size); ( N! _& e/ v$ X* h# ]
程序例: </P>+ n$ k. F$ q( X% }$ T0 R
<P><FONT color=#0000ff>#include <GRAPHICS.H>; m0 t! F: @) }' P1 y& r( b: y
#include <STDLIB.H>
8 y' B) T1 P1 B9 {% I. |#include <STDIO.H>
: E X6 V' ^6 D+ q: ^6 e#include <CONIO.H></FONT></P>& _1 ^0 N; [/ ^. o0 u- W
<P><FONT color=#0000ff>/* the names of the text styles supported */ ; ~/ z5 }0 D. o; K- B! c
char *fname[] = { "DEFAULT font",
" a3 h, }% \7 ?, l+ w"TRIPLEX font",
& g/ G: ?% C0 b( A0 j"SMALL font", : T3 j, ~* N ]* ^
"SANS SERIF font", 9 x& T+ `9 W {0 }8 B, A+ f
"GOTHIC font" . j4 @# N8 m, J) q( D. w
}; </FONT></P>4 ]9 Z, S7 D. _( d$ d4 T
<P><FONT color=#0000ff>int main(void)
9 b; x" n: u( G" c: r. S{
) u' I" G6 R8 z/* request auto detection */ ) Z2 e" r: }7 J5 l! q1 S
int gdriver = DETECT, gmode, errorcode;
) d+ P' F: I+ |% j6 y3 s0 g+ ]int style, midx, midy; - V$ S5 E% i, b$ m8 p7 z# Y0 r
int size = 1; </FONT></P>* O9 Y: d% m* L7 {
<P><FONT color=#0000ff>/* initialize graphics and local variables */ - ^! V3 b5 E3 v2 i+ x4 Q w( W
initgraph(&gdriver, &gmode, ""); </FONT></P>! Z. F# P p3 |+ P/ S% N% p. y7 ^3 ]
<P><FONT color=#0000ff>/* read result of initialization */ : Y- S; j) V& _ O% W& p5 x4 V* S+ d
errorcode = graphresult();
' M3 K) N+ T" a. L9 [if (errorcode != grOk) /* an error occurred */ 1 E7 q7 [' i3 |; D1 e
{ 1 G* X8 e, o3 J7 L+ @2 Q8 b
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ; [) `* x. k# o3 r; m @
printf("Press any key to halt:");
7 w. }/ `& Q& H9 {) {getch(); * i. q6 L& G6 a, D" b2 u
exit(1); /* terminate with an error code */ ) C Z, e- x* C4 @/ `7 O- \
} </FONT></P>
6 Z* V2 d* X- e<P><FONT color=#0000ff>midx = getmaxx() / 2;
1 J ]1 ]) y2 R1 y+ | gmidy = getmaxy() / 2; </FONT></P>
4 H7 |1 Y, v+ D7 S6 M<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>
5 N5 d3 {/ g, n<P><FONT color=#0000ff>/* loop through the available text styles */ : ]2 }& I6 r% E! O% t4 |) j ~
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
9 d, q0 Y3 H) I [0 c{ , k" k$ S7 Z1 a& g8 e) i
cleardevice();
/ n- s$ b: T" B: S/ J) }if (style == TRIPLEX_FONT) ' }$ G$ Z: c: |. Y
size = 4; </FONT></P>9 b; D9 F8 ^2 t
<P><FONT color=#0000ff>/* select the text style */
7 [8 q) H8 v: z- Q0 asettextstyle(style, HORIZ_DIR, size); </FONT></P># b. R5 p: s6 _! n+ K4 G
<P><FONT color=#0000ff>/* output a message */ , ]+ F* E- v5 w" A2 k }
outtextxy(midx, midy, fname[style]);
; F4 }; n: Z- C0 E& bgetch();
' V& L+ ]- l6 l; ]} </FONT></P>
6 J N2 p7 X& p<P><FONT color=#0000ff>/* clean up */
5 q2 o& f( J- N7 \/ l" eclosegraph(); X& I3 ]! `8 U9 I
return 0; 1 O- S R, [& V) y0 r
} / e; \6 V" R& F/ e8 Z, ]
</FONT>( w' T0 b, b+ R m7 X# v0 ]
</P>0 H1 W, V1 F2 r5 i0 `1 S; C
<P><FONT color=#ff0000>函数名: settextstyle </FONT>
2 H0 U, A6 r$ E/ \) N6 z功 能: 为图形输出设置当前的文本属性 - r. E' W9 R: F- o, Y& Q6 G
用 法: void far settextstyle (int font, int direction, char size);
- p8 U, p Q! x t% y! B程序例: </P>
+ z3 i! ]: d& y' b* i<P><FONT color=#0000ff>#include <GRAPHICS.H>% G5 Q$ G% l) u; x
#include <STDLIB.H>
& X0 M6 N3 x& N) Q5 H4 m3 ] s#include <STDIO.H>3 `* S( r E$ m& }$ X0 f. X
#include <CONIO.H></FONT></P>2 ?- D* j! ]* p) _
<P><FONT color=#0000ff>/* the names of the text styles supported */
! ]. X6 Y6 j. l! V0 x/ a. _4 S" hchar *fname[] = { "DEFAULT font",
! D& |/ v( o. C"TRIPLEX font",
* c3 u( G$ I' y/ @"SMALL font",
8 c/ P! f9 L c7 P4 {: \"SANS SERIF font",
' p' T. R- M" f: R3 y' O9 R% W"GOTHIC font"
% Y" L4 p1 o" S. {}; </FONT></P>
9 h, i3 \0 `" _* O0 H- a<P><FONT color=#0000ff>int main(void)
/ ?8 y! B x) G4 ?" Y( b W{ $ n9 J4 x, _2 N& s7 Q7 I
/* request auto detection */
( A) Q8 G6 a/ v( Sint gdriver = DETECT, gmode, errorcode; * @4 v: g: B7 r: q
int style, midx, midy; # {( C5 A5 ~+ i% u/ s1 z" k
int size = 1; </FONT></P>1 f& ]: e$ Z: Q: O
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 7 A' e% s# F# }& M; k- u
initgraph(&gdriver, &gmode, ""); </FONT></P>- B5 v! O* P7 T9 y* x* P* d) k
<P><FONT color=#0000ff>/* read result of initialization */ ! o8 J* D4 X. F! H, ?' ^+ Q8 }
errorcode = graphresult();
7 \9 p4 O! ?0 g1 g T, x; xif (errorcode != grOk) /* an error occurred */ 1 K/ c; Z( _+ H/ D- [8 H1 A
{ ^' T) ` B3 s7 o
printf("Graphics error: %s\n", grapherrormsg(errorcode));
9 [3 \; |. }( ^8 K# { |) v$ Oprintf("Press any key to halt:");
( F3 S" ~( B- J# Z4 qgetch();
1 Q" S* h& x3 Z# A, ?5 Cexit(1); /* terminate with an error code */ 2 `$ F; F& q3 N& _7 A! h! B# C, V
} </FONT></P>9 d- z& w$ H8 D$ E
<P><FONT color=#0000ff>midx = getmaxx() / 2; ( K% @4 e, ^6 F8 U' U3 W
midy = getmaxy() / 2; </FONT></P>
- }; w" |8 l0 `8 \4 H<P><FONT color=#0000ff>settextjustify(CENTER_TEXT, CENTER_TEXT); </FONT></P>) ~! K! \# a7 e+ V7 L# s, t( Q
<P><FONT color=#0000ff>/* loop through the available text styles */ ( L; P2 Z) D* u' k+ `# v
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++) ' n2 F8 X" p1 ]0 \4 H$ e7 m. Z1 V% J# ]
{ , ], w. X+ d6 C( y5 v
cleardevice();
* o6 r$ G/ `9 X: cif (style == TRIPLEX_FONT) 8 U |5 O* B+ b4 m2 ~" M
size = 4; </FONT></P>
% m4 {( H: t) b<P><FONT color=#0000ff>/* select the text style */
7 Q/ _; Q. _& y, f! psettextstyle(style, HORIZ_DIR, size); </FONT></P>' Y/ R4 N9 O( [( K- Y7 ~, D
<P><FONT color=#0000ff>/* output a message */
: J" n2 ?/ l' fouttextxy(midx, midy, fname[style]); P; ?2 P' w6 D/ {. a, J) _+ {+ p
getch();
6 x: A& C! J& v5 H} </FONT></P>. ]8 H: }( c: q
<P><FONT color=#0000ff>/* clean up */
! F# D* X8 @! C0 Bclosegraph();
6 c9 @- X# I- j" T. g' ?return 0;
- t! v# Y+ m+ ?2 d% H. ]5 F- P8 o# l} </FONT>
9 |- \! J6 C9 d! L# S$ B' ]$ ?7 X# @/ A0 Z+ t8 v5 _ }) q8 {) {
</P>' i6 l V; W1 Z# B. s
<P><FONT color=#ff0000>函数名: settime </FONT>% P# m {9 G4 |9 w
功 能: 设置系统时间 6 |+ y6 A w s1 G) o- S
用 法: void settime(struct time *timep);
5 U v& l7 ~ @' t' ^程序例: </P>1 e" f! Q3 U6 V' b# M1 }
<P><FONT color=#0000ff>#include <STDIO.H>
' k% l8 Z. d, L: q#include <DOS.H></FONT></P>
" v+ H( T! K2 s i0 o<P><FONT color=#0000ff>int main(void)
$ x$ y* L% H8 x* @; ]{
0 g6 K! A. [- z, t4 d+ n$ Pstruct time t; </FONT></P># L* M; x4 J$ p
<P><FONT color=#0000ff>gettime(&t); 6 P1 U& X" }9 o$ ]' E. W5 c! m
printf("The current minute is: %d\n", t.ti_min); 7 M- t8 x0 J& N( N5 N' M i
printf("The current hour is: %d\n", t.ti_hour);
6 S! `1 [; b7 Y, e4 K- |6 k! \+ sprintf("The current hundredth of a second is: %d\n", t.ti_hund); - y3 |" w6 A# [* e% ^5 k5 P& Y. k2 _& k
printf("The current second is: %d\n", t.ti_sec); </FONT></P>
+ H$ }$ J! P- ~: }, U. t, k<P><FONT color=#0000ff>/* Add one to the minutes struct element and then call settime */
8 F' i% e+ M6 W- lt.ti_min++;
7 C- e, }! c& L) U( V: z: Z5 Dsettime(&t); </FONT></P>- \/ f9 _, o' n5 r
<P><FONT color=#0000ff>return 0; ! G) L8 c5 ? H; `, L6 C
} </FONT>8 `2 \- m" C Y7 T
8 m$ {0 |* L, \4 {</P>
( f. j1 j* o- |, l0 ?<P><FONT color=#ff0000>函数名: setusercharsize </FONT>$ `7 f0 `% s$ @+ ~
功 能: 为矢量字体改变字符宽度和高度
& d" V8 k, q) ?1 K& X) G0 W用 法: void far setusercharsize(int multx, int dirx, int multy, int diry); % T2 \8 S% A" Y
程序例: </P>
" g% M8 w- l+ u4 \<P><FONT color=#0000ff>#include <GRAPHICS.H>
% C, L; i+ G* l; ?& J* I0 X4 X, X#include <STDLIB.H>
" P- {5 t7 U8 Y7 A/ b#include <STDIO.H>5 ^# W6 i0 k3 @) X1 W
#include <CONIO.H></FONT></P>* G. z; |; F" p2 v: T: P
<P><FONT color=#0000ff>int main(void) - I3 A. v5 y! |7 l
{
- ]+ x& A. h$ D3 o: r7 D! ?/* request autodetection */
& @8 x- T; W/ Sint gdriver = DETECT, gmode, errorcode; </FONT></P>; A( i7 k; A; _3 r6 U8 ]. h
<P><FONT color=#0000ff>/* initialize graphics and local variables */ $ x! l& K6 n+ C/ O# r* ?" K A1 z
initgraph(&gdriver, &gmode, ""); </FONT></P># H9 v- c! z! L: u0 W4 u
<P><FONT color=#0000ff>/* read result of initialization */ 5 V5 m# u0 n% z$ R
errorcode = graphresult();
" E( Q6 Y/ V& Z: W7 }if (errorcode != grOk) /* an error occurred */ # s+ q4 u; C! N/ Q) M2 M/ E( f
{
1 L+ {% I! k8 t( ^, X3 Hprintf("Graphics error: %s\n", grapherrormsg(errorcode));
: O; e4 ^0 ?1 Iprintf("Press any key to halt:"); 8 X' E2 f: q# U! h
getch(); 1 F6 ]. O6 c1 y* p
exit(1); /* terminate with an error code */
! ^. J+ ]% f& h3 k} </FONT></P>
: j( h5 R& _" G+ e! Q5 ]<P><FONT color=#0000ff>/* select a text style */
) C. ^7 o$ e/ ~, c7 `settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); </FONT></P>
: G- b' e2 N2 t5 o2 A% c& b<P><FONT color=#0000ff>/* move to the text starting position */ ' W' v5 s# S# @, X/ [# s% i$ S
moveto(0, getmaxy() / 2); </FONT></P>
& y5 I" P+ s( S' z7 @<P><FONT color=#0000ff>/* output some normal text */
6 P6 q* s! R- u0 I6 touttext("Norm "); </FONT></P>2 a' K' P8 w+ b
<P><FONT color=#0000ff>/* make the text 1/3 the normal width */
0 ]% |( i, ^5 \' |( \" V; Ssetusercharsize(1, 3, 1, 1);
+ `8 h6 V- m# K( S" Jouttext("Short "); </FONT></P>
$ b7 W: P. r( b( P<P><FONT color=#0000ff>/* make the text 3 times normal width */
. F5 K" {: w6 U8 Y/ V5 @7 Ssetusercharsize(3, 1, 1, 1);
& S7 p. X& ?% q) Aouttext("Wide"); </FONT></P>6 B- Z. l* o1 W$ O+ L4 V9 n5 ^
<P><FONT color=#0000ff>/* clean up */ + o* ~3 z! X/ L9 ~7 x
getch(); ; f; J9 |$ ~! V. ~/ T1 ~4 b+ C( I
closegraph(); 0 b6 J ]& w; c k( K
return 0; , x7 A: g$ r: _9 R8 ^0 t
} </FONT>
. s) T% q6 H6 N" V& F. _</P>
8 ~8 R. x, K, q& g, c3 l<P><FONT color=#ff0000>函数名: setvbuf </FONT>
9 W" f& s2 x b功 能: 把缓冲区与流相关
4 @! h# A6 U5 F# P用 法: int setvbuf(FILE *stream, char *buf, int type, unsigned size); 5 p ~5 T3 ^) c( ^1 J7 D
程序例: </P>
; @+ P* A2 J( B. x<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
2 N9 }7 L s8 I8 X( V2 O# C<P><FONT color=#0000ff>int main(void)
2 o. y2 y- _: o E/ B' k, U{ + @- E9 M5 S9 F) J
FILE *input, *output;
8 b$ r# e, x. K5 B; k0 Achar bufr[512]; </FONT></P>
, y" R. g# U, r) _# @5 V<P><FONT color=#0000ff>input = fopen("file.in", "r+b"); 4 a: @$ d9 C2 y# s+ x+ O% m
output = fopen("file.out", "w"); </FONT></P>
7 L! |7 d4 P5 @; D0 R+ o8 i2 u! ]) @<P><FONT color=#0000ff>/* set up input stream for minimal disk access, * k% e; g S* z3 p
using our own character buffer */ ; N- J l' Z' D
if (setvbuf(input, bufr, _IOFBF, 512) != 0) 9 w$ w4 ]2 D' Y. A! o+ z: v3 r
printf("failed to set up buffer for input file\n");
- N$ N' n8 s. Selse
) _/ M, y0 t( Y F; ]. bprintf("buffer set up for input file\n"); </FONT></P>' z2 F- r% K% q- [! q! @
<P><FONT color=#0000ff>/* set up output stream for line buffering using space that & I8 C: l% D1 R2 e ]6 b
will be obtained through an indirect call to malloc */
/ ^- E5 w& `4 J8 p# ~: yif (setvbuf(output, NULL, _IOLBF, 132) != 0) ' V; @9 G! y! l! f, ~
printf("failed to set up buffer for output file\n"); L. I( p! R& a; |: q
else
$ U) V( \* h6 V$ f" s( Rprintf("buffer set up for output file\n"); </FONT></P>
7 J# V p: ~, Q2 d& u7 \ D3 T<P><FONT color=#0000ff>/* perform file I/O here */ </FONT></P>' W$ v! s* W, |" {2 q: t/ R
<P><FONT color=#0000ff>/* close files */ 0 \( \6 O7 A3 k2 X6 O! z7 T
fclose(input); & P6 N; Z/ X3 I$ @! [9 q
fclose(output); ; `7 ~% S& L- H4 ^. j
return 0;
1 k6 [% V' P4 p% q}
: [, r/ U7 m. p) [. |& P, w</FONT>
7 W! B1 P' n6 I1 [. m
. f5 l! F$ v; a1 h* a</P>
& Y* T9 y: ^8 ]1 }9 X<P><FONT color=#ff0000>函数名: setvect </FONT>1 I) G( F# Y. X
功 能: 设置中断矢量入口 ; Q \- I/ D. @. N0 \' S" o7 t4 g
用 法: void setvect(int intr_num, void interrupt(*isr)());
4 I2 n* ?" p, z* K; s程序例: </P>/ c J4 h7 l6 X
<P><FONT color=#0000ff>/***NOTE: ) ~' t8 F& ?6 i6 D
This is an interrupt service routine. You can NOT compile this * |5 j' T0 j x7 S
program with Test Stack Overflow turned on and get an executable
: G( f8 x" o+ c3 ^* T6 V: i8 wfile which will operate correctly. */ </FONT></P>
# J! C8 C; N# l1 k& G" d<P><FONT color=#0000ff>#include <STDIO.H>
]- {0 ^7 B: T c- w- P7 q8 N; T#include <DOS.H>- G# s5 m: Q% u1 I6 Q
#include <CONIO.H></FONT></P>
# M4 L, L7 z d# x. f( }! c* c<P><FONT color=#0000ff>#define INTR 0X1C /* The clock tick interrupt */ </FONT></P>
8 I, W, o1 ]# g8 T9 I<P><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>
* v H' A; g: `9 j% B3 U<P><FONT color=#0000ff>int count=0; </FONT></P>4 S! F/ g9 }6 f& W3 g
<P><FONT color=#0000ff>void interrupt handler(void) / y5 n+ |( f7 O- `3 v6 k6 ^
{ / N6 @4 R- b% N' z a; b$ }
/* increase the global counter */
1 x+ J- ^' [6 N8 y% ~count++; </FONT></P>
7 X8 M7 ?1 X- ?7 D, S+ K" F9 t4 R<P><FONT color=#0000ff>/* call the old routine */
G L, d, i$ F9 f: j+ p9 b! a5 voldhandler();
& }5 F( v. l( c} </FONT></P>- n/ l6 g( @' u3 A; q+ O
<P><FONT color=#0000ff>int main(void) ' V2 |. W! @, N
{
( E( F4 D8 _# V/* save the old interrupt vector */ y y: E( e) L; f
oldhandler = getvect(INTR); </FONT></P>
' B. y6 ]1 u/ \7 j<P><FONT color=#0000ff>/* install the new interrupt handler */
T- ^% \# \/ B3 Nsetvect(INTR, handler); </FONT></P>
- j' L+ Y& X9 h/ \9 K0 }% U<P><FONT color=#0000ff>/* loop until the counter exceeds 20 */ ) m y6 j3 B" ^1 Y+ d9 a. f
while (count < 20) 3 y& [0 c0 @: }" Q0 H; L
printf("count is %d\n",count); </FONT></P>" O1 S/ ]. y6 `& x% @5 p9 }
<P><FONT color=#0000ff>/* reset the old interrupt handler */ 6 N3 ^ ^6 |$ m1 {* y
setvect(INTR, oldhandler); </FONT></P>
/ B5 e2 L& \2 G<P><FONT color=#0000ff>return 0;
. X" Q+ S( W9 {0 D' _' }; a}
) E7 Q' E8 i" X; q1 P+ T</FONT>
/ A! c; P) g5 M: a7 o" e</P>/ I( W8 _& ?1 k- s) J# C6 T
<P><FONT color=#ff0000>函数名: setverify </FONT>" {! n3 Y( I( j& ]; F% W4 Y
功 能: 设置验证状态 ; J# ]% y1 F- V. E. t% \9 d
用 法: void setverify(int value);
' e! K: k" b/ ?1 d+ o0 p' i4 d程序例: </P>
7 w) i# u8 g# E4 J; K" D<P><FONT color=#0000ff>#include <STDIO.H>
/ N( R- q9 j) G/ I#include <CONIO.H>
- {/ w' {: W7 W+ P#include <DOS.H></FONT></P>
, W* j7 o( S% f$ @8 j. S<P><FONT color=#0000ff>int main(void) 2 }# g# R$ b2 @0 e8 w, N, i/ A
{
8 E" e7 l* t0 d$ L. Qint verify_flag; </FONT></P>. Q( K( W: p% m+ U; W
<P><FONT color=#0000ff>printf("Enter 0 to set verify flag off\n");
& c' p9 ^- a" V l: S/ B j0 I2 iprintf("Enter 1 to set verify flag on\n"); </FONT></P>
, z. _" m' Q" D: B8 D<P><FONT color=#0000ff>verify_flag = getch() - 0; </FONT></P>
- D: B+ B0 v o [- I- u, Y<P><FONT color=#0000ff>setverify(verify_flag); </FONT></P>
$ E+ t5 J0 U6 Q* B<P><FONT color=#0000ff>if (getverify())
d* J' {! m9 S5 u# X- i/ Z2 uprintf("DOS verify flag is on\n");
$ f$ f; }" | m$ w2 Celse
: Y. ]! h" }! ]. s8 oprintf("DOS verify flag is off\n"); </FONT></P>
* u0 ]' z- x. k; N<P><FONT color=#0000ff>return 0; $ K8 o% l# H- \9 g6 t
} 2 P+ V7 ^/ H- x2 a0 Z' e2 ~
<FONT color=#ff0000>
8 ?# _7 u5 x- ?8 U- n</FONT></FONT></P>
+ g8 y& T g( B7 V5 O) B<P><FONT color=#ff0000>函数名: setviewport </FONT>
0 Q2 V" E$ }# q) @7 ?% \3 w功 能: 为图形输出设置当前视口
9 q& e0 R$ A6 ?4 ~ Z+ C, b用 法: void far setviewport(int left, int top, int right,
; t- K9 i2 Q8 W) _4 eint bottom, int clipflag);
: q* L1 W* T N" m9 l/ p0 B6 p程序例: </P>: ]$ k7 N1 E& \. ]* J% I( n! t2 R
<P><FONT color=#0000ff>#include <GRAPHICS.H>
0 o$ _3 V& Q, }8 X; b! s2 C' H#include <STDLIB.H>
0 d. w' K5 Q# A4 p/ l5 f" v7 J i#include <STDIO.H>2 A" @/ j0 Q9 O' m R$ d
#include <CONIO.H></FONT></P>1 r1 `3 H% k* X1 y6 |+ V/ L" o& g
<P><FONT color=#0000ff>#define CLIP_ON 1 /* activates clipping in viewport */ </FONT></P>
x4 W( }6 w& a% H9 o- L/ v<P><FONT color=#0000ff>int main(void) ! P$ h2 p* l* l( j: Q( k4 C: M; G7 F
{
0 H( R" T$ r4 w8 E N8 H: g/* request auto detection */ , `3 G* R9 f) ]# C: ^
int gdriver = DETECT, gmode, errorcode; </FONT></P>
5 a: {- G3 X+ N+ i$ `+ W<P><FONT color=#0000ff>/* initialize graphics and local variables */
4 m: |/ N, K/ N, }* hinitgraph(&gdriver, &gmode, ""); </FONT></P>
u: ~- `; u; ~( J; {1 M<P><FONT color=#0000ff>/* read result of initialization */
* \* M$ i' f0 q: u) }, zerrorcode = graphresult();
8 |, ]7 v: ?8 g- ^if (errorcode != grOk) /* an error occurred */
' {7 `8 R5 w' U/ M6 Q# K. i{
0 |# l3 Y: K) H, jprintf("Graphics error: %s\n", grapherrormsg(errorcode)); # C+ Y0 i, R; I" @0 A* X
printf("Press any key to halt:");
8 Z" E2 E" W5 {- L& ^" ggetch(); Q! t; @, w9 `/ m. P! [$ }. i! D
exit(1); /* terminate with an error code */ . i* f& H; g0 y. F, C- \$ k7 ]& v
} </FONT></P>( p$ G; |5 p4 ^6 n5 G7 I' k
<P><FONT color=#0000ff>setcolor(getmaxcolor()); </FONT></P>6 j5 h; V9 P) f; g* T3 f
<P><FONT color=#0000ff>/* message in default full-screen viewport */
+ S+ A" R P2 W8 R: B) y0 ]2 Bouttextxy(0, 0, "* <-- (0, 0) in default viewport"); </FONT></P>
% b9 O5 m: N6 h8 V6 }6 w4 C% a+ p( S<P><FONT color=#0000ff>/* create a smaller viewport */ ! o* v% z, n8 X; A& S c3 K
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); </FONT></P>
u/ V4 u/ ^3 s$ Z<P><FONT color=#0000ff>/* display some text */ 5 K3 F2 y# l* u7 _" [# e
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); </FONT></P>
8 Q+ p. z1 ^' i( D<P><FONT color=#0000ff>/* clean up */
y: E, o. j4 C7 Pgetch();
" ~. g1 s) s3 T& Qclosegraph(); * D. ]- }( a/ K9 b
return 0;
4 C3 H7 v" q3 V0 d; a( q* F- K} & a% V) U; q6 b6 e" D% u* h1 U& N
</FONT>
0 P5 [: F% e' I0 ?) r& v3 H5 d) c</P>6 R: k. U1 _6 ^! g
<P><FONT color=#ff0000>函数名: setvisualpage </FONT>+ y4 n. u4 Z0 ^8 I
功 能: 设置可见图形页号
: v/ u" E3 Q. i- @" T( T用 法: void far setvisualpage(int pagenum); / @6 n2 z/ h9 }% R Z# V& U
程序例: </P>" X$ ^. N* b2 k- O. t
<P><FONT color=#0000ff>#include <GRAPHICS.H>
- i1 M+ q6 y; K/ r' k. A#include <STDLIB.H>/ h3 q3 @ }: B. V1 {* g% h/ w
#include <STDIO.H>8 j$ l5 C/ [8 g* Q" Y( x
#include <CONIO.H></FONT></P>- _+ b* x; u6 j* @! y
<P><FONT color=#0000ff>int main(void) $ P7 p8 Z# ^6 G9 s2 [/ ~# T! T ~
{
. c! O( q% K# D/ ~! [/* select a driver and mode that supports */ , a. {1 l8 w+ ]6 ^ T. s
/* multiple pages. */ # z* e2 D" v2 v% W, ]" Y1 t
int gdriver = EGA, gmode = EGAHI, errorcode; 4 a* y& p" n% @. A- J
int x, y, ht; </FONT></P>% x' g$ j. ?4 i( a2 s4 T
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 7 V4 g% p+ J* S0 E" L5 t- Y3 u6 M
initgraph(&gdriver, &gmode, ""); </FONT></P>
\5 [2 t7 ]. ]; U2 X2 k! j. Q<P><FONT color=#0000ff>/* read result of initialization */ / F$ n% R' D' E
errorcode = graphresult();
8 y) n7 H5 H7 vif (errorcode != grOk) /* an error occurred */
% l& s& L7 w6 k( F5 \. Z{ , Q2 t$ |2 K. S) ^1 J; A9 R
printf("Graphics error: %s\n", grapherrormsg(errorcode));
M. [! y# t l' Sprintf("Press any key to halt:"); 2 ?9 n, q3 E0 B" [1 l* ~
getch(); & A) g8 s. b1 M! K: Y1 l0 L8 i
exit(1); /* terminate with an error code */
; D# u, p# z# X7 j6 y5 I} </FONT></P>' N) a$ _0 [0 O# u
<P><FONT color=#0000ff>x = getmaxx() / 2;
! _0 z5 x9 M) m6 xy = getmaxy() / 2;
3 x; b0 W- P: ght = textheight("W"); </FONT></P>
) K. j/ C4 i4 {# M7 |5 l5 X<P><FONT color=#0000ff>/* select the off screen page for drawing */
% Y/ g5 u* o! i9 e( S/ S$ t: ` Rsetactivepage(1); </FONT></P># g$ j9 q3 {9 Y
<P><FONT color=#0000ff>/* draw a line on page #1 */
2 S, B; S9 K. X) ^- `- Sline(0, 0, getmaxx(), getmaxy()); </FONT></P>* `% D! c. b* _0 W; [1 i
<P><FONT color=#0000ff>/* output a message on page #1 */ $ m% h1 e0 W" e$ Z, [) i/ l [
settextjustify(CENTER_TEXT, CENTER_TEXT); 6 }, E) m* h, T! s
outtextxy(x, y, "This is page #1:");
; U& B- O5 i1 z5 U+ @outtextxy(x, y+ht, "Press any key to halt:"); </FONT></P>7 k$ i( @& |2 c) B1 @% a% Z
<P><FONT color=#0000ff>/* select drawing to page #0 */ . N# J6 }2 p4 D% b
setactivepage(0); </FONT></P>
- E3 R) w& b3 T) A$ y<P><FONT color=#0000ff>/* output a message on page #0 */ 9 Q3 p l5 q; g6 |8 H# j
outtextxy(x, y, "This is page #0.");
: @8 H: Z" o, ]9 B/ j8 Iouttextxy(x, y+ht, "Press any key to view page #1:"); $ u) ^$ a4 s3 M7 w% D1 F( Q
getch(); </FONT></P>
. e! H' b( R6 m( |# u, M+ p$ L6 k8 q<P><FONT color=#0000ff>/* select page #1 as the visible page */ 4 E9 M2 V) H: w C
setvisualpage(1); </FONT></P>
0 k# a* N7 Y' z<P><FONT color=#0000ff>/* clean up */
4 f/ u+ T) b/ A" C: T0 [. W# y9 n. ^getch();
, ^* f9 E4 Y t! Iclosegraph();
! l/ |6 T0 e0 K- g" {return 0;
. z) d( w( G% G z( T} </FONT>
( s* c1 Y4 q/ {: t1 l) i+ H5 i/ f1 E. p5 W/ B; D1 ]. D7 r
</P>7 _" J! i0 z4 I3 w6 \. h) y
<P><FONT color=#ff0000>函数名: setwritemode </FONT>
6 R. C7 b1 P: J2 |8 `" `功 能: 设置图形方式下画线的输出模式 & C4 Z1 P. Q, f" s. N' n, f; R
用 法: void far setwritemode(int mode); 5 a' e; H# [ ]" D
程序例: </P>
2 n, g0 P; E& k<P><FONT color=#0000ff>#include <GRAPHICS.H>
6 Y" [' n: e }# C5 l7 V#include <STDLIB.H>
: P0 Y+ k6 S1 i, m9 c2 P#include <STDIO.H>$ C( c6 k. w' e
#include <CONIO.H></FONT></P>
1 z# e6 S: C3 y- K G. O! F* G0 n<P><FONT color=#0000ff>int main() ) [$ E8 `( |2 F/ A
{ ( C* M j/ p' E' [
/* request auto detection */ * w; Y, X" U# \ X& F
int gdriver = DETECT, gmode, errorcode;
% X. E( ^6 C) J# yint xmax, ymax; </FONT></P>" D6 M2 j& q+ s8 {' R" O
<P><FONT color=#0000ff>/* initialize graphics and local variables */
! w7 }7 G. I0 f4 a" ^6 X% u, B3 Minitgraph(&gdriver, &gmode, ""); </FONT></P>
; o4 R- o7 z9 ]9 m<P><FONT color=#0000ff>/* read result of initialization */ 8 i9 I4 Q3 a0 ^8 z( m
errorcode = graphresult(); 9 u* @+ `1 A& X$ ~+ B" A7 z$ a
if (errorcode != grOk) /* an error occurred */
/ _5 H& p0 M4 L) n1 ~& T{
; H( ^+ ^ K5 Z- q% n, p a# uprintf("Graphics error: %s\n", grapherrormsg(errorcode));
* D* Z6 D- U& D+ l5 Z: r6 fprintf("Press any key to halt:"); . ^ I# `# n" S' k: p+ ?5 r
getch();
8 Q2 W# [! l/ x! r' ~- wexit(1); /* terminate with an error code */
9 g2 O1 r7 X* {# @- Z1 ^# U' k) a} </FONT></P>
4 e& z6 `% X4 `% { {6 P<P><FONT color=#0000ff>xmax = getmaxx(); 9 x8 H- X8 U( A: U2 u! e
ymax = getmaxy(); </FONT></P>" S$ @) d( }1 e) i' D, a
<P><FONT color=#0000ff>/* select XOR drawing mode */ 8 {* A3 [( r8 Z; `1 N! `, D! h
setwritemode(XOR_PUT); </FONT></P>" y1 x" v0 G/ s2 D. s4 r4 V" |; c( e8 m
<P><FONT color=#0000ff>/* draw a line */
) @8 z# {) b! O' i+ ^. N3 Lline(0, 0, xmax, ymax);
; T2 }$ X" ^& Hgetch(); </FONT></P>
7 ?) V6 y* K |6 F' y' |. N5 i<P><FONT color=#0000ff>/* erase the line by drawing over it */
' z7 @) q% ^' C# {6 j3 uline(0, 0, xmax, ymax); % l# J+ E4 N! T% X* |9 ~
getch(); </FONT></P>
8 V' p- Y {& K- H<P><FONT color=#0000ff>/* select overwrite drawing mode */ O. p2 r3 J1 E! L w v5 s8 ~) I6 t* [
setwritemode(COPY_PUT); </FONT></P>2 {5 S; W+ K' ^' P" A, H3 t9 {/ U! N* o
<P><FONT color=#0000ff>/* draw a line */ 9 h! T: H. T# l
line(0, 0, xmax, ymax); </FONT></P>, z* P) |$ E8 Y7 l: S; e
<P><FONT color=#0000ff>/* clean up */ ) M# q, U# a8 t: i: B+ {( \6 k
getch(); 4 o! ^6 X% X6 q: C
closegraph(); / Y* h: u$ r0 I8 B: Q- E( }
return 0; * P+ J* n- p' b/ I- }1 C2 |
}
$ D' n! X' B" n0 z7 v% U</FONT>
% n* P# ^ J$ l+ l( X</P>
1 T$ I& }* h7 t* I$ U<P><FONT color=#ff0000>函数名: signal</FONT> : l$ Q% D- A7 _' h: b/ K5 q
功 能: 设置某一信号的对应动作 6 v l) W o$ H) |/ l# ]
用 法: int signal(int sig, sigfun fname);
9 R* N3 m% v: d! d% E程序例: </P>7 Q# I1 i+ K( z3 O! s
<P><FONT color=#0000ff>/* This example installs a signal handler routine for SIGFPE, : n3 J4 |! Q8 S# v
catches an integer overflow condition, makes an adjustment . Q( U7 X+ `" k% |1 Y
to AX register, and returns. This example program MAY cause - Q" Z! w' g7 u4 C j: w
your computer to crash, and will produce runtime errors ! d/ t4 L) q% \
depending on which memory model is used. / b+ h7 K: i# Q" c! b# a
*/ </FONT></P>8 X5 @, e' A2 P0 \/ Z$ y
<P><FONT color=#0000ff>#pragma inline
# g6 Y2 T) _# @3 g& k#include <STDIO.H>- m+ m7 Q8 t) h% G
#include <SIGNAL.H></FONT></P>; u- B1 d( O9 q) |( \7 I' x) p
<P><FONT color=#0000ff>void Catcher(int sig, int type, int *reglist)
* n: j% j( U( x1 u{ ! h4 \) }8 K. j/ v/ Q' p) R
printf("Caught it!\n");
% S1 s0 y+ M% m8 @# Q0 i% q9 D*(reglist + 8) = 3; /* make return AX = 3 */ ' n3 A4 e( x$ x9 p
} </FONT></P>. p& T0 s& Y7 } o' D
<P><FONT color=#0000ff>int main(void)
2 h$ f' P9 @; [; |# s1 Y{
) c4 J: y; R# \4 K; dsignal(SIGFPE, Catcher); 0 q% `3 J' [! {/ \; i- U
asm mov ax,07FFFH /* AX = 32767 */
& D2 H; }4 k8 Y- v6 D, kasm inc ax /* cause overflow */ $ m9 }3 h/ [$ X* F0 m. h! M- U1 w
asm into /* activate handler */ </FONT></P>
; M! W% s9 q, p/ R( H5 T<P><FONT color=#0000ff>/* The handler set AX to 3 on return. If that hadn't happened, 9 \. `9 h O: c$ u; y8 G3 v
there would have been another exception when the next 'into'
) L" f6 v1 M/ u7 ^" b: q( |" kwas executed after the 'dec' instruction. */ 5 o4 M, ?5 ]9 d
asm dec ax /* no overflow now */
! Q9 B7 U8 j# x5 {# {6 Dasm into /* doesn't activate */ 5 Z2 P/ L* [' D2 V
return 0; * E9 B$ |4 Y7 |& r/ q$ A* d
} . z q" X9 N' W8 c0 _
& N3 _. @1 q: c( ?
' u; R- P* @" b8 U% Q: ~</FONT></P>( `, w% o* w. l8 S* J. M
<P><FONT color=#ff0000>函数名: sin </FONT># f- r, ~, X( ~3 _8 l
功 能: 正弦函数 7 s8 M3 [1 Y7 V
用 法: double sin(double x); 2 M+ |( {( b7 ]: Q
程序例: </P># L( Y% z* X( `. U
<P><FONT color=#0000ff>#include <STDIO.H>
, _# g3 e" x& w! y#include <MATH.H></FONT></P>
4 q; K x3 t9 f- ~! M! J% v3 G<P><FONT color=#0000ff>int main(void) ) @* R9 p: I0 \+ O# Q( ?
{
0 X: I$ W% h% `. jdouble result, x = 0.5; </FONT></P>8 b2 W$ U# S& f8 r$ g8 |- P
<P><FONT color=#0000ff>result = sin(x); 4 m4 `* {# S3 L- A* A
printf("The sin() of %lf is %lf\n", x, result); 7 t& S% @) B5 q% \4 z
return 0;
* f- n; d9 K6 C' x! V}
6 E& D( T- }& _1 L: X @% W! j1 {. _$ S; e% N$ F) n; Y6 y1 e& k+ y
</FONT></P>
7 p. Q' |4 g c3 }. l<P><FONT color=#ff0000>函数名: sinh </FONT>: E0 e' j' ^: X6 U
功 能: 双曲正弦函数
5 `( L4 e$ _/ y& I9 T+ q, w+ W用 法: double sinh(double x); & G% {% q% m" K( E, c3 d
程序例: </P>
* ~' P- ^3 D4 Z+ h1 P<P><FONT color=#0000ff>#include <STDIO.H>4 X0 k2 [9 \2 M9 z/ r$ A! J
#include <MATH.H></FONT></P>
' C, D8 V! e# K<P><FONT color=#0000ff>int main(void)
& ]- ~" X. }; g0 ]6 Y( U. i{ / E7 g- l% _. U- r4 } l; u) ]3 _- C
double result, x = 0.5; </FONT></P>+ C) o/ U% L( t! u8 F2 j
<P><FONT color=#0000ff>result = sinh(x);
6 s/ B, N5 M( X5 n U$ M, G) Hprintf("The hyperbolic sin() of %lf is %lf\n", x, result); 9 s N) A' ~2 S6 _! c' Z
return 0; & F5 Z8 a( b: Q0 O+ `0 H3 x' r
} 3 h0 W. K6 d* S- w/ {) C1 e# n- r
% e" e ?! K; x) m1 n+ [6 N! Q9 ^
3 s) Q8 p' T# @. U# v' C8 y- R</FONT></P># \5 k/ u$ [8 A3 `" ^
<P><FONT color=#ff0000>函数名: sleep </FONT>
p4 c8 ?- ]$ {* v0 u6 J功 能: 执行挂起一段时间
8 c6 N" z$ u* g x+ w' a+ I/ t用 法: unsigned sleep(unsigned seconds);
0 E) e7 z, g o& S9 ?程序例: </P>
' [. B$ B5 p7 A0 z<P><FONT color=#0000ff>#include <DOS.H>$ ^( g6 E9 o; d' \
#include <STDIO.H></FONT></P>
& r# [9 w5 Z2 N- B2 d# b<P><FONT color=#0000ff>int main(void) / F: L( h2 i2 [& O
{ - x: l/ l! Q: ?$ y( g2 C$ Q+ i
int i; </FONT></P>. C8 ^6 B# x. R( p# b/ O
<P><FONT color=#0000ff>for (i=1; i<5; i++)
% m& P5 }9 x J6 `{
5 g3 D+ S* M! u( M0 n# `9 oprintf("Sleeping for %d seconds\n", i); - a1 f$ t8 T" g0 a. z" O* @
sleep(i);
+ Z# |' a' D9 l4 V, N3 }. d6 S}
. r* h8 |+ p9 ]) l- O8 [return 0;
0 F+ e' _. n1 ?1 e0 Q% p}
4 O8 `3 E# B+ r% { h/ c5 E0 n; W3 I/ i& @6 R1 H0 R
</FONT>8 |) d- w" ^; }' t9 `- c
</P>
$ T& t, h f& G8 H. j<P><FONT color=#ff0000>函数名: sopen </FONT>3 x3 {# y- ^5 a
功 能: 打开一共享文件
/ D' |. g8 W8 [" H+ P* y x3 k用 法: int sopen(char *pathname, int access, int shflag, int permiss); : z2 E' i1 E$ }* j: j: P
程序例: </P>
6 J7 g8 D0 C$ u+ @ b* H- u& z3 v- x<P><FONT color=#0000ff>#include <IO.H>
6 L, G6 l# F% W! t1 N#include <FCNTL.H>
3 I3 J+ l$ ~: I! q. R! k$ M#include <SYS\STAT.H>1 _5 z' }( ]4 n. B- W! k
#include <PROCESS.H>
8 k" Z D+ Q' a; y- p#include <SHARE.H>
( ~# J" W' v0 y& h#include <STDIO.H></FONT></P>) M+ T- e; w1 W: y0 _
<P><FONT color=#0000ff>int main(void)
. n2 t& R5 ^. L6 b# ]{ 1 S2 p( }5 u+ p' \' c" }. w' h
int handle;
. R) u0 W# y# S" e. q1 ^7 Bint status; </FONT></P>8 G$ \8 r: v6 @8 u& r
<P><FONT color=#0000ff>handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYNO, S_IREAD); </FONT></P>
+ [4 |, [+ I! A" f+ j7 o<P><FONT color=#0000ff>if (!handle) ! H: @+ d) k% F' @
{
2 W' Q0 o0 i8 ^7 t* Fprintf("sopen failed\n"); 6 E- y/ s! _' H# O
exit(1);
4 E1 R0 x0 u* n5 ^} </FONT></P>
5 J5 H' j n( M( X, h3 F2 j<P><FONT color=#0000ff>status = access("c:\\autoexec.bat", 6); # |8 X' t2 E8 c# j
if (status == 0)
8 {0 |5 I a5 C: X& W% Sprintf("read/write access allowed\n");
, i4 _( R! f, G* f# [; B8 d( felse
4 p6 T6 o, e, ]7 K: i9 S/ eprintf("read/write access not allowed\n"); </FONT></P>
1 h! X( R6 [! g<P><FONT color=#0000ff>close(handle);
2 K& ^: j& S/ V: B9 hreturn 0;
- z9 O/ f: [% r4 K2 J: W}
+ c8 D( }$ o3 a7 }- q$ ~/ ~) _: W; d, B8 \* t
</FONT># G" }3 [1 Q$ c
</P>
) W7 U9 t* c9 S<P><FONT color=#ff0000>函数名: sound </FONT>
4 b* [# h! ?2 F& G* t# _功 能: 以指定频率打开PC扬声器
: _( Y9 Q+ V3 W% b: {+ Z$ D用 法: void sound(unsigned frequency);
' c, `) X& }, Z" a6 U程序例: </P>
+ i C! O) T) Q<P><FONT color=#0000ff>/* Emits a 7-Hz tone for 10 seconds.
5 U3 ? g; _2 }7 d. v2 BYour PC may not be able to emit a 7-Hz tone. */ " a! P6 E8 e; N; ^1 r
#include <DOS.H></FONT></P>6 M/ d% I0 {! f8 L O7 G3 M
<P><FONT color=#0000ff>int main(void)
" }$ j4 J8 y# t7 E9 n{
6 }! B5 c7 M7 i* K3 wsound(7);
{- X* z* T2 L( mdelay(10000); - e& l: G- N( H# g8 o/ F% j7 o! d8 U
nosound(); 2 D+ b5 i, r' L, X7 ~
return 0; : M/ U' F& q% z4 \! H
} </FONT>
& G4 d6 H3 w6 |# w* Z( [, a3 i9 `1 ^6 N
0 l$ U7 l: c5 z1 v" Q1 z/ E; X4 O; a</P>
6 T( ?$ }0 ?5 k' `+ ` s% Y+ r) t8 b<P><FONT color=#ff0000>函数名: spawnl </FONT># g$ D6 w( U7 S1 k$ {5 P
功 能: 创建并运行子程序
1 p' E% |' {! x# _用 法: int spawnl(int mode, char *pathname, char *arg0, 4 f) \5 ~4 _% t8 a
arg1, ... argn, NULL);
: V" H* e$ p& d9 u, a4 u1 t- Z( z6 p! s程序例: </P>
9 b r5 V7 m o# E! G6 Z# ~<P><FONT color=#0000ff>#include <PROCESS.H>
: m- Y+ D4 |! W6 ~' C#include <STDIO.H>! h) `! H8 P' k) V, O
#include <CONIO.H></FONT></P>8 K) V# \: r" W% R/ h( y4 h! b% J
<P><FONT color=#0000ff>int main(void)
: ` U. V! j) z" }: c/ R# W1 z{
3 t+ ], S2 J! w, P# z6 Zint result; </FONT></P>
" Y6 L, [/ M* d' w# |<P><FONT color=#0000ff>clrscr(); 4 G+ {6 _* p3 ^' c5 q
result = spawnl(P_WAIT, "tcc.exe", NULL); - s+ V6 P+ I. L9 y: B
if (result == -1) & H+ J0 ]/ ^5 \4 A. j5 |( e
{ ) ], R) q) N6 _! z1 m
perror("Error from spawnl"); ! Z; G6 ]/ s7 A; j
exit(1); v" D" \1 C* }
} * F0 J8 U3 j9 j; m
return 0; - z O% }! U7 X: x& ~
}
0 }; [ O. f* S/ \+ R. B* R( |</FONT>
# t2 Q3 Q: c3 E( _! w+ }</P>" |! z3 ]' F2 q
<P><FONT color=#ff0000>函数名: spawnle </FONT>
$ Y8 ]6 M& e2 @! `* M& d功 能: 创建并运行子程序 ; @" v$ m- N; Z J% i4 e) M! W
用 法: int spawnle(int mode, char *pathname, char *arg0,
$ R7 k: L" ]4 f* q$ d7 m varg1,..., argn, NULL); % G- ^) {0 ^) f/ ^# T
程序例: </P>
! _1 C; o" d! T9 G( i4 N6 ?1 ~<P><FONT color=#0000ff>/* spawnle() example */ </FONT></P>9 ^' I$ q1 M& N* s" W( V2 M& q
<P><FONT color=#0000ff>#include <PROCESS.H>6 {" F5 Y7 X8 Q+ E
#include <STDIO.H>
Q& O2 ~6 P; z M. }# c#include <CONIO.H></FONT></P>. j! M% f9 O$ ~& m2 s
<P><FONT color=#0000ff>int main(void) 3 J% W- L0 ^, w4 A5 ^
{ - D8 r5 U ]) y4 j
int result; </FONT></P>2 S9 }# y1 `4 O k6 P# F
<P><FONT color=#0000ff>clrscr();
0 r/ i2 ~$ G- A/ w3 x' D! aresult = spawnle(P_WAIT, "tcc.exe", NULL, NULL);
' p. I- `) n4 k. i* ]3 Z' @if (result == -1)
1 X, Z. D: @& W6 ~; U( e; G- V7 H{ 1 r* W9 r9 H8 X8 L: u- s
perror("Error from spawnle");
9 X8 D* S- b! u- w$ ]2 M' l3 p6 f/ jexit(1); ' O; t, z7 W' ?- `9 E& c+ S
} * ^+ P, J7 P3 t, ~9 `3 A
return 0;
' O+ J$ A- s& B9 `: z}
! K3 C5 ]! }8 E
/ J# [2 c' u* e C) [% k# ]; O% ~* g7 g( [
</FONT></P>
3 r$ F) |& p9 H6 E, ?/ n<P><FONT color=#ff0000>函数名: sprintf </FONT>
* Q& d1 P+ Y1 V, m- r功 能: 送格式化输出到字符串中
: T, k; s+ _5 y" ?1 |" {* _3 P用 法: int sprintf(char *string, char *farmat [,argument,...]); 3 y& g8 ]/ o$ ?% Z
程序例: </P>" W5 I' E. G- @6 O$ T
<P><FONT color=#0000ff>#include <STDIO.H>( i% d* K- K* ^& k9 W, F
#include <MATH.H></FONT></P>5 V, H% b3 S8 _7 ]7 m
<P><FONT color=#0000ff>int main(void) ! H; b% I7 j' } b4 X, }
{
9 Z4 W( G% N N) A% S1 Fchar buffer[80]; </FONT></P>
( r3 O, k- m5 ?1 c<P><FONT color=#0000ff>sprintf(buffer, "An approximation of Pi is %f\n", M_PI);
& @9 ~4 w" O( c' u# Vputs(buffer); : o$ o, M8 F1 y8 T- v1 i
return 0;
. v1 _; t1 P8 Q; _0 p. N}
# D) R$ G: A# Y7 O0 E1 F% F</FONT>
: _" t" o* A3 f+ Y1 p</P>
4 c H) [7 t! ~( I/ H8 e9 I3 I4 U<P><FONT color=#ff0000>函数名: sqrt </FONT>7 K$ Q! t6 k4 j
功 能: 计算平方根
+ M) W. F; ]7 s$ F# Y用 法: double sqrt(double x); ) E1 c) [0 \) X
程序例: </P>
! R0 y* W3 h, e' ~+ |- F, h<P><FONT color=#0000ff>#include <MATH.H>' o. x7 q1 n0 B" H6 I$ _# j+ {
#include <STDIO.H></FONT></P>
h3 M: n4 x) G8 [: ^<P><FONT color=#0000ff>int main(void) " d1 }4 ~- L9 a, ~3 X
{ , y+ g* {; e. H5 {) i6 Y
double x = 4.0, result; </FONT></P>' ?1 C5 k2 S; s, c. y
<P><FONT color=#0000ff>result = sqrt(x);
* g8 b- V; c! mprintf("The square root of %lf is %lf\n", x, result);
- C- H, w6 X6 ]2 \* H+ sreturn 0;
0 G2 Y4 H; M; f- ~9 y}
. C: c1 v9 j+ P' ?/ Q5 U2 z6 [</FONT></P>$ U4 b7 }% `1 J+ T# C$ _
<P><FONT color=#ff0000>函数名: srand </FONT>
( G$ F1 h8 \7 B功 能: 初始化随机数发生器
# D2 d I& q! T* I用 法: void srand(unsigned seed); 9 E! A5 [8 d5 ?
程序例: </P>
5 m2 k& c4 X, c9 K. U( d. h<P><FONT color=#0000ff>#include <STDLIB.H> n' _2 R0 E& n2 K$ N
#include <STDIO.H># G6 x' }! r1 I
#include <TIME.H></FONT></P>: y9 J( p) o, R5 z& X2 H- @; K
<P><FONT color=#0000ff>int main(void)
0 Z7 w. a4 I; T! H2 x) [# p{ & g9 j8 p( H$ } l o: {
int i; % j& R$ Z/ n! h% f7 K/ z5 [% J
time_t t; </FONT></P>
1 A0 i- a- I* J9 G1 Q<P><FONT color=#0000ff>srand((unsigned) time(&t));
m, ]4 B: `$ h2 w5 F- Q1 s6 ^printf("Ten random numbers from 0 to 99\n\n"); ( t3 M2 _! k* ]( L. z6 r
for(i=0; i<10; i++) / i( l f I4 L1 v
printf("%d\n", rand() % 100);
2 g, Y$ h5 ?, I( C6 }! ereturn 0; , e. p8 m' [* @0 \# C8 E
}
0 n, Y3 H. |- a! t3 j</FONT># D. M8 i$ P" }
</P>' w8 H- K0 H+ r
<P><FONT color=#ff0000>函数名: sscanf </FONT>3 |& p% w* | O) |. ]5 { ?
功 能: 执行从字符串中的格式化输入
" N" [+ [/ [/ ~* E用 法: int sscanf(char *string, char *format[,argument,...]);
. N4 J6 r& R5 g程序例: </P>
0 W" u# W( T- ^9 N; S% M) D<P><FONT color=#0000ff>#include <STDIO.H>
/ \1 F; c1 ?( q* ?! i( `: o#include <CONIO.H></FONT></P>0 E3 I* D. z) r2 a5 ~
<P><FONT color=#0000ff>int main(void)
& |# K) l5 h% J! [' c1 _$ c u{ 6 E2 b6 Z$ R3 ]. x
char label[20];
i8 D, y% {3 \. L! j8 i5 e! _char name[20];
* o( [' f# ~. Z$ W) B& w% E* Dint entries = 0;
" n0 s4 p1 V j, ~* o, v0 x; Pint loop, age; 5 R6 [, F& c7 i j
double salary; </FONT></P>
- P9 \5 @( m; a) `0 V<P><FONT color=#0000ff>struct Entry_struct $ {+ h6 `$ z( H3 P
{ 4 m. E" a* s/ ^& _# I; C0 F! e
char name[20]; ' q$ F7 e7 c( u M' c1 Q- o
int age; H( b4 e* E1 e! z8 f7 x$ @3 L
float salary;
8 D, e! P* o, Q$ @7 g; ^} entry[20]; </FONT></P>, N( Y1 G5 O8 p% ]/ `
<P><FONT color=#0000ff>/* Input a label as a string of characters restricting to 20 characters */
& V9 r% O' u/ k; h* r& c. yprintf("\n\nPlease enter a label for the chart: ");
6 k' U8 W u4 s+ H1 l7 {" p' ascanf("%20s", label); : p% H1 j8 X0 d* R( c
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>( h6 |8 K, Q, C# R9 F8 t+ z$ |5 ~4 F- G( f
<P><FONT color=#0000ff>/* Input number of entries as an integer */
* d7 w! x9 E$ M9 Yprintf("How many entries will there be? (less than 20) ");
$ y2 n8 N6 G4 \% T6 K0 d3 D7 {scanf("%d", &entries);
8 z1 T+ T: {* jfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
3 v1 G3 r/ ~$ N' e. g' e<P><FONT color=#0000ff>/* input a name restricting input to only letters upper or lower case */ 4 @+ b0 f3 q" \
for (loop=0;loop<ENTRIES;++LOOP) * m# o4 L7 j2 a
{ ) {8 `) C% Q! E# r3 e: Z7 @
printf("Entry %d\n", loop);
$ b2 x5 [3 Y0 U( s# |( ^printf(" Name : ");
, b8 W2 P% ~4 Q9 o# Q) A3 escanf("%[A-Za-z]", entry[loop].name);
# l3 d1 w- L2 Y9 xfflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>$ I$ d6 v; h" ~, n. Y# p
<P><FONT color=#0000ff>/* input an age as an integer */
: U/ U0 W$ a9 ~- P0 ~$ Cprintf(" Age : "); # |6 G. {: v$ P' |
scanf("%d", &entry[loop].age); - i+ D1 k. S% T8 }& S# a( q
fflush(stdin); /* flush the input stream in case of bad input */ </FONT></P>
) s0 g3 `. b2 N q' E9 l<P><FONT color=#0000ff>/* input a salary as a float */
3 i; a$ T& W+ j n/ Q8 W7 Cprintf(" Salary : ");
/ S, ^# t; H: {5 z9 u9 P# F3 yscanf("%f", &entry[loop].salary); " Q5 w( a0 a3 K& Z9 J! z, a' c3 @
fflush(stdin); /* flush the input stream in case of bad input */
) a2 Y5 p' ~, k* q+ a) q0 |} </FONT></P>
0 I) S: q7 K) ]- O2 `1 R! Q<P><FONT color=#0000ff>/* Input a name, age and salary as a string, integer, and double */ ) U9 x3 W2 }. @9 G, k
printf("\nPlease enter your name, age and salary\n");
0 U' u; p0 }* x" }scanf("%20s %d %lf", name, &age, &salary);
- ?3 Y% [1 @; Z' B" x: ?</FONT></P>$ C/ c4 K5 g4 Q" s" o+ J* S o
<P><FONT color=#0000ff>/* Print out the data that was input */ . ^9 H& S8 J9 B2 i& B1 \
printf("\n\nTable %s\n",label);
4 j8 ?$ N& a9 L6 a# Uprintf("Compiled by %s age %d $%15.2lf\n", name, age, salary); + w, }& ?! v) |+ g+ W- M
printf("-----------------------------------------------------\n");
" S4 l' B) r% w* e. q! y2 {* xfor (loop=0;loop<ENTRIES;++LOOP) " T. A# r* |7 ]1 Y
printf("%4d | %-20s | %5d | %15.2lf\n",
" f9 Z9 {# m( X$ X* }- Z6 kloop + 1,
; w5 L& s, G2 v- ~/ l1 q0 R, K4 Qentry[loop].name, + k4 y- g; l T& }; ?
entry[loop].age, 0 ]: S0 ~$ B! j9 {; ^& b) C9 x/ a
entry[loop].salary);
& S0 K3 ?" T2 ~3 d- cprintf("-----------------------------------------------------\n"); 4 M6 u, \( w$ I0 H3 y/ ]6 ^. c
return 0;
" d0 c$ S/ b7 X}
- H7 k; a# \7 M! E9 l
& g" e8 ]" m5 c! m1 L# [* Y3 z</FONT></P>
7 r* a9 D8 u+ Q0 A<P><FONT color=#ff0000>函数名: stat </FONT>
# p4 q7 [1 ], p9 w" h8 v1 k; t: }: E功 能: 读取打开文件信息
0 X; E+ z7 R8 l4 S用 法: int stat(char *pathname, struct stat *buff); 0 y6 \2 |1 \7 s7 ?6 T
程序例: </P>. q* D% l) L! u
<P><FONT color=#0000ff>#include <SYS\STAT.H>5 j$ e/ c" q7 w% q, S5 [' t4 ?# b
#include <STDIO.H>- @6 Y& B2 v" y" \3 o: q2 o7 E
#include <TIME.H></FONT></P>* Y- u$ M I. i8 {2 Q6 r8 o
<P><FONT color=#0000ff>#define FILENAME "TEST.$$$" </FONT></P>, K1 X9 C4 j' t. c( K$ l% u
<P><FONT color=#0000ff>int main(void)
3 r+ y. c$ g0 N; e% ]( r" O, I{
& j" E6 K- N# s8 L# ustruct stat statbuf;
3 j8 r. j0 }- u* Q# c6 A* }FILE *stream; </FONT></P>
6 j3 h& O( n( X: E, M<P><FONT color=#0000ff>/* open a file for update */ 1 N; O- s. n2 \4 a. P. r0 E
if ((stream = fopen(FILENAME, "w+")) == NULL)
) p% l: ~" A+ @2 J% S/ z, ?$ n- h{ # |1 x/ z# h w$ _6 S
fprintf(stderr, "Cannot open output file.\n");
c( [6 Z% e! c ^: p) T" Ureturn(1);
9 n+ N) F2 r+ V6 u. a; I} </FONT></P>
9 f/ t' t% E( F" p<P><FONT color=#0000ff>/* get information about the file */
2 j$ o% n( u; B# B, k) k$ S2 G3 Pstat(FILENAME, &statbuf); </FONT></P># ?6 S6 p& y7 q9 b; n
<P><FONT color=#0000ff>fclose(stream); </FONT></P>5 j E8 g( L' a/ e6 O
<P><FONT color=#0000ff>/* display the information returned */
. F, W$ `9 |9 I$ i8 uif (statbuf.st_mode & S_IFCHR) 3 S. t7 m' e. Z3 [, c4 G X
printf("Handle refers to a device.\n"); ( ]0 w6 r6 h/ q: Q7 v& h
if (statbuf.st_mode & S_IFREG)
+ i6 L1 {/ a9 k- ^# L; Aprintf("Handle refers to an ordinary file.\n"); + F N$ F- s1 T: \( u& U; i+ [
if (statbuf.st_mode & S_IREAD) , u9 j6 m2 M- u6 A: ^6 L% Z
printf("User has read permission on file.\n");
% y9 u O% X; `if (statbuf.st_mode & S_IWRITE) 6 b9 M. s) t- R% l0 q, D
printf("User has write permission on file.\n"); </FONT></P>0 U, N" Q' r- `/ ~
<P><FONT color=#0000ff>printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
" O; G4 K5 |# x/ gprintf("Size of file in bytes: %ld\n", statbuf.st_size);
# i: |9 x7 N) _1 E" l/ ~; ]printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
$ E9 f( c: H/ Xreturn 0;
7 D6 p$ W! s# p8 Q. h, ?}
# _+ g8 d1 y% l/ N
5 j* D/ H' c( H</FONT>0 J( y4 |, d0 `; @4 R* G- ]+ Y2 ^
</P>
6 V. ]9 D! M6 j8 u; |: _' u! Q- ^4 g<P><FONT color=#ff0000>函数名: _status87 </FONT>$ D8 S" V3 _$ L8 `6 x9 H( X
功 能: 取浮点状态 0 o# E) T8 |2 h! w/ U5 y
用 法: unsigned int _status87(void); 5 t; u9 ~9 F7 a0 L
程序例: </P>
& ]: e z J8 u+ C<P><FONT color=#0000ff>#include <STDIO.H>
; a( _' E% }2 x% f: Q#include <FLOAT.H></FONT></P>
0 Z/ k' O3 u0 b& ~/ k' W<P><FONT color=#0000ff>int main(void)
H6 Y2 n p4 V" ]3 s% O# C1 ?{ - w; K1 c; `' }$ v
float x;
- w" u) g/ o9 [/ {0 I: U+ r' pdouble y = 1.5e-100; </FONT></P>0 R+ Y! E/ _1 ?% a; h; S9 I
<P><FONT color=#0000ff>printf("Status 87 before error: %x\n", _status87()); </FONT></P>( D/ c9 P) D. Q) c
<P><FONT color=#0000ff>x = y; /* <-- force an error to occur */ ; W M) f, g; T
y = x; </FONT></P>
# l1 C3 x' }) `4 \# L% U- _! p<P><FONT color=#0000ff>printf("Status 87 after error : %x\n", _status87()); - y( x3 a+ Q- Z1 q4 Z
return 0;
4 J0 n- d2 l ` e}
- s/ C: g. t/ I</FONT>) r( q: d$ Y3 a' F
</P>
; ]; {7 d) [" o! ~<P><FONT color=#ff0000>函数名: stime </FONT>
8 d/ A2 P; X" T* s3 e% y功 能: 设置时间
) Q/ O" q) b- v; P. w( H用 法: int stime(long *tp);
% p- {4 _. W9 g程序例: </P>
2 |: T5 H* x0 ]" q<P><FONT color=#0000ff>#include <STDIO.H>* o4 c* Q. s$ ?3 j; v$ g
#include <TIME.H>5 `: l" e) j2 p; w1 w
#include <DOS.H></FONT></P># y6 N# S" C! V8 i; J4 P9 i
<P><FONT color=#0000ff>int main(void)
& q0 [- G w" |% o. S{ 9 M5 Y! L) G9 ]
time_t t;
6 K. F6 c8 w- {/ ^' l3 L& ystruct tm *area; </FONT></P>& V! w& ]! Z+ n3 [7 G( `
<P><FONT color=#0000ff>t = time(NULL); * w+ r `) h# ]" a% B
area = localtime(&t); # Y& |6 G' L$ {+ { m
printf("Number of seconds since 1/1/1970 is: %ld\n", t);
6 u, M5 X3 O' v! q' G% J7 w" l+ u( i, dprintf("Local time is: %s", asctime(area)); </FONT></P>
9 }4 B' }, J1 t& n0 q" c5 Q<P><FONT color=#0000ff>t++;
& K: Z+ \, ^/ j5 Jarea = localtime(&t);
# n" I$ U% D8 H) Eprintf("Add a second: %s", asctime(area)); </FONT></P>
3 o0 ?0 v- w% G+ l, C) J O<P><FONT color=#0000ff>t += 60;
1 Q, \! D5 m+ a( J/ Z3 Y tarea = localtime(&t); - ?; i. U6 X* M+ R
printf("Add a minute: %s", asctime(area)); </FONT></P>
) X. p/ X+ g$ J- q9 I+ p<P><FONT color=#0000ff>t += 3600;
+ w' z! P6 p; P& oarea = localtime(&t);
9 a3 A! Y8 m2 G. a1 ?. I$ }- Pprintf("Add an hour: %s", asctime(area)); </FONT></P>
5 b( Z0 |$ N: _; c. P<P><FONT color=#0000ff>t += 86400L;
/ A1 M# B; h6 A) Z2 Rarea = localtime(&t); # @# e& d( p$ @. l% N0 h
printf("Add a day: %s", asctime(area)); </FONT></P>
' {4 @' y% l# d. B5 P/ j/ H<P><FONT color=#0000ff>t += 2592000L;
9 Q) f+ P2 R- @+ ~) x# yarea = localtime(&t); 6 z% }1 j) L& {9 f+ S; e" H9 k
printf("Add a month: %s", asctime(area)); </FONT></P>
/ \* {) c3 s! p9 Z( U( |" N<P><FONT color=#0000ff>t += 31536000L; 6 F3 n4 C5 G$ y
area = localtime(&t); 0 _6 d2 t% W# i [
printf("Add a year: %s", asctime(area)); $ n& R* y5 m1 C! X
return 0; 1 z- u6 I' ], P. m
} </FONT>
& f ^, B6 T6 }" A" T8 ?: l, q! }8 j# \, Z3 g, w! F+ o
; B% H8 n: `! r$ U* v, O
</P>
; r' C; A; O( I) I& d<P><FONT color=#ff0000>函数名: stpcpy </FONT>$ U) V# T9 W8 i4 }
功 能: 拷贝一个字符串到另一个 ; k0 b- r" `8 v
用 法: char *stpcpy(char *destin, char *source);
& X @* g2 g& s% a+ i1 b" M程序例: </P>8 B& ^' x1 u% \
<P><FONT color=#0000ff>#include <STDIO.H>
/ }9 p- I7 a2 l$ p8 d#include <STRING.H></FONT></P>
, |3 g, @# J, s<P><FONT color=#0000ff>int main(void)
& G/ @& B% j& M& ^; Q4 V{
: C3 S( k L/ u+ _& Bchar string[10]; 5 T0 Y: z+ P9 p( b2 r8 C: j0 z
char *str1 = "abcdefghi"; </FONT></P>
+ O, A& s7 I( ^+ ?/ v8 A/ Z: L<P><FONT color=#0000ff>stpcpy(string, str1);
1 r' B- L {1 o) h3 Z0 n7 s, _printf("%s\n", string); ! a0 A: @+ M- u: c/ v0 }) H2 `) l
return 0;
- J" v h- L2 @+ j# o/ n t# W} 6 k* a& B- T! ~; x: i
( x9 |+ v s& A9 u
</FONT>4 Q4 E2 }4 I" h! e3 M/ X& o
</P>) ?3 `2 O5 c/ m( B
<P><FONT color=#ff0000>函数名: strcat </FONT> |6 M z2 m) M, W3 C
功 能: 字符串拼接函数 , N- W6 W: C" E7 R6 M8 J1 f
用 法: char *strcat(char *destin, char *source); 7 _% ?# B+ z* r( E# S% H$ O' t
程序例: </P>
& s _7 |9 t g( ]- [<P><FONT color=#0000ff>#include <STRING.H>
' Z* j: u# R; y#include <STDIO.H></FONT></P>
3 f; ?8 d- s- U( j, L8 @<P><FONT color=#0000ff>int main(void) + j2 B# S& a: e
{ . q, J9 U& R+ w
char destination[25]; ' x( k- W3 U1 P: C
char *blank = " ", *c = "C++", *Borland = "Borland"; </FONT></P>
% h9 @7 C8 r* T4 U- B% g/ h<P><FONT color=#0000ff>strcpy(destination, Borland);
1 U2 H9 Q# Y7 ^0 D) O5 I0 tstrcat(destination, blank); & }/ E5 u+ y/ x+ X
strcat(destination, c); </FONT></P>7 M8 s6 i C u% m9 F
<P><FONT color=#0000ff>printf("%s\n", destination); 5 [) L! ], ~2 I2 f" e$ ?
return 0; . N6 J3 `7 Q9 F9 r7 Z: z4 y/ G6 z' l
} 3 d9 k& \) \6 G$ b9 W/ C0 E
3 y' H- D# B% J</FONT><FONT color=#ff0000>
. R2 @* H6 |) `. G</FONT></P>
2 C8 T( Y7 o! b- z/ A# E<P><FONT color=#ff0000>函数名: strchr </FONT># x. c0 H, S3 S6 x" R
功 能: 在一个串中查找给定字符的第一个匹配之处\ # Z+ _- N( P# m. q
用 法: char *strchr(char *str, char c); * W$ N8 u2 m0 }4 |. X7 S6 K/ x% H: B
程序例: </P>
1 Z3 }: x( o ^/ ]2 ^& Q. }<P><FONT color=#0000ff>#include <STRING.H>, ^5 W8 p e" w6 A0 o" N+ _6 Q
#include <STDIO.H></FONT></P>
0 Z+ H+ n) R( d5 a! u<P><FONT color=#0000ff>int main(void) 1 ?8 e& U# _4 w' l8 B
{
8 O! `+ b0 J) c( O6 A+ gchar string[15];
- `3 |, t' I d2 I5 qchar *ptr, c = 'r'; </FONT></P>/ V; o0 d4 S! d7 l
<P><FONT color=#0000ff>strcpy(string, "This is a string");
# F( c) }1 ?4 N% E. q% }8 c4 }ptr = strchr(string, c); , w8 }1 y: x m5 r5 C
if (ptr)
/ V Q" @4 q0 N2 N Mprintf("The character %c is at position: %d\n", c, ptr-string);
) [& I- T4 d8 d: ^* h$ M$ zelse
6 d; H) T2 x, }, g1 R1 h- g4 h+ Aprintf("The character was not found\n"); 2 h% j9 e& N: L" U* Q
return 0; - ^9 n2 A9 A4 e2 I) R! Z
}
/ p; e$ f1 @. h4 ^' K</FONT>
8 Y! U& d7 [ {4 e q c, ?) E
</P>
, @, D1 Y2 r+ T# ]# ]' ~<P><FONT color=#ff0000>函数名: strcmp</FONT> ' g% L2 v2 O5 a) W
功 能: 串比较 0 D- h# L* t M# o
用 法: int strcmp(char *str1, char *str2); # ^# a# [2 X- f& n1 r- b
程序例: </P>
1 u+ ~% e9 @& Y& f<P><FONT color=#0000ff>#include <STRING.H>
[% |1 o* `% g7 x#include <STDIO.H></FONT></P>
- u, p4 G- ~* Y z8 X, [3 p9 V9 t) N<P><FONT color=#0000ff>int main(void)
* u* M6 m( ^$ b/ s# t* `) a{
0 s6 y( h/ K" ^: Cchar *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; 2 a; \$ a3 L! k0 b! V. }8 ~
int ptr; </FONT></P>4 f: x, y2 I; A: x2 K
<P><FONT color=#0000ff>ptr = strcmp(buf2, buf1);
# m& Q( B. q* q( H6 Eif (ptr > 0)
7 d* Y$ z: J- z: g. e3 Oprintf("buffer 2 is greater than buffer 1\n"); 5 k0 }( e9 o' W& m- Z
else
/ U' [9 J5 J+ L Qprintf("buffer 2 is less than buffer 1\n"); </FONT></P>' R V* G) K6 u9 v
<P><FONT color=#0000ff>ptr = strcmp(buf2, buf3); 4 c7 w$ I. I0 `$ J6 F6 W
if (ptr > 0)
( ?4 U8 o, d7 ?/ i; o4 Dprintf("buffer 2 is greater than buffer 3\n");
. a: _0 H) X/ Q! A3 S1 Gelse 2 ]) o- z1 e1 P' W% l4 p2 m, m0 v
printf("buffer 2 is less than buffer 3\n"); </FONT></P>
7 V8 m. t8 Z d<P><FONT color=#0000ff>return 0;
% m! q- b j5 B/ B$ |% i} 0 f7 V$ c, F# x, w5 G: C
7 g R0 [ J3 V3 H8 d- A
3 \% F1 z: W# F+ Z! R2 L</FONT></P>8 Q# M3 C7 _9 ~* h Z. \
<P><FONT color=#ff0000>函数名: strncmpi </FONT>
5 K4 F& o) ^4 F$ z功 能: 将一个串中的一部分与另一个串比较, 不管大小写 ; \1 ?2 h$ t% v; D& Z1 X7 \
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen); 0 c( u+ Q/ @3 q- t
程序例: </P>4 f3 p. Q4 J8 D6 y) D5 Q
<P><FONT color=#0000ff>#include <STRING.H>6 R& H) R V% a8 Z
#include <STDIO.H></FONT></P>) o9 h8 K/ t. T' V- I% W2 C
<P><FONT color=#0000ff>int main(void)
. o; ^* k h3 G7 n' y: D: V{
6 H0 E# p9 F, h' n) jchar *buf1 = "BBB", *buf2 = "bbb"; ' f/ P$ f, O% l2 t0 W/ S
int ptr; </FONT></P>
$ q' L! f' O# f- J3 l<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>
: w5 e2 t3 Y! q8 G' ^<P><FONT color=#0000ff>if (ptr > 0)
* z& \0 i# E, ]. ~printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
8 {% |7 j9 t% Y5 H1 a<P><FONT color=#0000ff>if (ptr < 0) ; a8 L# _: a( g+ v ~* L$ H) b
printf("buffer 2 is less than buffer 1\n"); </FONT></P>, G" o7 o, C/ B9 f: N' A
<P><FONT color=#0000ff>if (ptr == 0)
B/ S* ^6 k& Iprintf("buffer 2 equals buffer 1\n"); </FONT></P>0 j6 X7 h: @! o$ W
<P><FONT color=#0000ff>return 0;
8 q$ K! l+ s0 L |+ C}
; N( q# |3 B; `" v3 p& E& \
j: M- G- s) v h' Y [6 E</FONT>
9 c+ j" _- e" J' }, ]/ u: i8 g8 o! X</P>
; u$ `, N$ m0 K" _2 t+ M U7 F<P><FONT color=#ff0000>函数名: strcpy </FONT>
0 Y# d2 y. Y) G3 x功 能: 串拷贝
0 ~' W6 N \0 N8 V3 N1 U用 法: char *strcpy(char *str1, char *str2); 2 w% Z1 V$ q# N3 q( c
程序例: </P>
/ `3 l& M. Y" z, P% C% E<P><FONT color=#0000ff>#include <STDIO.H>+ V( S! X, R2 U$ T e& a
#include <STRING.H></FONT></P>
3 ?: S; }/ g! c! o<P><FONT color=#0000ff>int main(void)
# u' M8 V, d1 q: t( R{ $ M1 e; K; W% t
char string[10]; * P6 \) E( H3 w2 h' @
char *str1 = "abcdefghi"; </FONT></P>. x: B& i; n6 @8 L$ ^
<P><FONT color=#0000ff>strcpy(string, str1); 4 L2 E5 m, x1 Y* M' e! \0 u
printf("%s\n", string);
" A! t: U7 s' p& treturn 0; u& f) v) W! z0 ~6 J/ i1 [
}
( W1 y$ P; p+ `3 L</FONT>
5 T! V8 f% |6 o, e
3 L9 E* h% G$ z( K1 U, }( E+ s5 L</P>1 u* q5 W8 S$ e- c; b+ G* Q, v x6 C
<P><FONT color=#ff0000>函数名: strcspn </FONT>" I6 M- B @, H, G# ?5 e2 N
功 能: 在串中查找第一个给定字符集内容的段
5 ^" T- A$ G, z' |0 N用 法: int strcspn(char *str1, char *str2);
5 w# d: V; W" W. C: I程序例: </P>) I# s% R! L. q0 W% M# ?% r. X- E5 u
<P><FONT color=#0000ff>#include <STDIO.H>
! c S' p4 Z) D( u+ o#include <STRING.H>
* l9 V: V! ~8 G: v# }#include <ALLOC.H></FONT></P>
" K4 I" ]0 [7 A- U6 c( R<P><FONT color=#0000ff>int main(void)
) A4 l3 r; d& }- E2 B. g{ ! g/ C2 ^% R# ~) m6 X* s
char *string1 = "1234567890"; ( B" j" ~7 l! j4 @$ J L+ g* g) v+ k
char *string2 = "747DC8";
9 N% Q- G4 a2 n9 l Gint length; </FONT></P>
' u O5 V: K3 A9 {<P><FONT color=#0000ff>length = strcspn(string1, string2); % n! c% O" |# e
printf("Character where strings intersect is at position %d\n", length); </FONT></P>+ V5 \3 u' F) Z" b: |
<P><FONT color=#0000ff>return 0; . g, P* y- `/ T3 U; i: ^3 ?- A
}
: ]: _/ Z8 d5 Q0 ^1 O+ G- p: V</FONT>
& Z3 r6 y' Z1 I; C$ }) x" P& D8 I! ?6 m% f* f+ Z
</P>
4 P- q" O: F0 T2 L4 {, M<P><FONT color=#ff0000>函数名: strdup </FONT>
8 c) y6 V+ t: ~' E: m/ p& ^功 能: 将串拷贝到新建的位置处 * w, L& Y$ D% s% C ]* o
用 法: char *strdup(char *str); ( ?4 b+ k9 |0 n9 I
程序例: </P>( t0 |6 e5 z2 m0 E
<P><FONT color=#0000ff>#include <STDIO.H>2 H R7 @* t# e% X9 t/ {. x# o' O& r
#include <STRING.H>0 J' ~; t7 t5 f
#include <ALLOC.H></FONT></P>; {. f2 J* V# o* {' M5 P, E
<P><FONT color=#0000ff>int main(void) ^! t" b& F3 ^8 U4 Y+ s0 O
{
2 C. S) }4 W0 bchar *dup_str, *string = "abcde"; </FONT></P>
0 n0 U0 m7 v' \<P><FONT color=#0000ff>dup_str = strdup(string); 1 H, s. L3 ]1 N9 U, K
printf("%s\n", dup_str); 0 B, {5 z' x- _3 o5 l9 L1 ]
free(dup_str); </FONT></P>
' E+ }4 G2 @0 }5 I+ y+ j<P><FONT color=#0000ff>return 0;
+ J9 f& [& V. C7 K, J7 H0 b' E} ' f- d* P- p8 `, h& v" l; ~
7 I' O/ O$ g" L. G; b0 A
</FONT>% X3 Y) o- C3 M5 j5 T4 ]& L3 a
</P>' {( c3 A0 Z( L, W* y1 ?
<P><FONT color=#ff0000>函数名: stricmp </FONT>
3 Z* f: i; ?0 p! c# t功 能: 以大小写不敏感方式比较两个串
+ V+ C9 ?1 }$ o3 o0 V1 g4 \用 法: int stricmp(char *str1, char *str2); % U" M0 S0 y I/ h8 z
程序例: </P>9 c$ Q) W5 O" Z) Q
<P><FONT color=#0000ff>#include <STRING.H>
# Q) u2 K. J# O& K+ X7 B* r#include <STDIO.H></FONT></P>- e) G( t% q1 G" P7 a" `# q
<P><FONT color=#0000ff>int main(void) ' i( Q7 C% B) r- y8 ^ }- S2 Y+ c. h
{
) G/ B% i) S* W' o* r, N# Kchar *buf1 = "BBB", *buf2 = "bbb";
4 `; d$ e2 ]% w) m4 Aint ptr; </FONT></P>) L% N9 r7 Z" A$ T1 y
<P><FONT color=#0000ff>ptr = stricmp(buf2, buf1); </FONT></P>
: [' U! _. S( {+ K; J. @( B<P><FONT color=#0000ff>if (ptr > 0)
2 z5 l) M a( d+ Q" Iprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>; } b8 z5 G3 x) o
<P><FONT color=#0000ff>if (ptr < 0)
; o- O" A4 R( ?2 @* wprintf("buffer 2 is less than buffer 1\n"); </FONT></P>+ c9 w u2 u* S; ?
<P><FONT color=#0000ff>if (ptr == 0) 6 \) f3 J% e9 [" X
printf("buffer 2 equals buffer 1\n"); </FONT></P> X! k G u$ P
<P><FONT color=#0000ff>return 0; 3 h- s" P# w- u+ V1 @) _9 v
} ) ~/ R6 Z2 T/ A; X, F: f- r6 m
</FONT>8 j3 x' D" z/ _4 s& x* U0 V
</P>6 P0 E1 c' c0 B# c
<P><FONT color=#ff0000>函数名: strerror </FONT>9 b8 n! r- ], r* V
功 能: 返回指向错误信息字符串的指针 ^* k5 s- F+ T4 n5 I9 e
用 法: char *strerror(int errnum);
* v% {# C7 E4 h, b程序例: </P>6 \- `4 J7 s- U* i4 r, w
<P><FONT color=#0000ff>#include <STDIO.H>
1 h4 a9 E E$ a' ]8 v8 p/ S8 T0 G#include <ERRNO.H></FONT></P>
4 X6 Z6 S0 b0 Y( [# ^; S<P><FONT color=#0000ff>int main(void)
! U* v2 G' w9 u- ^5 H" n{
3 @# b- H9 ^: ?: [: q$ C/ Schar *buffer;
6 ]" l7 x4 \) E. f. e1 ]buffer = strerror(errno);
; f! k. P) G7 @1 s; H: Gprintf("Error: %s\n", buffer);
8 ~6 {2 w! F& H! l2 D- a5 \return 0; * S6 H# j. G1 i# f
}
0 r/ ~. o( a& g% b' @4 T) ]
( `- s- ]" z# ?% U/ {) m9 X5 j</FONT>' T+ S) ]; M- B1 u# E: D
</P>
$ p4 P" J$ R5 n- M- {& |2 y+ |4 W<P><FONT color=#ff0000>函数名: strcmpi </FONT>* g1 y' [: `& Z9 b
功 能: 将一个串与另一个比较, 不管大小写
! ]$ o3 `: t+ i; u* _+ x用 法: int strcmpi(char *str1, char *str2); 1 S9 N/ R" w# Q3 A
程序例: </P>8 _0 n( s5 m6 I( L
<P><FONT color=#0000ff>#include <STRING.H>8 L8 Y4 P6 M3 r/ k6 E, y
#include <STDIO.H></FONT></P>% @2 j+ W9 H/ F# s# W& ^% z" n
<P><FONT color=#0000ff>int main(void)
& \4 ?- q+ O& |{
: T1 \. Z) M! {char *buf1 = "BBB", *buf2 = "bbb"; 5 ` E" T: z/ m) P$ _! [1 P/ D
int ptr; </FONT></P>
& d8 a- G* a% Y% }6 j' i<P><FONT color=#0000ff>ptr = strcmpi(buf2, buf1); </FONT></P>$ S2 e6 M' u2 x7 C
<P><FONT color=#0000ff>if (ptr > 0)
3 R2 Q( a' Y4 W& N7 g6 s# Q' |: _+ Aprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>2 U8 M) x9 o, S, x
<P><FONT color=#0000ff>if (ptr < 0)
k: Y# l7 T# s D5 Yprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
' H }( A2 _* q1 b4 F' W8 U<P><FONT color=#0000ff>if (ptr == 0)
- r! i9 M* Q- u5 x0 bprintf("buffer 2 equals buffer 1\n"); </FONT></P>8 q5 T! |" f0 d' T4 P) |
<P><FONT color=#0000ff>return 0;
& E* L0 ?' U# J% W6 b}
+ r" P; W( \- }% L</FONT>
. K; i* P2 d5 R" n( @/ K! Q0 Z$ o4 e d$ i9 |9 ~$ i' k. G7 I9 y* M) F0 b
</P># p; O0 q, u G. q9 T
<P><FONT color=#ff0000>函数名: strncmp </FONT>6 e8 v( e( W8 O' |3 R! |8 w. V
功 能: 串比较
4 A( j# p% T3 I用 法: int strncmp(char *str1, char *str2, int maxlen); + S& p' [5 A) b
程序例: </P>
" L7 |& M6 |% Q<P><FONT color=#0000ff>#include <STRING.H>! U! t. W2 `$ R
#include <STDIO.H></FONT></P>
/ w. B& R5 ?' e; ]' X, c% ]<P><FONT color=#0000ff>int main(void) </FONT></P>
( m' g: K& o3 `( Z* _+ q& L$ Y<P><FONT color=#0000ff>{
7 F4 i% @% Q9 o; l* }7 B) Y1 r- Ochar *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
! S- [+ q2 Y0 Vint ptr; </FONT></P>
; j# R" T( a3 b: R t/ F<P><FONT color=#0000ff>ptr = strncmp(buf2,buf1,3); , |- q! b$ u* j
if (ptr > 0) % ~* Q- ]7 W8 M h( `: o
printf("buffer 2 is greater than buffer 1\n"); : E% _4 }: e' K( ~' A7 G/ b7 m
else
' f) b8 Y& I5 W0 g m4 n) t( o! Vprintf("buffer 2 is less than buffer 1\n"); </FONT></P>
! j7 x; U9 Z3 z' x0 S6 l7 Y<P><FONT color=#0000ff>ptr = strncmp(buf2,buf3,3);
9 R" [! L7 H$ I+ Kif (ptr > 0)
9 S! T4 Q: R9 I# v: H; Sprintf("buffer 2 is greater than buffer 3\n");
; ?3 F' H: w& O% g9 S( oelse + Y. H+ p* q4 V
printf("buffer 2 is less than buffer 3\n"); </FONT></P>
% t+ a+ l9 [$ ?: ~& L% _<P><FONT color=#0000ff>return(0); ( E9 J7 D3 ?' [4 H- w# b
} $ j( U. b0 m! c3 [/ N2 `! w! @
( L. H9 K B3 G# u4 ~" O: i
</FONT></P>
3 G0 r. o. t6 A0 H; S1 ^<P><FONT color=#ff0000>函数名: strncmpi </FONT>
+ P% M- G Z7 y9 @+ y功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
% N$ ?) Q3 k2 K6 E* D. _用 法: int strncmpi(char *str1, char *str2); 0 d" L I( w: i! M; ^
程序例: </P>
/ @# X* A; Z2 Y3 H" G<P><FONT color=#0000ff>#include <STRING.H>: }+ N K( \& o& H/ k
#include <STDIO.H></FONT></P>
: T6 H. r+ b, o8 b( x2 e<P><FONT color=#0000ff>int main(void) 6 [4 ~/ y6 B" S6 D. Y% Y; _" h
{ ! c% K& Q( r t* p
char *buf1 = "BBBccc", *buf2 = "bbbccc";
6 R. ^, N7 }2 L( e* |int ptr; </FONT></P>3 Z$ z+ c7 C, y6 f# G1 ?
<P><FONT color=#0000ff>ptr = strncmpi(buf2,buf1,3); </FONT></P>
* @$ E8 Y% v& Y' W0 G<P><FONT color=#0000ff>if (ptr > 0) $ b& }5 ?2 ]% L1 Y( v# A+ x
printf("buffer 2 is greater than buffer 1\n"); </FONT></P>
) A! b# O# E( n. o<P><FONT color=#0000ff>if (ptr < 0)
2 g/ }/ X. b0 |& {7 M5 g' @printf("buffer 2 is less than buffer 1\n"); </FONT></P>* i5 V# w& O/ F# e7 G, P2 D! g
<P><FONT color=#0000ff>if (ptr == 0)
, F, C) \* c! _: b) K0 \printf("buffer 2 equals buffer 1\n"); </FONT></P>% H9 F3 K3 j! [: s! c- C2 E6 s
<P><FONT color=#0000ff>return 0; </FONT>1 N% e1 p" V8 f$ a
} ; k X+ Z6 C& c2 `4 C( X5 f2 G' P' C
; z' I$ b9 @: {7 t</P>
6 M6 M% }( L$ M5 o8 q9 ^7 D2 l4 j' ^- t<P><FONT color=#ff0000>函数名: strncpy </FONT>" P. W; J- H4 X) L/ t" w3 i, ^
功 能: 串拷贝
, Z* `* o0 S# x7 D1 c用 法: char *strncpy(char *destin, char *source, int maxlen); , N* v( u0 y# g @
程序例: </P>7 `' I- `% @4 P) o8 Y8 C
<P><FONT color=#0000ff>#include <STDIO.H>
* S% s' S* j. v- ]1 q% ]' G& |#include <STRING.H></FONT></P># [7 v/ E0 K- p/ w- _, c
<P><FONT color=#0000ff>int main(void) ! T% A1 h4 p( ~' P4 U. A/ x
{ ! J5 O S/ E" m7 x& ~# U
char string[10];
9 Q& L+ D! \2 G0 [% Nchar *str1 = "abcdefghi"; </FONT></P>
+ e2 c5 t% D$ F2 p<P><FONT color=#0000ff>strncpy(string, str1, 3); 2 w5 g+ e' S6 ?
string[3] = '\0';
. C2 e0 W$ c( W* x3 l( ?printf("%s\n", string);
8 i6 w9 s0 ^7 N/ z, a; nreturn 0; 1 Q- v+ N! r) N, s% Q: ] ]
} 1 B. U7 P' @& b2 ^8 l
</FONT>
2 T* t! x$ l8 c* P' {</P>
+ R" I" U% u! W5 ~<P><FONT color=#ff0000>函数名: strnicmp </FONT>; X# F$ U1 a9 |0 v9 U
功 能: 不注重大小写地比较两个串 ; o$ x4 e2 F) X4 G9 ^' ~" B
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen); 0 V6 |$ T S N3 l
程序例: </P>' G7 G) w, N3 L9 ^( A3 h
<P><FONT color=#0000ff>#include <STRING.H>
, Z; i; d! t$ y1 x" t8 \- p! D#include <STDIO.H></FONT></P>
" W; ^! [( b2 I<P><FONT color=#0000ff>int main(void) # Q2 [3 y6 q# ?
{ , _) k* p# h, U! k- X5 ~6 v6 `. \
char *buf1 = "BBBccc", *buf2 = "bbbccc";
/ K9 v5 T" h. F+ r) c3 D& qint ptr; </FONT></P>: m$ R- i" i6 @3 W+ J# Y
<P><FONT color=#0000ff>ptr = strnicmp(buf2, buf1, 3); </FONT></P>
9 _, `0 c0 P: c# K<P><FONT color=#0000ff>if (ptr > 0)
# s" Z* N$ v% [! Y* T3 L, h. {9 Uprintf("buffer 2 is greater than buffer 1\n"); </FONT></P>3 J: d* o* x Z2 O* y
<P><FONT color=#0000ff>if (ptr < 0)
1 H/ F# `3 k) |, pprintf("buffer 2 is less than buffer 1\n"); </FONT></P>/ E9 N; o, h* W& ]. w: z1 ~4 ?
<P><FONT color=#0000ff>if (ptr == 0)
- Q, B& }9 n/ fprintf("buffer 2 equals buffer 1\n"); </FONT></P>
% f" P! X+ }6 [& q& w& n<P><FONT color=#0000ff>return 0;
; r }2 p% k! w4 w+ D- E} ; U7 N: i: ?- I3 e$ O% p( p
. B: h7 j1 k. o5 s. H5 o3 _
</FONT>
. n7 }2 _ G4 A& y/ v2 N ]</P>
7 ~1 E. ]4 R; \; e8 V<P><FONT color=#ff0000>函数名: strnset </FONT>, @: U- @, P- h* n3 W
功 能: 将一个串中的所有字符都设为指定字符
9 n7 e0 y3 ~8 D" \- C: e0 M* [用 法: char *strnset(char *str, char ch, unsigned n); 9 c7 {+ |$ Y7 Y/ f
程序例: </P>
1 h- C% A" T& {- O<P><FONT color=#0000ff>#include <STDIO.H> H( A/ H8 T5 ]: W$ `# v8 _' _
#include <STRING.H></FONT></P>% Y- d C i3 [
<P><FONT color=#0000ff>int main(void) ( x E4 H" ?$ V% @; G
{
2 s0 ^9 m# _3 P) F! j2 xchar *string = "abcdefghijklmnopqrstuvwxyz"; # ?1 C" `8 x9 ? |% h. \
char letter = 'x'; </FONT></P>
1 S l0 }" r# d O<P><FONT color=#0000ff>printf("string before strnset: %s\n", string); $ {$ W: v9 ~2 t n5 ^9 F
strnset(string, letter, 13); @+ i" d; `4 ]4 v
printf("string after strnset: %s\n", string); </FONT></P>7 B- Z! S( }. @ I; I. j
<P><FONT color=#0000ff>return 0; 7 L, q$ \2 y/ ~) V& Z# k& c0 I# f
} # a0 j% E$ g3 O$ o8 S+ }' ^
</FONT>
5 a: ~/ O3 Z7 R; b1 D8 [</P>- {8 }" T1 j' Q5 r
<P><FONT color=#ff0000>函数名: strpbrk </FONT>' U* f; F' b# z5 L0 q
功 能: 在串中查找给定字符集中的字符
4 B* Z& r. `4 N j5 D2 c用 法: char *strpbrk(char *str1, char *str2);
& p3 Y* F; o, C3 |程序例: </P>
' y! `5 ]9 J$ _ s<P><FONT color=#0000ff>#include <STDIO.H>: y7 G' U4 x8 s+ l
#include <STRING.H></FONT></P>+ O l1 B/ ]+ h2 f* D
<P><FONT color=#0000ff>int main(void)
) J; j0 _# X1 K{ 3 }2 I: G/ D1 s' Y
char *string1 = "abcdefghijklmnopqrstuvwxyz"; 3 D3 @( W* @; G9 H+ x6 q- j
char *string2 = "onm";
. N2 i2 _ s# L2 Uchar *ptr; </FONT></P>
8 X, m0 u3 y! h7 A: R<P><FONT color=#0000ff>ptr = strpbrk(string1, string2); </FONT></P>
) @6 Y }/ o. Z" z<P><FONT color=#0000ff>if (ptr)
( u0 Y8 m- C; Z. n6 {printf("strpbrk found first character: %c\n", *ptr);
+ `! s0 v+ x @8 J# Aelse
{7 g6 A8 V H5 {printf("strpbrk didn't find character in set\n"); </FONT></P>
/ o+ \5 c! U3 x% b2 S: ^<P><FONT color=#0000ff>return 0;
3 ]% k3 d6 r) L) U; R}
: ^5 F; r" E/ p. z/ M) J, R, b. S+ S5 h7 P+ U1 K6 _, N
</FONT>
9 G: I1 r Y5 r</P>
* }5 e# V8 x5 r9 V3 u# Y6 k<P><FONT color=#ff0000>函数名: strrchr </FONT>. B& j8 n* H$ x( e; K
功 能: 在串中查找指定字符的最后一个出现 8 E! h3 I, }' }1 c- j* p) d
用 法: char *strrchr(char *str, char c); - `5 w5 f# m6 v2 a! l9 M% }% E
程序例: </P>+ @4 t% U8 p+ N# {
<P><FONT color=#0000ff>#include <STRING.H>9 L5 `' b+ i- H9 h+ e1 {8 u
#include <STDIO.H></FONT></P>
1 i) x9 p- R( a. k) Q x- s8 P<P><FONT color=#0000ff>int main(void) " M' }6 y6 P" t+ {6 s
{
' |; y" l, k+ }# o1 Schar string[15]; 1 G& v, t# H- B7 z4 X8 Q
char *ptr, c = 'r'; </FONT></P>
# X9 g+ _/ ?- w3 F* V! x" j<P><FONT color=#0000ff>strcpy(string, "This is a string");
: M" e$ q4 J0 H# p, cptr = strrchr(string, c);
$ k/ u! ^9 J! T$ p6 `if (ptr) 9 v" V5 Y" D& L! j* d# P
printf("The character %c is at position: %d\n", c, ptr-string); 0 U6 t& X+ z4 \2 l* U& S/ h
else $ _) t, C7 f* s) R9 y' Q# {. J
printf("The character was not found\n");
1 }2 R, g0 {* Y: Rreturn 0;
" N' f/ B0 w' j; T1 u9 y8 G% N) \} </FONT>
8 T3 z2 |% A ^) |( G6 J/ b
4 ]; C* i7 x6 W& x T/ o
5 ~( F$ ]9 y6 @0 J1 Q</P>: A1 Y' F4 G3 G- A/ H0 b
<P><FONT color=#ff0000>函数名: strrev </FONT>
7 z+ V! e2 [) D4 ~& j, l& t* b& H功 能: 串倒转 $ y9 I- k( E B# M
用 法: char *strrev(char *str); 4 _8 e, F$ p3 H+ s
程序例: </P>
' X. W% G7 @1 a1 _<P><FONT color=#0000ff>#include <STRING.H>
' E& w* `/ O2 B; ]8 t! m9 V#include <STDIO.H></FONT></P>
6 C) e7 C2 ~$ n, Z5 A. ~<P><FONT color=#0000ff>int main(void) ( l6 m1 ~1 Q" C4 p
{ ( J, k) g& Q9 _2 {+ f
char *forward = "string"; </FONT></P>
8 y* Q2 @2 B! l* T<P><FONT color=#0000ff>printf("Before strrev(): %s\n", forward);
" V; s; f6 ?' a& Kstrrev(forward); ; F: a% E! R: |
printf("After strrev(): %s\n", forward); - P- c' A# E6 V( l
return 0; ! U- [. x4 j) k4 E' L
} </FONT>/ A( e& Y6 ~# r% Y; M- Y; o8 p. z
</P>8 W% `3 s7 W( n" D3 \
<P><FONT color=#ff0000>函数名: strset </FONT>
) ]8 g& x9 E; ?. ?. w" d功 能: 将一个串中的所有字符都设为指定字符 0 |3 ?( M2 h L' h1 y
用 法: char *strset(char *str, char c);
# i9 |* q6 W3 ^: g/ s程序例: </P>9 G8 [3 q1 Q* C
<P><FONT color=#0000ff>#include <STDIO.H>
, h+ Y" v0 I; `8 o. X+ S#include <STRING.H></FONT></P>3 T' @) Z+ }% a
<P><FONT color=#0000ff>int main(void) * E [. h5 u3 p% t. I& X2 h
{
# [8 K, C& U- {3 c8 m! T8 fchar string[10] = "123456789"; * y# E- ?; p/ }# X2 W1 \! ]% o
char symbol = 'c'; </FONT></P>
4 G% {# I- I1 S<P><FONT color=#0000ff>printf("Before strset(): %s\n", string); . ]( ]/ h" D& F9 u. Z# T6 e+ A. `
strset(string, symbol);
/ p8 \% ]8 ?. e1 ]2 m5 `4 }1 ?printf("After strset(): %s\n", string); % ~2 q0 c: w. r. D; q- V
return 0; 3 P- k7 P. r' g# g' c$ J* D+ u
}
5 w3 ?8 H+ h0 r4 N
/ B' b) [+ i! y</FONT>) j* w! z$ D; {0 k9 `7 l
</P>
- w8 B% G3 j% v0 V4 `/ P# L6 \<P><FONT color=#ff0000>函数名: strspn </FONT>' Y4 k' {# X$ `, M J3 c9 Z1 i
功 能: 在串中查找指定字符集的子集的第一次出现
5 R9 E, P6 O4 y ~用 法: int strspn(char *str1, char *str2); ! [& ?. K5 ~# D* o/ @; }
程序例: </P>( j+ u3 G3 e9 q ?- b% e) A
<P><FONT color=#0000ff>#include <STDIO.H>
; Z* o* L6 o- h6 y& V3 M#include <STRING.H>
: l' B4 I* W# n0 q#include <ALLOC.H></FONT></P>
3 M- U7 L7 G- k$ r3 P% X( a# ]5 {<P><FONT color=#0000ff>int main(void) 8 F N5 h2 D1 j- O- z! i
{
6 v8 C8 M( N. w0 E7 vchar *string1 = "1234567890";
0 i i8 l: ~, L2 vchar *string2 = "123DC8";
* p8 G6 x8 z1 T- p% y/ kint length; </FONT></P>/ @* T, c$ \, D J1 C9 f9 o4 m: B8 Z
<P><FONT color=#0000ff>length = strspn(string1, string2); - K. W/ h' i# X) ^. R! s
printf("Character where strings differ is at position %d\n", length); 7 w9 c& E8 j, m$ M6 Z/ d
return 0; 2 H: p9 j% S* z* s- q
}
) h P! R' \3 J0 }* p" e</FONT>
5 m2 U, I# z! X& d. h</P>
1 i! ^& k9 o* h, f<P><FONT color=#ff0000>函数名: strstr </FONT>5 x0 B% I7 G) U: [6 M6 G' M4 ?8 d% |
功 能: 在串中查找指定字符串的第一次出现 o) j; g1 ]# E- }8 K
用 法: char *strstr(char *str1, char *str2);
; U! A: q* S( T% J. a5 V' L ]程序例: </P>
6 O. H; M( H7 b1 d- ?<P><FONT color=#0000ff>#include <STDIO.H>9 J9 ^+ ?' i& ^. Y. H
#include <STRING.H></FONT></P>
' Q" e) J2 O: b+ M<P><FONT color=#0000ff>int main(void)
1 W+ @6 P; c. w8 o- S{
) U& S$ z' m" [# ~: Q# d4 schar *str1 = "Borland International", *str2 = "nation", *ptr; </FONT></P>
: ]3 Z/ @1 [8 Q8 G1 z2 W<P><FONT color=#0000ff>ptr = strstr(str1, str2);
6 L, r" s0 z2 L9 _; I7 aprintf("The substring is: %s\n", ptr); - M# ~6 A+ D, B8 }5 m/ f
return 0;
: q2 W; c7 j1 i6 S} </FONT>
r8 R1 q6 K; d) q1 Y5 V, A) ?' W& J0 V$ s& Z" y& H" ~
</P>8 Q& J; l3 Q( w
<P><FONT color=#ff0000>函数名: strtod </FONT>; ]. g3 X* s, k D; O
功 能: 将字符串转换为double型值 8 U6 r5 `3 C4 b0 x9 U! Y7 W
用 法: double strtod(char *str, char **endptr); ' y! n9 M% i6 J ^4 z/ X% K1 {% L1 Q
程序例: </P>
3 w. }# \6 e0 d0 i: i: ^<P><FONT color=#0000ff>#include <STDIO.H>9 S" k! N g" I; Z
#include <STDLIB.H></FONT></P>. g+ k+ f- k0 R! U- Z
<P><FONT color=#0000ff>int main(void) " v( O) K* e+ v8 ]7 I( L
{
% Y8 Q" M8 |3 X% E- schar input[80], *endptr;
* K0 [, \( E" `, F) T. p, |double value; </FONT></P>
3 {2 N% H: T1 b" R+ u<P><FONT color=#0000ff>printf("Enter a floating point number:");
1 L5 W" m4 B6 G* t5 b+ H+ t, Lgets(input);
$ a8 M: p& ]! F8 Uvalue = strtod(input, &endptr);
" ^; x7 `% |" b b/ y8 Vprintf("The string is %s the number is %lf\n", input, value); " D# [0 k7 C ?8 w, B$ K, R: X
return 0;
- K9 r6 E! z& ]% ~& h1 z1 @$ S5 R}
/ A) X' s* \4 L, l: `, W</FONT>
& F$ B/ t/ f% R$ j; s- m9 I& k1 o- z+ M `
</P>; r T, e2 y$ _# L9 W; ]
<P><FONT color=#ff0000>函数名: strtok </FONT>, v+ \ @# L% K4 P( ^8 [' g
功 能: 查找由在第二个串中指定的分界符分隔开的单词 % M! X: j, g g2 g2 x
用 法: char *strtok(char *str1, char *str2);
* ]" ~: L3 E; W) Q* Y, i3 o# u0 A程序例: </P>
: ^$ y4 ^( w3 A<P><FONT color=#0000ff>#include <STRING.H>
& A$ i( d' M Q6 ~8 q" B#include <STDIO.H></FONT></P>" B- Y5 h" R) F6 m/ _
<P><FONT color=#0000ff>int main(void) E4 ^3 {9 k: x N) Z
{
& X) ]9 F: G. {+ k1 ~ nchar input[16] = "abc,d"; # y5 X3 f" ^4 G4 S
char *p; </FONT></P>9 S' S; B' a- k; Z9 j
<P><FONT color=#0000ff>/* strtok places a NULL terminator # J* Z9 I6 C( k) F2 f8 }
in front of the token, if found */ % A; h$ V6 |8 z! h; {: H, x
p = strtok(input, ",");
8 E3 G2 [+ E( Y. I, Kif (p) printf("%s\n", p); </FONT></P>- g+ C. D t5 _+ w
<P><FONT color=#0000ff>/* A second call to strtok using a NULL ; N% z+ |' O) r
as the first parameter returns a pointer
+ k4 S( M1 f: T1 G( K2 b: T _% qto the character following the token */
2 f0 X: m/ C( z2 Q' |( {2 ap = strtok(NULL, ","); ( K+ A( m; ?# E
if (p) printf("%s\n", p);
* B* P5 m8 O% ^( preturn 0;
3 n9 D9 z7 F: S+ P}
/ }+ N5 ^5 K9 J
. A1 x# d, e6 A+ E$ ^, ^( ?
" X5 u# f1 `- L4 a! r</FONT></P>
. G! { b3 Z" n" |( H<P><FONT color=#ff0000>函数名: strtol </FONT>: ?& ~7 p. a0 n/ b: X' B" P$ _
功 能: 将串转换为长整数 4 g( W( b' X% ~0 M
用 法: long strtol(char *str, char **endptr, int base);
, |! [# O2 t% ?' r6 q5 ]$ U1 J- V( n程序例: </P>
. H7 x' P# B/ L& o9 ]2 D<P><FONT color=#0000ff>#include <STDLIB.H>
* {0 W- [; Q" g/ t#include <STDIO.H></FONT></P>
! O! r% p9 I+ W. f, T! O. n) v" _<P><FONT color=#0000ff>int main(void)
( `/ j1 O" Q8 t{
0 [; g. ?( R/ \. l# g7 jchar *string = "87654321", *endptr; * A9 P1 [7 a% @. C2 m
long lnumber; </FONT></P>
7 f& P& `% b$ v<P><FONT color=#0000ff>/* strtol converts string to long integer */ - Y' ?# b* e8 Z2 Z" G
lnumber = strtol(string, &endptr, 10);
8 m- t1 h$ F) C/ T0 kprintf("string = %s long = %ld\n", string, lnumber); </FONT></P>; [/ U$ b+ r3 A6 g) V1 L/ D: O
<P><FONT color=#0000ff>return 0; ' ]$ ~. x K4 [2 C. f
} </FONT>& V! y- Z, U D/ w' Q" l u
</P>7 f5 ]- z3 H+ X! J
<P><FONT color=#ff0000>函数名: strupr </FONT>. [9 T, }, [3 t2 K1 I; i9 g g
功 能: 将串中的小写字母转换为大写字母 + @7 g1 M2 x6 b$ }( t0 d5 q
用 法: char *strupr(char *str); & m: t8 A7 E( f* J/ H' p
程序例: </P>9 ]& H0 R5 Q+ T' Y8 P2 g
<P><FONT color=#0000ff>#include <STDIO.H>6 t* g2 [, X9 K$ x6 v- n5 h8 L
#include <STRING.H></FONT></P>* t( @/ h$ S! e! L& ^5 Q% h$ X3 w
<P><FONT color=#0000ff>int main(void)
+ ~4 B' P0 R) L1 G( f! ?{
U H; Y# H8 G c- ~, ochar *string = "abcdefghijklmnopqrstuvwxyz", *ptr; </FONT></P>* k0 c) i1 M W, W$ X8 F$ H
<P><FONT color=#0000ff>/* converts string to upper case characters */ 5 |4 Y- m& I3 \4 F7 w4 G7 i9 O
ptr = strupr(string); ! V; b+ i* _) D" h7 e" S
printf("%s\n", ptr); + ~' i& r; b. a* t0 t8 r
return 0;
; p7 q9 x/ s" L" Q/ F} * v @ T9 K* Y# Z2 [
, s$ _& V: z& w5 M/ z& J* u J8 G3 I</FONT>
. }" s1 L2 F+ y: h; o- M' O4 T</P>
% K: X7 [* B. [+ K<P><FONT color=#ff0000>函数名: swab </FONT>
1 s: U/ A8 o" c功 能: 交换字节
: \$ L& r, f, a& ]1 D7 B1 b用 法: void swab (char *from, char *to, int nbytes); ' z9 v" z& B4 ?0 ^& W$ z% h8 U
程序例: </P>
/ o: W9 O" T, X# h* O3 ]; \2 g$ e<P><FONT color=#0000ff>#include <STDLIB.H>. c" J. }) u8 Q' C. b9 v
#include <STDIO.H>
' ?& m: ~* ?) c# i8 u: w#include <STRING.H></FONT></P>, D) T9 Y7 z, C; N& @& O2 ]6 u% l
<P><FONT color=#0000ff>char source[15] = "rFna koBlrna d"; 7 Y# r$ f' h% ^. ?: P
char target[15]; </FONT></P>' o* `, C* v, O' A
<P><FONT color=#0000ff>int main(void) 1 p7 @7 o4 I, N2 u/ E" X, j
{ + r7 o9 _8 ~7 Y3 v% o
swab(source, target, strlen(source)); % {. D+ e" S" [& ^
printf("This is target: %s\n", target);
: O) k+ n; l4 W- I* A6 L5 Y+ Qreturn 0;
" e9 N) t& {1 F; a/ U}
5 H! }! ?0 Z+ ^; ^0 I</FONT>
8 M3 A7 {7 U& G& s1 y4 n1 i! C
2 d5 ~* m6 p2 [& V e</P>
9 q/ ~6 d" e. H' J% M8 {<P><FONT color=#ff0000>函数名: system </FONT>' z. ~' E5 T ]# ?. ^
功 能: 发出一个DOS命令
4 V) X2 h- E! o! l用 法: int system(char *command);
, f4 p; ?) w5 y/ r" @5 ~9 t程序例: </P>
( o/ u" ~! y0 |<P><FONT color=#0000ff>#include <STDLIB.H>
7 S' A7 ]1 x3 Q7 g. K5 e9 J, o! K4 v9 A#include <STDIO.H></FONT></P>
, F' n' j5 D2 ?& A8 i5 W' N<P><FONT color=#0000ff>int main(void) # y- O( n$ O( s x. n4 r
{ 1 K( l$ \# h2 t, M& E/ m
printf("About to spawn command.com and run a DOS command\n"); , L: V1 a' L0 o1 L, a* C
system("dir");
1 x3 f8 |5 G( _) i/ t) wreturn 0;
! c8 |" b* k( ?4 b$ q8 }" d} </FONT></P> |
zan
|