- 在线时间
- 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>函数名: qsort </FONT>
# p- G1 g; C! X7 U Y6 |- ~% X功 能: 使用快速排序例程进行排序 C! x) q: P3 Y8 u
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); # V; ?' b* B- m& ^
程序例: </P>
0 Q. _5 W/ t5 \2 O7 `< ><FONT color=#0000ff>#include <STDIO.H>6 w# v9 s+ u- b
#include <STDLIB.H>
* @$ j6 o4 `" h" h9 u#include <STRING.H></FONT></P>6 D" E; e, V) y; J% [- y& x' _, i
< ><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
/ u. I1 i. e# k# F$ ~4 G< ><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; 1 Q) K. D* V+ b/ }" e' I
</FONT></P>2 H5 f9 Z$ @3 S
< ><FONT color=#0000ff>int main(void) 6 ~% @% N6 c% X& N
{
& r5 j! h+ ]& N6 Bint x; </FONT></P>2 [! L9 m9 q, d& w) e7 _( [, w
< ><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
$ s3 [$ d E) H3 Ffor (x = 0; x < 5; x++)
9 l) y6 T4 n9 p$ Xprintf("%s\n", list[x]); " Z: z+ @9 @& i# g W# _5 h9 b
return 0;
3 s& s% X5 U& D& v$ w+ b} </FONT></P>
) W. Q9 `: z3 ~, K! x< ><FONT color=#0000ff>int sort_function( const void *a, const void *b) * g5 z& g% i4 I# H
{ 4 Q5 J! B3 e# \3 e( V7 f$ N
return( strcmp(a,b) ); + {( q: c- U b& r3 v+ s
} </FONT>
v: M/ I9 \' {6 Z</P>% [% a* U8 v: j* F7 x7 v# W O+ O
8 J6 h' a) ^( o N, V
! F5 V1 l; f0 a8 i5 i6 o
<p>
9 C2 N% u E4 x d" y< ><FONT color=#ff0000>函数名: qsort </FONT>0 G6 f' ]4 V' v' h! x
功 能: 使用快速排序例程进行排序 ! [/ j9 t1 s! t3 G
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)()); ( @/ C7 s0 J$ r& {1 V5 f' @& F
程序例: </P>
: y! ^2 I, h* I! D J( J% H- i< ><FONT color=#0000ff>#include <STDIO.H> s9 {. T$ F* L$ ~/ r% |
#include <STDLIB.H>
2 p# l- L" F' `; A3 c+ Y0 Q, ~#include <STRING.H></FONT></P>
. l) C' O6 o: @7 ]< ><FONT color=#0000ff>int sort_function( const void *a, const void *b); </FONT></P>
) K6 U i3 i( t/ c) @< ><FONT color=#0000ff>char list[5][4] = { "cat", "car", "cab", "cap", "can" }; 6 m! @( G& T8 f" |
</FONT></P>- ^; L! s8 }0 h- Q* i# E- [
< ><FONT color=#0000ff>int main(void) 0 D% ]# J; g2 ]$ C( |
{ ' z8 D- L) P0 i
int x; </FONT></P>
9 y; Y, f1 E, |) q< ><FONT color=#0000ff>qsort((void *)list, 5, sizeof(list[0]), sort_function);
& R1 G/ v$ {& [) ^for (x = 0; x < 5; x++)
, \8 {9 s6 m( j. F1 _3 \( Oprintf("%s\n", list[x]); - B! s6 v1 x9 P7 q6 K0 Z
return 0;
3 o7 F6 O7 i9 m0 W5 Y; W8 ]/ H$ f} </FONT></P>
X8 q7 ?" j) M5 z< ><FONT color=#0000ff>int sort_function( const void *a, const void *b)
7 ^0 I+ B% H' I4 n Y{
# g2 t6 m1 r! K+ freturn( strcmp(a,b) );
( d3 U! L" R' m i8 p/ I' {, }}</FONT></P>
/ w$ v) w4 P3 b# L. j< ><FONT color=#0000ff>
! `" `! Y2 i" [7 U2 B</FONT>, h2 m! }$ }: M
</P>
; x5 T9 s% j/ u+ y< ><FONT color=#ff0000>函数名: raise </FONT>0 ^' F/ c( x+ i' T6 s
功 能: 向正在执行的程序发送一个信号 ' c- H4 y9 F" _6 _2 |8 G) q/ L
用 法: int raise(int sig);
) ^* s5 R) w% I/ {8 H6 n9 r程序例: </P>; }3 Z* c& z1 q
< ><FONT color=#0000ff>#include <SIGNAL.H></FONT></P>
6 z, a! R0 G8 I! F/ ]< ><FONT color=#0000ff>int main(void) ( M' j5 k1 M& @
{ ; }+ B( c \; X# T3 Y5 h# N- o
int a, b; </FONT></P>
7 j i' ?3 ]7 T8 u1 U< ><FONT color=#0000ff>a = 10; 9 ?8 \& k, ?2 q" Z$ }3 [5 ^
b = 0;
% z7 L) r- X. c6 U( Q7 B; ?if (b == 0) 9 ]4 D2 \- K. P3 _6 Q, P8 F
/* preempt divide by zero error */
; K, z# C" e3 N; e9 N4 Araise(SIGFPE);
$ H' S% i$ j: w) y* ja = a / b;
; n7 p: F) d6 C, nreturn 0; 0 R& ^3 z$ r2 V% ] E' _0 V0 E6 D* F
}
( h V) n# N* T9 o+ _2 A4 q( J</FONT>
' g; G4 i y/ M6 T- ]</P>
( X1 _0 v6 U+ O& e< ><FONT color=#ff0000>函数名: rand </FONT>
% W/ L1 {* [' B/ W功 能: 随机数发生器 6 ?( O/ q5 k' C9 L7 h
用 法: void rand(void);
6 ~& y. ]0 h1 M& j2 ]* `程序例: </P>$ ^) Z' X- r# v1 t& S2 d' n
< ><FONT color=#0000ff>#include <STDLIB.H>8 V8 I% v$ i, M) \& V# p$ K
#include <STDIO.H></FONT></P>
/ Q3 ?5 U/ Q7 o: y< ><FONT color=#0000ff>int main(void)
' t' D5 ~* j- t0 F{ - p9 a4 g; I5 x5 a. Y2 r
int i; </FONT></P>
; W9 E. Z3 [! _( l$ ^: e8 H# z6 a< ><FONT color=#0000ff>printf("Ten random numbers from 0 to 99\n\n"); + o! `) ]2 s/ u: `: ]4 |
for(i=0; i<10; i++)
. _; v! N) ^; D F- c wprintf("%d\n", rand() % 100);
/ p9 o: G- A# z' Ireturn 0;
8 S+ n' j( u, k! d} 8 @9 o2 G, ]# p; M' a
* n1 R7 ? w( X. W0 K! j</FONT></P>* ^4 l7 \9 b5 V
< ><FONT color=#ff0000>函数名: randbrd </FONT>
2 T8 J) n( S; P1 Q7 x功 能: 随机块读 / |. l# [: `# K: T; f
用 法: int randbrd(struct fcb *fcbptr, int reccnt);
4 k! I+ }" ~7 B: M- O8 t. Z5 o程序例: </P>
5 K# H3 {" i2 X2 Q< ><FONT color=#0000ff>#include < ROCESS.H>* c) j% r5 n8 H* v5 {; L( D' c( G6 u+ u
#include <STRING.H>
" N; K E' O/ H; z: r4 d#include <STDIO.H>2 ?* L, {7 H- S& k
#include <DOS.H></FONT></P>
$ R/ z9 I# C* c6 f9 Y9 i% F< ><FONT color=#0000ff>int main(void) - ^. f; s, c" D( e8 }4 T2 O% H
{
; {& [+ ~ E, p* F$ V5 ~0 gchar far *save_dta; " \6 i/ @4 G8 J+ F& d
char line[80], buffer[256]; 0 g/ W e. d4 q" a1 e
struct fcb blk; , U h# m& M @
int i, result; </FONT></P>
: }- x" k! q) R< ><FONT color=#0000ff>/* get user input file name for dta */ - q% y( {6 G6 {! f! B3 t: w4 B' C
printf("Enter drive and file name (no path - i.e. a:file.dat)\n");
/ s0 Z8 p9 @, t) q$ w; q P0 x2 rgets(line); </FONT></P>1 B: H0 n. ^# @. g
< ><FONT color=#0000ff>/* put file name in fcb */ ' y, g) S; _. C# V
if (!parsfnm(line, &blk, 1)) - p7 F t5 h0 ^! J0 Q* u
{
# U# m, w+ g5 P7 R- G% j9 zprintf("Error in call to parsfnm\n");
4 v1 e* v [ }$ Nexit(1);
% h0 T+ G' V# A* V8 Q}
8 x3 H+ d) p9 r- B* I+ {: Xprintf("Drive #%d File: %s\n\n", blk.fcb_drive, blk.fcb_name); </FONT></P>: @0 e5 }8 D7 `" y( C
< ><FONT color=#0000ff>/* open file with DOS FCB open file */
% ~) b2 r. Z J* W4 b6 D1 ibdosptr(0x0F, &blk, 0); </FONT></P>7 i6 }% ]5 h- e {9 n/ v6 j
<P><FONT color=#0000ff>/* save old dta, and set new one */
; O. ^6 a/ Q8 S* ?6 \$ s0 `7 g- Fsave_dta = getdta();
: T* M' Q$ R6 b/ T) H3 v! a) Asetdta(buffer); </FONT></P>' D$ B6 c4 Y9 Q
<P><FONT color=#0000ff>/* set up info for the new dta */
- W9 c/ M- y4 rblk.fcb_recsize = 128; # M6 w# W5 W8 w
blk.fcb_random = 0L;
5 s4 f7 b$ v. |6 a+ Aresult = randbrd(&blk, 1); </FONT></P>& l2 G8 W* }/ d8 Q C
<P><FONT color=#0000ff>/* check results from randbrd */ 9 `6 \2 K8 V: o
if (!result)
0 Y, h0 V" u% aprintf("Read OK\n\n"); 8 B# O; W; J. L/ [+ }+ I
else
- Q f9 ~0 L k{ 0 }$ n4 L4 X d- _% f. N
perror("Error during read");
9 g# {- c# T$ ?( O8 j' z( }exit(1); * @# k9 x' a- V# f
} </FONT></P>1 m2 C" g, n6 d3 T" p. ^
<P><FONT color=#0000ff>/* read in data from the new dta */ * u" y* t( e6 E; Z4 Y1 j3 o
printf("The first 128 characters are:\n"); 3 Y$ ]" L* a/ u/ m: j
for (i=0; i<128; i++) & s. [6 o: R0 g% U9 k$ t
putchar(buffer); </FONT></P>
: }( g1 R& l5 k! Y A! w<P><FONT color=#0000ff>/* restore previous dta */
3 j2 i1 B9 D/ Y6 W0 Nsetdta(save_dta); </FONT></P>" D+ \# e- g( m
<P><FONT color=#0000ff>return 0;
L/ E! N% J. d' w4 H! x2 P} </FONT>2 ~- F4 [9 A" C5 a0 x. P x
</P>+ a! d% E: F+ y N! {. c3 ]
<P><FONT color=#ff0000>函数名: randbwr </FONT>+ J& \; C5 }& n) R; D' M
功 能: 随机块写 - g3 C& A. G8 ]4 A
用 法: int randbwr(struct fcp *fcbptr, int reccnt); & X1 E6 J/ S; Q- V1 e& C
程序例: </P>
+ y$ V/ ?" _, a- o" ]/ G+ h/ G<P><FONT color=#0000ff>#include <PROCESS.H>
* I9 s3 D- T: }4 A. l. w) P#include <STRING.H>
/ F( K1 y9 O' f) |0 E, J#include <STDIO.H>
/ e/ L; N T% b#include <DOS.H></FONT></P>
9 F. C1 [3 F" M6 X) G" I+ l2 i. g: J<P><FONT color=#0000ff>int main(void) " Z5 k; ?$ S' ], c# `: N2 u- E
{ ) l3 C9 R: d7 q+ A
char far *save_dta; : Z/ `. [- n9 p7 s
char line[80];
. b" E, }& k/ g/ n7 cchar buffer[256] = "RANDBWR test!";
- @/ ~# F$ b, z: n5 O" mstruct fcb blk;
0 G2 D+ F5 s# ^+ P8 uint result; </FONT></P>
" \+ ~, E& _$ I+ Q, b<P><FONT color=#0000ff>/* get new file name from user */
" ]0 }5 U% y& {& J: P+ ?# Z( Kprintf("Enter a file name to create (no path - ie. a:file.dat\n"); 1 Z7 P G7 Q0 p$ h
gets(line); </FONT></P>5 O! K! z# f. M z1 {
<P><FONT color=#0000ff>/* parse the new file name to the dta */
6 G( ^5 A- l, ?0 s( `8 Hparsfnm(line,&blk,1); ; l8 u5 o7 d* r9 \! w
printf("Drive #%d File: %s\n", blk.fcb_drive, blk.fcb_name); </FONT></P>) ]+ ?* i3 i' {
<P><FONT color=#0000ff>/* request DOS services to create file */
# R8 S$ D4 {% k8 J4 R% uif (bdosptr(0x16, &blk, 0) == -1) % D) I1 l$ u" _! u( Z$ R
{
$ y0 @3 c/ e4 Z5 m9 @perror("Error creating file");
% j! c& c$ T. E- x% e( texit(1); ! k& n0 |4 I' j' j1 {' Y# R3 Q! [1 `
} </FONT></P>
/ c- ~- X0 p0 N% i* a( {' H* o' V<P><FONT color=#0000ff>/* save old dta and set new dta */
! D7 b5 b! c# Isave_dta = getdta(); - M1 p4 k# h) n' p' Y* o/ u7 A
setdta(buffer); </FONT></P>; V: g- W6 |: l7 b; k. ]+ m" N
<P><FONT color=#0000ff>/* write new records */
5 b# y# O' T) D/ Qblk.fcb_recsize = 256; + ?8 p6 H: {& U% ^! V
blk.fcb_random = 0L;
3 v, U( B* K# x0 |: S+ Vresult = randbwr(&blk, 1); </FONT></P>' C; D- w6 F! C$ {# i* C5 N' C
<P><FONT color=#0000ff>if (!result) ( Z9 I& i6 C' l+ K! u% U1 R
printf("Write OK\n"); h3 L6 l T7 g6 }- K
else & H6 q5 v, p# W2 o
{ 3 y4 w; ~- d; p/ H, Q! ]2 `7 }
perror("Disk error"); # G8 n# j' `+ n% A
exit(1);
: C- v, |; ?9 i$ r; n} </FONT></P>
. ~4 F7 I; c! e! K* T/ J) l) `& Q<P><FONT color=#0000ff>/* request DOS services to close the file */ # w/ k1 q( {4 h& v
if (bdosptr(0x10, &blk, 0) == -1)
) K: U# p- f; H{
% ?: z1 |6 N; B: Gperror("Error closing file"); 8 r- c, h" Z3 z8 }$ W7 R& L( @
exit(1); ( N, B: r. P: {0 d2 `8 A
} </FONT></P>- d3 M+ J9 T' F4 J# `
<P><FONT color=#0000ff>/* reset the old dta */ . m# R) \# R0 y
setdta(save_dta); </FONT></P>! \* m6 v D7 o, g: e
<P><FONT color=#0000ff>return 0; ! F4 ^0 \) B( A% j, H F. n9 g
} % P" Q0 k# q- m: e' O- L
</FONT>) f1 K9 J0 U$ ?/ p
</P>
5 ~; t) i4 x D0 [& [+ i, e7 Y<P><FONT color=#ff0000>函数名: random </FONT>2 k1 y5 Y; d) |1 h$ V% ^! u' k) u
功 能: 随机数发生器
" [, S8 r' j) B用 法: int random(int num); . H( B( P& \+ L6 Q2 x
程序例: </P>
/ _8 r2 P7 e1 v. F" r<P><FONT color=#0000ff>#include <STDLIB.H>
5 O8 l1 \, f9 N/ W v#include <STDIO.H>
2 I# H6 H* p; e# D+ u#include <TIME.H></FONT></P>
1 ]1 h6 l- @2 L0 o: U6 w<P><FONT color=#0000ff>/* prints a random number in the range 0 to 99 */
0 n, y# H$ b8 e$ _, t! Gint main(void) % \) k+ ?$ U" o1 g9 X
{ 1 x( f9 t& e' m# X0 V
randomize(); 1 |6 U4 Z8 S9 J( o- ?8 i
printf("Random number in the 0-99 range: %d\n", random (100));
$ O J# ?! E: P/ V3 w6 Jreturn 0;
( ]% R! n$ S! A( N+ L+ t} 5 p, w+ S7 Z& R. F$ ?* i' p
</FONT>
/ L" A. M# [* i! Y" V! z% U</P>
6 s, {: v9 q0 K3 f<P><FONT color=#ff0000>函数名: randomize </FONT>
3 f9 Y' G8 |9 H$ B) y功 能: 初始化随机数发生器 1 m- e1 ~5 b/ b; R! U+ J5 H0 g
用 法: void randomize(void);
5 z- [2 q8 Y d: ^程序例: </P>
# M8 s9 G( Y9 a7 K# G6 V) J$ g% m, U<P><FONT color=#0000ff>#include <STDLIB.H>+ U O! B3 w3 C6 ^% \
#include <STDIO.H>0 L3 X3 r. N2 T- x& f
#include <TIME.H></FONT></P>
6 b3 F, ]+ G( c<P><FONT color=#0000ff>int main(void) 0 h, B# _% \8 ?) \" L+ t4 n
{ : ?' _/ [5 A* `- k2 Y
int i; </FONT></P>
/ i5 Z0 {0 T, t! ^1 N! E' V4 j: c<P><FONT color=#0000ff>randomize();
! d# G$ n) F7 [' M. j/ G: \printf("Ten random numbers from 0 to 99\n\n"); ! j+ m) }( G4 I+ |4 ^9 @
for(i=0; i<10; i++) 0 s: H z f1 ~
printf("%d\n", rand() % 100); 4 n& D9 @8 C& S* }9 @' Q0 E
return 0; ! M8 v! {6 T9 |9 o. M
} * i8 k* }; Y! ?" z( H: {$ d
</FONT>
3 K% Z; @% ^4 A$ L4 N1 L8 A& o U</P>" `' X9 f) o+ U/ _
<P><FONT color=#ff0000>函数名: read </FONT>% ]( G& K! o; e. i ^, n i1 W
功 能: 从文件中读
1 ~& u: b: ~9 O; K& @7 Q: V用 法: int read(int handle, void *buf, int nbyte); - t% d% ]# D' F$ ?/ q8 `! A
程序例: </P>
: R6 X1 R! k* X4 B+ s m* K w, @( i<P><FONT color=#0000ff>#include <STDIO.H>2 J; d0 S7 s) X
#include <IO.H>5 P: l# s# M& x5 }- s
#include <ALLOC.H>* e' c& l" T. S6 `$ m3 T
#include <FCNTL.H>0 L7 i; x9 `# p0 b8 k# \5 W6 S
#include <PROCESS.H>
# o+ W5 [; A# E! k: N#include <SYS\STAT.H></FONT></P>
O5 e! W6 T$ ~9 ]# r<P><FONT color=#0000ff>int main(void) ( y4 w$ G. x! m& n% |" a0 j) i
{
8 [2 M6 o6 O' D* x1 j5 B9 m, Zvoid *buf;
# W. C& l) l3 g! n9 t! Zint handle, bytes; </FONT></P>' G; E+ U. b/ A2 A0 x9 P
<P><FONT color=#0000ff>buf = malloc(10); </FONT></P>
3 H8 H' _8 S! l: Q, q' Y<P><FONT color=#0000ff>/* + l( P! m: C- [: N& }
Looks for a file in the current directory named TEST.$$$ and attempts 4 s2 L5 O$ Y, D! C2 o/ m
to read 10 bytes from it. To use this example you should create the
+ J6 i! A1 G$ c( K9 W$ r/ E( nfile TEST.$$$
, r. O; r! l% i*/
. G _" S& e; @2 J7 yif ((handle =
$ r G. F8 ~7 G' u' m) p/ X# Yopen("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
6 E% u1 d5 {; o) D# v; }* J; }{
0 }" l, s/ e7 R6 f0 }& Hprintf("Error Opening File\n");
3 e% u6 U/ h( E" ~ ]+ A3 ]+ cexit(1); $ B1 k# {7 u. n6 [
} </FONT></P>
( Y/ x9 B% e* Q7 h<P><FONT color=#0000ff>if ((bytes = read(handle, buf, 10)) == -1) { + A5 S" ] W8 B
printf("Read Failed.\n"); : ~: e1 ^) H) R2 A
exit(1);
7 b! J% @/ A& E6 C} ; Q+ l) e# V1 O4 L& e. l) o
else {
& v& A8 x# R6 K! y8 {' }0 K* m! [printf("Read: %d bytes read.\n", bytes);
}3 v/ G. d+ T6 A/ Z/ u} 7 f2 ?: u+ P2 L; ]
return 0; . x7 y' B* F0 g1 a" |2 t
} </FONT>
0 d/ ?* s G) Z! d( B1 @- V2 f. s( i t j
</P>
0 {8 O: J( q m2 {, l<P><FONT color=#ff0000>函数名: realloc </FONT>. t! J- y/ z) j4 L* r) j5 i
功 能: 重新分配主存 9 d" T5 B* B& f; G0 R
用 法: void *realloc(void *ptr, unsigned newsize);
9 x2 v, Y w: s: e1 g程序例: </P>7 c5 [/ K4 v* ?9 h3 H
<P><FONT color=#0000ff>#include <STDIO.H>6 Q/ y4 o! h: I O) i3 g
#include <ALLOC.H>8 _# s9 N( }( N* L
#include <STRING.H></FONT></P>
1 a) t' |2 e4 n<P><FONT color=#0000ff>int main(void) 5 \1 b$ X" ^$ Z: d; ]% _
{ 4 w1 E+ M$ {! j( |
char *str; </FONT></P>
- v ~! ~/ X3 i* n<P><FONT color=#0000ff>/* allocate memory for string */
3 c$ ?% Y- k# ystr = malloc(10); </FONT></P>
& h/ P8 W! [( m7 Y- d<P><FONT color=#0000ff>/* copy "Hello" into string */ 7 n! E5 a; Y) I7 |) ?
strcpy(str, "Hello"); </FONT></P>1 P, _ T$ @0 w# q& ?. O
<P><FONT color=#0000ff>printf("String is %s\n Address is %p\n", str, str); ; G$ B q0 ~- w$ i& X2 J% S4 T
str = realloc(str, 20);
2 X+ X2 S. S2 `7 S; q8 iprintf("String is %s\n New address is %p\n", str, str); </FONT></P>8 i/ ]4 o2 s0 s- x$ P* }+ J& J
<P><FONT color=#0000ff>/* free memory */
. m; j; o4 _8 `& z v' Lfree(str); </FONT></P>2 ~( d+ {; R* c# |
<P><FONT color=#0000ff>return 0; % e5 K+ [7 A" M( }0 ]7 ^, [
} ( ]8 e# B2 A8 T* r) R
</FONT>
0 u6 k2 i, H8 l</P>6 o$ g) `: ?2 ~; i$ M' e
<P><FONT color=#ff0000>函数名: rectangle </FONT>
2 i$ m! K& T$ I) W, n3 k功 能: 画一个矩形
1 o0 k" [ z2 s" r' ?+ U1 B0 b用 法: void far rectangle(int left, int top, int right, int bottom);
5 f! M5 t4 C. A程序例: </P>
0 Y# K2 K" t& O- n u0 N) ]<P><FONT color=#0000ff>#include <GRAPHICS.H>/ A- }$ t# \0 V, X& }7 _( X& L
#include <STDLIB.H>1 {! L4 x4 _# E+ Q) I6 p, B
#include <STDIO.H>
/ r% Z9 ~' ^( ~$ M#include <CONIO.H></FONT></P>- D: c3 w, z+ {- J- A( g, e/ M
<P><FONT color=#0000ff>int main(void) 4 @3 v! A- a# P1 P) k* Y/ ~; K0 |
{
+ x* j) g' n) j. N- A/* request auto detection */ ' y7 n7 N0 Y$ g) M/ q, J$ J' W {
int gdriver = DETECT, gmode, errorcode; # S0 q# s' Y* F7 z, F8 o
int left, top, right, bottom; </FONT></P>) f) p C- b( }* ~/ u
<P><FONT color=#0000ff>/* initialize graphics and local variables */
: r7 e5 P$ f* O% q% Z8 O9 Z' finitgraph(&gdriver, &gmode, ""); </FONT></P>
9 g' T$ X' d4 e# v5 g' y6 M* u<P><FONT color=#0000ff>/* read result of initialization */ 4 F8 z( n; F, d4 Z0 {" A7 k
errorcode = graphresult();
6 p5 r) z: A- J+ T; P; |5 E0 Nif (errorcode != grOk) /* an error occurred */
3 M |, c" D# Z8 P{ 0 ?' @6 Q6 \/ j8 H. B2 C- l
printf("Graphics error: %s\n", grapherrormsg(errorcode));
3 U9 {4 {# @ P' v8 c4 O1 Aprintf("Press any key to halt:"); 0 F5 s" r5 v% u! C/ Q/ ?6 X
getch();
5 O; J' ]# v) z5 ^exit(1); /* terminate with an error code */
7 K, v+ z3 O7 d5 U} </FONT></P>/ a. |3 z. O T0 D4 S
<P><FONT color=#0000ff>left = getmaxx() / 2 - 50;
# E& s( F) M' b( stop = getmaxy() / 2 - 50; , o! @) v* g \3 B$ Q
right = getmaxx() / 2 + 50;
% B' S" ~. w9 O6 Hbottom = getmaxy() / 2 + 50; </FONT></P>$ j2 P. [$ K& r( i) Q' o, C$ k
<P><FONT color=#0000ff>/* draw a rectangle */
[ |6 S1 `' ^* \! o. F; Lrectangle(left,top,right,bottom); </FONT></P>' Z5 p7 [8 f* a9 y
<P><FONT color=#0000ff>/* clean up */
0 Q& e6 N5 j* m& @9 Z' f% |getch(); 1 Z5 t" q& `5 ?! p; W- g
closegraph();
4 x4 b5 h2 U" u+ Creturn 0;
: L! E* n* h5 [3 I4 h7 y}
: q# y3 {9 t% c3 d/ J/ t1 ^</FONT>% C, S! S/ W/ q/ z+ E: _
</P>. ^) }" X; d- I1 K( k0 S% }5 y" [; |
<P><FONT color=#ff0000>函数名: registerbgidriver </FONT>
2 ~( ^: Q2 n; J* E功 能: 登录已连接进来的图形驱动程序代码 ! U" [ P$ s6 N; U1 Z( N( G$ h" O
用 法: int registerbgidriver(void(*driver)(void)); . J$ a( Z9 ~8 f
程序例: </P>* G; q3 t! _6 j/ a) s
<P><FONT color=#0000ff>#include <GRAPHICS.H>: q* R! u$ F/ E
#include <STDLIB.H>
6 {4 U4 O: w2 e7 f$ a8 i#include <STDIO.H>
* \9 L0 y1 e, C2 @9 B#include <CONIO.H></FONT></P>
7 `9 s, O5 Q7 I1 V2 \2 r$ Q<P><FONT color=#0000ff>int main(void) V: ?/ O" F. |) U! |% t
{
, A+ |; q }2 Z5 P; a P/* request auto detection */ y) A9 b. W6 |+ I. E: A1 o, i
int gdriver = DETECT, gmode, errorcode; </FONT></P>' P' V6 y7 N. k+ F; ~/ m
<P><FONT color=#0000ff>/* register a driver that was added into graphics.lib */ ) O3 A: g% F" k) H, d
errorcode = registerbgidriver(EGAVGA_driver); </FONT></P>% s+ T! N" b: u4 k) s: {% F
<P><FONT color=#0000ff>/* report any registration errors */
+ v9 d) a. K7 o% p% r* ]9 kif (errorcode < 0) % }6 A8 d+ r/ t) `' C$ t8 V* }
{ 5 c- R6 J9 p+ _% K ? |) W+ e
printf("Graphics error: %s\n", grapherrormsg(errorcode)); - j2 S2 }! O( M. b/ E
printf("Press any key to halt:");
3 ]: s5 _) W3 X* ~- fgetch();
* b- a6 n* }0 Z8 iexit(1); /* terminate with an error code */ / ~/ o" s/ H. |) B. V& [$ E- [6 ^/ i c
} </FONT></P>
% D6 D' G7 D* z U! K<P><FONT color=#0000ff>/* initialize graphics and local variables */
. o. R. A, m, U( X% B, r; W1 p: Einitgraph(&gdriver, &gmode, ""); </FONT></P>
7 v7 p. s4 R* n<P><FONT color=#0000ff>/* read result of initialization */
. @9 G" y6 {0 n( Q# J* Berrorcode = graphresult();
( [ J* X% ?( V" t$ Z' e% `if (errorcode != grOk) /* an error occurred */
5 b- k2 V0 P, v6 R [, D _8 D{ , S# A5 l% P4 p7 x9 `
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 0 Y; o }# h+ q9 {8 Q/ f6 k0 ]
printf("Press any key to halt:"); 6 N- [4 t) W, R1 @5 ]9 {0 `
getch();
6 q, e' d7 E; J5 ~9 i% x* Nexit(1); /* terminate with an error code */
' c, B+ d6 j$ }9 J4 s% d- u} </FONT></P>/ k1 I9 T8 I2 D& a7 j! [& h
<P><FONT color=#0000ff>/* draw a line */ / k, E4 k, O( h/ w2 g2 E
line(0, 0, getmaxx(), getmaxy()); </FONT></P>
8 ?& ]6 M0 g! `5 P! ?, U' ^<P><FONT color=#0000ff>/* clean up */
$ c$ x* [) H5 L" j1 R5 [# d( m8 A( lgetch();
E0 A3 Y1 V0 Z. J1 m1 S6 fclosegraph(); , J& S9 v0 |& H
return 0;
; `+ }9 i9 v y) e} * F4 z( ]0 q; \
</FONT>
6 i4 S9 o" g! g( q" Q) f: j</P>- f- i! Q% }! e6 H2 k n% q
<P><FONT color=#ff0000>函数名: remove </FONT>- J2 W9 u: K9 I' j! |/ m
功 能: 删除一个文件 ! [: Z5 Z( j d+ m
用 法: int remove(char *filename);
4 T a; ^) Y& d程序例: </P>( T. C2 n+ j9 D) }5 Q
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>. P6 ~* z9 O& o; T
<P><FONT color=#0000ff>int main(void)
4 l; i4 @2 o! [/ `! I9 V$ i. K{ ' z: x; i i) A$ v/ V3 a
char file[80]; </FONT></P>
$ h# ~! p, ^3 ?* ` d<P><FONT color=#0000ff>/* prompt for file name to delete */ 9 d+ i3 Z7 ^* e$ j
printf("File to delete: ");
: ]6 p- f- Y W4 {: W' s4 @6 m. J* Agets(file); </FONT></P>
: X: n, m; m9 L, ?* E: ]& K<P><FONT color=#0000ff>/* delete the file */
( d: i& b. |7 Hif (remove(file) == 0)
, v& j$ B, i+ J3 xprintf("Removed %s.\n",file); 5 l- w5 U1 t: Q! N' R5 T
else 1 a& P/ l' Q d
perror("remove"); </FONT></P>
. \/ O! X: R* l z<P><FONT color=#0000ff>return 0; & I4 X: ^; N8 \+ N6 ~: Q. Q: n' e
} " f2 x) _- k* s) G0 B$ \ I# h* n
6 f5 l& g1 u3 O' [% N9 y, M</FONT></P>7 a4 s( R1 [) ^: |: e% |5 ?
<P><FONT color=#ff0000>函数名: rename </FONT>5 y8 b6 ~ A- f/ H% U$ u
功 能: 重命名文件 / }6 ?; i$ R3 b: ]" I8 i
用 法: int rename(char *oldname, char *newname);
5 N1 U$ H% W9 v程序例: </P> R; d7 x/ @1 z6 f
<P><FONT color=#0000ff>#include <STDIO.H></FONT></P>
4 d7 X: l& q# a8 T+ h( \, S<P><FONT color=#0000ff>int main(void)
3 o5 N' h( O+ u( Y i{
, s$ x' Y/ Y# u t" @( o. v$ t: w( Hchar oldname[80], newname[80]; </FONT></P>
. ~; w( p$ g5 L7 ?8 U. w; w<P><FONT color=#0000ff>/* prompt for file to rename and new name */
$ t* h/ Q1 e% V# qprintf("File to rename: ");
5 o/ ^2 j6 \% C2 v0 E0 j& Ogets(oldname);
8 h5 ^1 g8 ^+ l6 S6 W- T1 Nprintf("New name: "); - G3 b* \' |+ Q% d
gets(newname); </FONT></P>& a1 W, i6 h; h* Z
<P><FONT color=#0000ff>/* Rename the file */ ) v2 y8 q, K% Z
if (rename(oldname, newname) == 0) # `- {% ^" E# _/ l7 ^2 Q
printf("Renamed %s to %s.\n", oldname, newname); % B3 U/ x' U% c) l/ C5 }
else ! Q o8 O% ?9 O; x% P
perror("rename"); </FONT></P>2 s5 P& S7 h1 Y" C7 B
<P><FONT color=#0000ff>return 0;
; e) Q' r8 V$ h. N8 o} 4 j F+ R2 Q) V
. j C- {9 R( N7 a/ b</FONT></P>6 [1 ]) H8 [4 ~- A# ?" }
<P><FONT color=#ff0000>函数名: restorecrtmode </FONT>0 d) F# R* H- {
功 能: 将屏幕模式恢复为先前的imitgraph设置 1 O. u' w( }9 [7 p
用 法: void far restorecrtmode(void);
- g* _- H- k3 `9 d程序例: </P>
) _" l0 O+ N% M5 J<P><FONT color=#0000ff>#include <GRAPHICS.H>
! w) @6 C& r1 \8 R: v#include <STDLIB.H>
: V% I' @& e4 u! z#include <STDIO.H>
/ B/ Z- I y6 |9 N% o2 V#include <CONIO.H></FONT></P>7 B; D6 F$ Y5 |- H' J& r/ v
<P><FONT color=#0000ff>int main(void)
+ i( J2 t. z7 P5 Z: l( ?{
. P1 W- R7 X3 G6 P. |/* request auto detection */ - G; Z1 ]8 l2 z4 r
int gdriver = DETECT, gmode, errorcode;
2 n6 U; N# |, Z1 }4 W fint x, y; </FONT></P>. [% P1 `7 E; O! }$ c6 p
<P><FONT color=#0000ff>/* initialize graphics and local variables */ 6 R5 B! N! `( [; r% A. B# M
initgraph(&gdriver, &gmode, ""); </FONT></P>- G: x" U$ l+ \3 K
<P><FONT color=#0000ff>/* read result of initialization */
3 d3 h' I) {, _4 r( merrorcode = graphresult();
# ^# u; t8 Y4 s+ Y8 |if (errorcode != grOk) /* an error occurred */
3 Z- y6 [4 I |# B{ 5 l( [. d$ |% J5 P5 F
printf("Graphics error: %s\n", grapherrormsg(errorcode));
- d4 ]' t. x# Tprintf("Press any key to halt:");
: {) d) R% E9 U1 _/ Cgetch(); 5 @$ p4 P) L( _1 s" c/ r% q5 S
exit(1); /* terminate with an error code */
9 x7 d, @9 m* J6 D} </FONT></P>
4 K) g7 k8 L% J<P><FONT color=#0000ff>x = getmaxx() / 2; # S [6 B7 |) }& {1 o$ |* |8 n* Z
y = getmaxy() / 2; </FONT></P>: f# V& [8 P+ J% w/ S. R# t
<P><FONT color=#0000ff>/* output a message */
: E) F0 I9 ~$ T7 Rsettextjustify(CENTER_TEXT, CENTER_TEXT);
# V6 Y: O: ^3 s; R' K% p( v& s* }( h( nouttextxy(x, y, "Press any key to exit graphics:"); % P; z( I/ t: j4 w. L
getch(); </FONT></P>8 o( B. |, r" T
<P><FONT color=#0000ff>/* restore system to text mode */ - F; z$ q% ~7 x z1 `1 k7 T. F
restorecrtmode();
% X. A1 r2 _( Wprintf("We're now in text mode.\n");
8 M; {5 a9 v. y0 J& O' J2 vprintf("Press any key to return to graphics mode:");
0 A; l; X0 w( T/ l: B5 Fgetch(); </FONT></P>
3 k9 y' W9 g0 B* ]9 z<P><FONT color=#0000ff>/* return to graphics mode */
$ ?4 [) g" l* csetgraphmode(getgraphmode()); </FONT></P>* L( M# |7 {4 U
<P><FONT color=#0000ff>/* output a message */ ' M* F8 I, r v, J, V" K
settextjustify(CENTER_TEXT, CENTER_TEXT);
# t1 x: d& Y% R" m \* \outtextxy(x, y, "We're back in graphics mode."); 0 @- w1 a4 h' {) \' ?/ Y' S
outtextxy(x, y+textheight("W"), "Press any key to halt:"); </FONT></P>- I( b/ \* H9 _; D" g7 q
<P><FONT color=#0000ff>/* clean up */
, _' S' i. L5 I# h! t- e. igetch(); 6 w, `; d2 w E/ W* c5 c: |
closegraph(); * }* X- E5 J. c# ]) F# ~5 e
return 0; ' Z/ C4 s9 s# w1 k n5 u
}
9 A- w# t+ j% z" S5 h" q. v</FONT>
; o% W$ r1 `; h</P>
9 z4 Z% G4 h, }0 M: u<P><FONT color=#ff0000>函数名: rewind </FONT>
; n$ ^, D+ z2 H功 能: 将文件指针重新指向一个流的开头 + B2 H* `2 g. H8 ^
用 法: int rewind(FILE *stream);
3 A! }$ ]9 e7 n# e! X$ S7 z程序例: </P>
& q' m, Q9 [ a; ]2 q& K- q<P><FONT color=#0000ff>#include <STDIO.H>- p; s, g$ g' m3 X: B4 G% p
#include <DIR.H></FONT></P>& c: P" D- k$ h( Q4 a# @
<P><FONT color=#0000ff>int main(void) + J$ \: H$ O; e8 E! R* y( g
{
* U* z; B! I# l3 dFILE *fp; 0 I& H9 S% e1 }) p
char *fname = "TXXXXXX", *newname, first; </FONT></P>
! _% o/ m5 e6 ^<P><FONT color=#0000ff>newname = mktemp(fname); 6 H3 s4 y& f8 h+ l) ]0 l
fp = fopen(newname,"w+");
8 x- s) [6 N" U% M9 N4 ufprintf(fp,"abcdefghijklmnopqrstuvwxyz"); ) P) n5 k3 a, Z; y& `: p6 i5 o2 G! ~
rewind(fp); y& m, D1 `7 i0 b3 {
fscanf(fp,"%c",&first);
& d8 _, e* R# R/ ~ T4 y+ uprintf("The first character is: %c\n",first); + {" z. W& M. Z; H- g
fclose(fp); # t7 Q. j& j& f, W5 M1 `
remove(newname); </FONT></P>
. o8 t! s0 X/ L! t& P<P><FONT color=#0000ff>return 0;
/ {; ]" I+ }: j+ E" n. M, }$ b}
0 F' r% l2 Q; i</FONT>. A! x% I3 B ^ r( {1 y# d
</P>
j+ F: {8 d% q0 l, X0 Z; t2 x4 |; P<P><FONT color=#ff0000>函数名: rmdir </FONT>% B" L0 r6 O* ?: M
功 能: 删除DOS文件目录 , f, r# I0 @" y- K: _
用 法: int rmdir(char *stream);
* D4 ~; ^" g1 Z& M2 Z/ ?程序例: </P>
) `! r$ Q5 i7 I3 }+ c<P><FONT color=#0000ff>#include <STDIO.H>
- a* p# h2 r* `#include <CONIO.H>
& U( z5 t% Z: ^+ o% H#include <PROCESS.H>
2 R+ S6 x9 s4 C. _0 C#include <DIR.H></FONT></P>
N+ k. Z+ t ?* G<P><FONT color=#0000ff>#define DIRNAME "testdir.$$$" </FONT></P>
* B' T6 `# d9 u: a<P><FONT color=#0000ff>int main(void)
0 `% |8 H2 a% w6 h{
7 p% ] w" p7 p7 c; e7 g9 ]- D3 zint stat; </FONT></P>
% {9 B% R% A: k; l0 U<P><FONT color=#0000ff>stat = mkdir(DIRNAME); % A3 C4 X5 X. c8 t7 A4 c! x
if (!stat)
- S- w, G/ o! i E2 S/ Jprintf("Directory created\n"); * j+ { E4 L m8 S+ b5 C
else
" @6 L* J+ k! h0 U1 J0 D2 I- Y{
p1 H! ^+ }" ~printf("Unable to create directory\n");
+ m9 ^7 w- {: E& cexit(1);
4 c' N5 N ~! V* v& W% o} </FONT></P>! ^- K6 G! E3 W: t) P) S% B' S% o
<P><FONT color=#0000ff>getch();
5 t0 F6 v/ {: I& w9 {3 ?3 M" tsystem("dir/p");
! r4 c: d# D# P* x& _getch(); </FONT></P>6 K8 [" p I4 t7 x+ t( R/ h( Q; F
<P><FONT color=#0000ff>stat = rmdir(DIRNAME);
2 o6 V1 {3 n5 ?2 r5 B1 t' \if (!stat) , Q3 F7 g3 s; M
printf("\nDirectory deleted\n");
1 h/ C) F' x% X* M; `, }else
* n# R1 g0 F; Z3 R{
; h) u7 d- R" H" U+ Kperror("\nUnable to delete directory\n");
+ y; ?& }- o3 k9 Kexit(1); ' F! N; I4 D# C/ L" ~# c
} </FONT></P>
6 h" s, f- S" \% D<P><FONT color=#0000ff>return 0;
- a( W/ o5 X6 l& n" m9 t/ p }} </FONT></P> |
zan
|