|
函数大全(c开头), G( R8 H) t# u4 z3 n
5 T7 v5 \6 x) H函数名: cabs
9 ?) Z0 m j! E8 d( {0 @" I" r2 e功 能: 计算复数的绝对值
/ P4 r8 B) H! y9 U9 j用 法: double cabs(struct complex z); 7 }. [+ y/ V8 @8 v2 Y
程序例: 5 [' d3 R, H0 ^% w* b
#include ; G% v3 L' D. T
#include ) t! O6 G/ q! L
int main(void)
. ~. [4 \. `* c" V1 a! A! S5 n{ 0 o$ G% R) r( x% p
struct complex z; 3 p, Z# `( g$ I4 A" @8 F
double val; 6 e0 T4 C' i. ]% D
z.x = 2.0;
% L3 b3 R0 f2 E+ W8 |" Dz.y = 1.0; 5 P7 A: T( N2 }& g% `
val = cabs(z); , b7 w3 T: m; ^' F2 z
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); 7 E1 N+ m' o, u7 s6 d% r7 w
return 0;
) g& @% `4 g& W7 j; ^}
2 p( W: E, P1 D2 T1 k/ V. v3 w/ h2 F
8 W7 e3 D" T' d 7 @% o' ^- v% }# K
函数名: calloc # p, K8 c! M$ e+ ]7 S1 ]
功 能: 分配主存储器 2 |) V! w. M9 R' A* v4 T
用 法: void *calloc(size_t nelem, size_t elsize);
/ f. k: m1 E. v2 d; d4 Q程序例: . H U1 r7 b& g
#include ) ^4 M3 z( F5 g% _& ^
#include
- \5 u4 V2 {# h- C6 P& u) X% Cint main(void) + B; }1 q' V( {' Z8 ]
{
5 I$ A+ S/ d$ zchar *str = NULL;
" f0 e, d! w- `* u% g8 r/* allocate memory for string */
# Z/ x" S! W5 p, T# J+ @9 O9 pstr = calloc(10, sizeof(char)); 4 B1 ^/ B+ F3 a) k4 C. k5 k
/* copy "Hello" into string */ 0 M( ]) p! ]" Z0 J
strcpy(str, "Hello");
# M/ J6 h/ T# C- K0 |6 P. E; R3 v1 d/* display string */
* Y( U% E+ Z- ?9 ^( m mprintf("String is %s\n", str); * I% r& Y" i# K
/* free memory */
' ~4 `1 O2 l, }; H/ efree(str); ) {. T9 M9 _/ l2 T& a7 K/ p* @
return 0; Z; v" H3 T# Q2 u0 q
}
% b8 D& E( T3 q ^. W" r2 d3 M, l
8 r& J4 i, r. ?$ [+ K6 r l! t9 c. [; c) J+ ] g8 J# A
( H/ ?, j0 j- K: X& U函数名: ceil
6 A4 j( l) d! y功 能: 向上舍入 6 t- Q' ~5 Y6 G% Y# D: v
用 法: double ceil(double x);
5 {3 M* V- j0 x# y* R+ V7 y程序例:
/ v7 Y' N# w; q- n#include 5 c: [+ }3 I; |4 y" \& e2 C
#include 4 N" Q2 E: c9 z9 O
int main(void) $ C" K6 I8 K. T
{ V4 e ?" _& C) l; S# ]2 l
double number = 123.54;
4 ?+ @- y5 W/ n- i G; m& X! Bdouble down, up; 8 w8 w, ?0 O! i; D6 C8 z# w5 y
down = floor(number);
+ A3 u/ k L4 ^# Lup = ceil(number);
7 Y7 g8 Q( N m) I3 sprintf("original number %5.2lf\n", number);
1 W' Y# U) H! s+ ~8 Pprintf("number rounded down %5.2lf\n", down);
- G3 J6 `( ]4 P# [printf("number rounded up %5.2lf\n", up); 3 b7 M+ ]2 p1 M( L, p
return 0;
' t* i* R, Y7 \/ f0 z/ ?}
- y, q; }4 z4 ?0 K1 a* l2 Z
) a# `" l. _2 ~9 s
& R8 k1 L& z& @3 A2 x, Q" N 0 I- y! A9 L0 Z% I% ~5 b
函数名: cgets
, O: i0 O: y2 ^" Y/ _! z* _功 能: 从控制台读字符串 , @" z' z2 ?& I- F
用 法: char *cgets(char *str);
. v& S" C- [( a9 }程序例: : J9 F1 R: _7 N; n( v
#include 2 c6 _, ~) o- ]/ O7 }
#include
, z9 C- j$ i$ r6 W. A' ^int main(void) * n; J7 F& t" p3 D$ F
{
0 \' H5 S' f. g2 Uchar buffer[83]; ( @$ D% `. R* b2 S: j5 V
char *p; 5 X! Q2 `9 r1 v& }+ t
/* There's space for 80 characters plus the NULL terminator */
5 N5 w. g' H) K. s; C4 h2 }! _buffer[0] = 81;
0 W* h/ K$ J2 k" [# \# Wprintf("Input some chars:");
& b2 h; w! @) m1 Q5 x4 n# y9 q: ap = cgets(buffer); : ?3 f6 a" J# g8 o+ V2 u) w
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 1 Z- ~/ A- S9 a, t& @
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 5 w2 a1 g6 `. Q9 S! }
/* Leave room for 5 characters plus the NULL terminator */
; m5 P/ B( Z* X( q$ G8 U; S5 J" Jbuffer[0] = 6;
2 s" Y* D- j5 X1 w9 S+ {printf("Input some chars:"); / s, @4 |1 M1 l0 H M% o. B. }
p = cgets(buffer);
{) t2 L2 N3 z: |- G( y% ?printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); " y: q( T/ P" a9 G
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 4 f2 V! M+ v0 H( S F
return 0; 1 Y. O$ M$ N" |6 z: t
}
& N- S5 M/ R# E" z% @9 M# b% `/ [' X5 r# |% Z' c
* f0 U* j# A' B7 @3 {9 _
6 J" |3 H; p' ~1 u1 P, O& J9 k& e0 C
函数名: chdir $ X" M- J& h) \7 e1 p! O
功 能: 改变工作目录
0 M" ]4 i, \( [+ L& f8 J用 法: int chdir(const char *path);
' T* {! m+ t% @6 m7 b, E+ Y4 y程序例:
1 v) w3 C1 ~3 I- Z#include
7 m! K; T$ k( t# H5 B" x#include
! Q, |2 @0 |; w9 C5 Y#include 1 ~' ^+ G5 @. W2 G1 B" E& t- m! Y
char old_dir[MAXDIR]; " `; C+ b, @, O) J" |
char new_dir[MAXDIR]; 5 U" H. {7 [! v: K/ w7 e9 M$ Z
int main(void) 2 j4 ~) b0 b( ?5 d1 X' e) E* X8 f
{
/ H, \) Z' o5 @2 M- }, sif (getcurdir(0, old_dir)) & p) Y! J% c& t; S0 U
{
5 o0 H i2 V) Eperror("getcurdir()");
) r; \6 u4 X& V6 o" I: Fexit(1);
# r8 y- O+ @8 t& R% p}
; A3 d( Y4 ^9 r2 V4 Zprintf("Current directory is: \\%s\n", old_dir);
# O5 D* _1 e; T) h6 @0 jif (chdir("\\")) 1 b; h: X3 k2 U2 v; }; q4 l! r, u
{ B& }% `2 t3 ^
perror("chdir()"); ) g- S& |( }& |1 `) G) c A
exit(1);
6 \9 p- F. G0 m9 l( s% ]- R. b}
* e3 A, k, k# d7 |' j) L, r" Qif (getcurdir(0, new_dir)) 1 y4 U7 k" Z- E+ I4 @
{
; V5 e0 c9 T% Nperror("getcurdir()");
# p* L- Q1 N. Z% C' P) Iexit(1); + b* E5 v! F2 C; o3 J# Z
}
) d1 `( n- m: \; xprintf("Current directory is now: \\%s\n", new_dir);
7 n- p# o( y# Q; z5 A; jprintf("\nChanging back to orignal directory: \\%s\n", old_dir);
, _2 f% \3 E) S) Lif (chdir(old_dir)) " B- J d' h6 b: d( s4 H
{
- Y7 d; }8 Y _4 cperror("chdir()"); ! t) L9 X4 V6 q; [7 K! ^' n
exit(1); % G: S! T* c, v5 o+ G, H
} + T" S. r; w4 ~% V" M+ D: L9 G* j
return 0; * E% X/ d2 T& A4 l
} * R) ^0 k* a, G2 e- g- @2 g, Y) o
# T, u. B) o9 H# _. |0 f7 d
4 g% _5 Z& V1 B( T d% I) J3 a& J函数名: _chmod, chmod . W- C% \+ D) c3 [9 X2 f9 I' W
功 能: 改变文件的访问方式 " g- ]9 @: v; g% {7 J
用 法: int chmod(const char *filename, int permiss); & r9 V3 E7 y' F6 R
程序例: 0 p% P4 H9 n, F) f5 z$ y0 A6 d
#include ; {) u+ `: a% e& ? P+ g
#include ! g' ~; u `/ P3 L- u
#include
* A8 x/ O" ?: P/ J7 a7 J3 V9 gvoid make_read_only(char *filename);
* E% Q9 b2 a! Q2 k# U( C* \int main(void) ' K# e g2 l3 V# s6 A
{ 3 Y5 Z- v, L1 c: N
make_read_only("NOTEXIST.FIL");
- e2 y) \. O' \3 j! s8 ~, Bmake_read_only("MYFILE.FIL"); ( e C, n p$ P+ w7 k
return 0;
8 @& W; c% [4 B8 l}
- E5 T7 i; ]0 q1 h4 lvoid make_read_only(char *filename) . @% G+ e. }3 m. n: E% y! A4 F
{
$ T7 g5 X o/ V# q8 Dint stat;
4 `1 U& U0 C4 i3 |5 Astat = chmod(filename, S_IREAD); 9 T {8 w, {! u. \7 f
if (stat) ! x6 X% J6 ]4 \9 Q+ P
printf("Couldn't make %s read-only\n", filename);
( F7 d/ |; I9 u L3 } yelse / j0 L \5 z9 L0 w
printf("Made %s read-only\n", filename); 4 }7 `1 M- `! y* X( d6 C' L: b5 \
}
J/ }/ J( ~! A3 K: x% O l
# _5 w4 W( S! K& l- Q% v* v5 B: p+ e) p" x
% [% Z1 Q! w. W( w- W0 T0 Y函数名: chsize
: C9 K5 t' L* D7 x# N功 能: 改变文件大小
/ E: k+ k, G/ d' j8 R用 法: int chsize(int handle, long size);
, i# h/ r" s: D) i3 d% [程序例: ) k. z% H6 v6 \( _7 P
#include
8 U- y; w" j0 l; S: O#include
% N% h' K0 A. G. r8 y#include # ]2 n1 j5 g+ C, k: s
int main(void)
# G/ i! o+ G6 e: o{ 0 r7 q- k0 S/ t! |9 i' p& {
int handle;
1 }+ P( k E- J! j1 q; X. kchar buf[11] = "0123456789"; / m" F, J- W" c) b) y0 }1 D% V
/* create text file containing 10 bytes */ 0 P$ p+ K, g7 T$ r
handle = open("DUMMY.FIL", O_CREAT);
1 G4 c' y6 n) Y1 Z5 Lwrite(handle, buf, strlen(buf)); 9 k; [1 A. j+ ?3 ^3 x
/* truncate the file to 5 bytes in size */ 0 J+ k- |1 k! c7 I1 n
chsize(handle, 5);
( n7 J6 a; I& E6 K2 p/* close the file */ / m) e. |+ A/ R/ f1 S. Y% ]# ~
close(handle); ! d c. l1 h7 O d9 ?2 X! [7 W
return 0;
9 T. K8 Y% g: v0 I}
6 Y5 q/ k0 y3 K' l) v6 W& i/ {) b5 D0 e/ K
8 k7 B9 f; l" o2 X0 p$ R2 W! Y9 {
函数名: circle
( i! \! n0 @4 a: }$ q g m- [功 能: 在给定半径以(x, y)为圆心画圆 ! H# K6 z1 X1 X- y- f/ g% \6 s
用 法: void far circle(int x, int y, int radius); 8 W) b& c2 s) T K6 g
程序例:
" ^: \9 B0 N6 m# ?6 W) t#include
9 e+ ^6 i9 N$ b9 x#include % s. l3 c) i6 p A
#include
0 p5 o: [9 ~9 m7 Z' j. ?#include , E3 `6 y) O" h& q; Q4 {8 ^& A, {9 v
int main(void)
1 g" {% t: o$ ?# G# q9 [+ W{ % @2 |( | e0 J1 w6 O: n7 S
/* request auto detection */
1 c, W- J/ v' H& @. S' oint gdriver = DETECT, gmode, errorcode; . H4 O% p; N% j. n0 H
int midx, midy; 3 |0 f5 n- \0 x0 `
int radius = 100;
% F3 D( S# B) ]4 z* r/* initialize graphics and local variables */
z2 P: j6 ~( A2 |: U# x0 Z# H( `0 jinitgraph(&gdriver, &gmode, "");
4 z% x* H* S4 F6 h/* read result of initialization */
3 |9 P: j! a; X5 Oerrorcode = graphresult();
9 R0 Z$ W1 W1 Z6 V) X V. cif (errorcode != grOk) /* an error occurred */ + |, [/ [7 U. v8 |$ N' l
{ 8 j; h$ G, e" y1 l: h, ]4 V
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ; s L! f9 r$ z* }
printf("Press any key to halt:");
! N- P0 t% T; j7 ]" Q" A* E: Ugetch(); ) Q1 ?0 W/ j+ t& h# L
exit(1); /* terminate with an error code */
% [$ J1 s5 c' F} * G& k; `; a/ R B" X
midx = getmaxx() / 2;
6 X6 k1 |5 D! a9 ~/ xmidy = getmaxy() / 2; 1 O. W) ]4 M. m* j g, V5 y8 ?
setcolor(getmaxcolor()); " ^; E- n) r; G% p
/* draw the circle */ % Y5 }+ [9 t8 |1 z
circle(midx, midy, radius); . s4 M- N% u7 ?( i1 z# `6 [
/* clean up */ n/ t5 U4 w" R# t2 h* y. d
getch(); # `+ }8 {: X$ Y* y9 M, j: P
closegraph();
% P" \) A! V! Preturn 0; $ K! a4 i; c0 P3 h
} . f, k9 u, a! S6 E! m6 |9 I8 y+ ~
& e: J: `3 ]5 q) T5 p, t/ X$ G* m. P8 A
! w! ~& a; u' m# D* u- }1 E I c函数名: cleardevice ' X5 ~& [- @9 U' v0 V$ G
功 能: 清除图形屏幕 $ \* @1 j" H, E4 k+ Q1 R7 O. g! @ b
用 法: void far cleardevice(void); " ^ {) _' b) s% o
程序例: * K5 K* K3 ` n$ I. {9 m
#include
2 B8 E: t2 N% e' g# ~% R#include 0 M6 K9 F/ \4 r1 N
#include
' V! @0 @1 [' k: J# W9 o#include . G' y' x8 Y' {$ }, ^, J: U
int main(void)
5 V- X! [$ l, q8 C- V7 O{
1 I; {8 }8 |+ S& d/* request auto detection */ # D! B2 O, `, [0 e/ ^2 t
int gdriver = DETECT, gmode, errorcode; * J/ U' P. T, `" L& ], I
int midx, midy;
9 `" y4 [1 d0 m: X# e$ R- L7 ^/* initialize graphics and local variables */ 8 l/ }# M0 Z7 B# b9 p+ y
initgraph(&gdriver, &gmode, "");
4 I6 k* u c4 J+ _" U/* read result of initialization */
1 D$ e# N7 O/ berrorcode = graphresult();
* [9 V' {$ F. C! j0 p7 ^6 E9 ?if (errorcode != grOk) /* an error occurred */
7 I# Y( T! @5 ~' S{
~' A# D- {# k6 wprintf("Graphics error: %s\n", grapherrormsg(errorcode)); - g8 i9 b/ P/ F! Q: x5 ~9 G6 X6 A
printf("Press any key to halt:"); 6 [/ O5 e( D ]. K$ k
getch();
% I) u' h" _# K) D3 Wexit(1); /* terminate with an error code */
2 _- E) G- L' q: f4 v, P1 m} ; _: T4 l+ A# j2 Q: G+ ]: S
midx = getmaxx() / 2; / X: |% v$ X0 {: S; u" g. H
midy = getmaxy() / 2;
% z, w+ z7 M& q* usetcolor(getmaxcolor()); 7 j3 V0 e% ^+ l( i- p4 i2 g- q
/* for centering screen messages */
! {% Y! a+ i \, Fsettextjustify(CENTER_TEXT, CENTER_TEXT); " R5 o) ?) e( H$ a
/* output a message to the screen */ * q' k4 `( L/ h( @$ [% w" O
outtextxy(midx, midy, "press any key to clear the screen:"); ; }0 ?: y8 K/ s! S/ k6 _! E6 P2 Z
/* wait for a key */ % y8 E' U% K1 u* c
getch();
6 T, p R0 M) ?+ S) k/* clear the screen */ 4 S- K3 l5 }' h3 d; U' _# p7 |
cleardevice();
- }$ y8 [: n; E/ S0 @/* output another message */
1 C# i# n) S) i6 Vouttextxy(midx, midy, "press any key to quit:");
7 v9 T8 X W/ D0 O/* clean up */
& |2 C$ H. A* l2 d" \; ], \getch(); % _( |6 i# R) m$ B0 \5 W8 ^8 K
closegraph(); 6 J& q. k# j2 g% \( }
return 0;
3 O) Y, C6 ?; v) R}
& z$ |) { X# w( A9 l1 [7 ^' h* W+ K
+ s- E! D1 J7 G- v# R* P, W7 C ) t9 h3 g" i" P
函数名: clearerr
5 D1 M) n% y/ N$ W- B) z功 能: 复位错误标志
g0 c) `2 A5 I: j5 U# i O用 法:void clearerr(FILE *stream);
9 p8 z9 e0 u* }4 b( N- W2 }程序例: ) m) w' q% U4 q) t; S
#include
7 ?/ b/ [& S X" ^3 l2 pint main(void) ) S3 o; o3 V" I# q
{ . ?9 J2 l& M0 f% a7 Q. U
FILE *fp;
: {. a( [' z- R+ z- L+ Gchar ch;
0 O" B& @3 Q9 @$ X3 F/* open a file for writing */ 1 A4 X5 T( Y# T6 U( S) K
fp = fopen("DUMMY.FIL", "w"); ( R5 j) e. t# U' q1 h) Z" u
/* force an error condition by attempting to read */
) h& q1 s& Y: f& u# C0 Y1 vch = fgetc(fp);
8 V' d# u9 }) z) m0 U8 R; Wprintf("%c\n",ch);
2 l3 W: O% X' d0 @1 Xif (ferror(fp)) 8 y" W; P4 D+ H7 C" l# _/ } G* `# b
{ : O1 @+ N2 `7 j9 {$ u3 t! x
/* display an error message */ " D2 C" `! @) C7 [& M0 t
printf("Error reading from DUMMY.FIL\n"); " n# V+ Q% S' x9 N( f+ W* T
/* reset the error and EOF indicators */ ( A8 N' v$ L* k$ _7 T; T7 c
clearerr(fp); 9 G0 N+ i0 `- l& B% c$ |1 B
}
' E/ S* F4 z2 {- Q) Hfclose(fp);
; s, T6 J) C% e+ T! e% {return 0;
; E9 U' F4 i6 ?/ T} ( \" ]( q1 r/ {) h
+ X/ m7 K" {' Y$ U/ j. U: y
1 U6 l+ S; p- Z1 r4 O- a ) S: y- n- u! t1 ?
函数名: clearviewport 1 _& ~1 }6 o5 w# q) r
功 能: 清除图形视区
$ p5 M! E9 V* _! D1 b' ]用 法: void far clearviewport(void); . _' e: ]& t4 Q
程序例:
/ z4 b8 X( _6 i9 I7 s2 \ j T. v! ~#include 0 x% k# {; }4 V* x+ Y8 K3 s2 _
#include
% n$ }# A& x F4 H1 r5 g, l#include . B4 e' Y0 M: q$ K4 W# \& t+ J
#include
1 q2 K* Y; B N- \& |% e#define CLIP_ON 1 /* activates clipping in viewport */ % l& q. Z) u+ q& L2 E
int main(void)
* d3 f" I: h3 [3 w; x- o2 }{
6 g9 R" h7 b1 e2 J; V3 v, g/* request auto detection */
1 x- ^2 s& k" ?- R. w9 jint gdriver = DETECT, gmode, errorcode;
: O9 e1 r/ S, j6 [1 k- `int ht; " q* {$ n8 W. b: i1 e# E
/* initialize graphics and local variables */ " B( k3 `4 z8 @: {! K
initgraph(&gdriver, &gmode, ""); / N# O+ I/ \8 U) _3 m1 O
/* read result of initialization */
1 F5 c5 b* a" ~/ d: kerrorcode = graphresult(); 0 H" V/ S3 l8 d; c! x
if (errorcode != grOk) /* an error occurred */
: k7 X* F, k0 R% T' w, Y; c{
/ @ Z& x b" e5 e9 \) g8 l5 Aprintf("Graphics error: %s\n", grapherrormsg(errorcode)); + t+ k9 l# B+ p2 Y2 n
printf("Press any key to halt:"); $ w2 y4 Z( N+ C8 k+ R$ j0 ]
getch();
3 m$ Y& _' C, t+ T$ C# |# D% @exit(1); /* terminate with an error code */
% ?. A* u: B8 `# x} ' S" k0 @( G8 ]7 @
setcolor(getmaxcolor());
# j+ E: e4 _" y6 g& T/ }) A9 Lht = textheight("W"); & n* a0 e0 J1 r6 }, o
/* message in default full-screen viewport */ 1 V. n4 z2 `: u% ?* ]
outtextxy(0, 0, "* <-- (0, 0) in default viewport");
; o0 N# U1 R" z! w8 m7 x/* create a smaller viewport */ ) o- a) T+ i& z
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); * U) l K+ s- w3 V% d& i9 o' ?8 t: X
/* display some messages */
& S/ z$ G/ K( O) a% |' Xouttextxy(0, 0, "* <-- (0, 0) in smaller viewport");
, h7 K; ^( T0 i. uouttextxy(0, 2*ht, "Press any key to clear viewport:");
& d8 T6 C( r7 b/* wait for a key */
4 n' S+ A8 z1 d8 ggetch();
# D6 k" L: ~, {& j/ e q: J/* clear the viewport */ ; C6 v6 E# _! w% w, q' P( K! w
clearviewport();
$ ~# \; J2 x( H* d+ \0 [+ x/* output another message */ & k( ?3 @- l( {; f
outtextxy(0, 0, "Press any key to quit:"); 4 W/ A$ Y+ W/ M8 y
/* clean up */
- @6 {5 N- W/ i: Y' e( ^getch(); & i d/ O" y5 d9 M3 b, ]
closegraph();
' w0 y+ q& \- E, J) Jreturn 0;
( q1 J( p8 Z. c$ v% ]' c- M: y}
; \# \0 |. Q6 Y! F+ F6 V+ M/ F' S* s- W( H8 t2 S
5 r2 Y/ o! X, I$ Z* g5 Q7 ^- B
" E. _, h l% {& g函数名: _close, close
. \$ t! e* d' f5 D' ]( Q功 能: 关闭文件句柄
, e5 E8 p; m' H7 K4 X8 ~6 t+ @) P/ X用 法: int close(int handle); 3 S* D# F* u& s. T' \6 n% N; C# |
程序例:
) d; w! c% |; f: L' g#include
3 X& C- `8 U% O; v) Z- n#include
! |# u7 P* B5 z! e#include
$ j) {+ K- U$ q: d, u7 `#include
2 V9 N4 {6 f( k1 W" E. N, ?main() . g# ^1 N+ d) w: W3 ]) P; g
{ O' T; i& h/ F: k
int handle;
# v9 A, H8 m x0 r8 achar buf[11] = "0123456789";
) q7 W! G; C* n) X& U/* create a file containing 10 bytes */
+ o! Q1 k8 v% q9 u8 C2 Jhandle = open("NEW.FIL", O_CREAT); & Q2 z5 q; O% T+ p
if (handle > -1) 7 I) x! u/ Q v% m6 {3 |
{
$ D- k$ r: X' x1 ~6 \' R- jwrite(handle, buf, strlen(buf)); 0 S6 U9 @0 a/ e) C
/* close the file */
, [/ S7 |8 _4 Y8 m" A# Nclose(handle);
1 o4 `5 t, \' O ], ?5 w}
1 m+ p% u5 j2 t; t5 f2 Selse
& F" {/ a ^. k- g3 r{ " E& O) U/ V" ^- ~. }. X! N
printf("Error opening file\n"); / z9 b5 u- N- l3 }
} # @1 D6 J4 ~; ?6 s
return 0;
8 k( d9 S5 D# {: I% p} 9 J9 g J( z9 c/ N; w5 ^/ D6 `
: z; b7 e5 ]& A/ h4 r/ C9 X( [8 U2 `7 Q, U( w
/ V d# \; L7 _0 A- W: \
函数名: clock
+ ]3 V' G' s% S: L" T: _& K2 ^功 能: 确定处理器时间
& O$ @- G" P% T用 法: clock_t clock(void); 4 g( V6 z5 @0 t" h# g
程序例:
3 m/ z7 b/ @% {* H#include
9 z% ^3 B% A& J* z8 p. ]#include # ]) o$ j! R* V V
#include
3 z3 \: I; x: Y- O- Rint main(void) : E4 r' [- @5 @# Y/ z
{
* c. L/ Y! d5 W0 qclock_t start, end; 6 l6 y! I! b1 ?8 f, m! V
start = clock();
% e. n3 j- m, C. c1 {+ r$ J9 zdelay(2000); ! R ]- C$ [- R* g- T% G
end = clock(); 5 J, \9 i* {2 }- X2 @2 ~
printf("The time was: %f\n", (end - start) / CLK_TCK); $ o1 g, F- V. q$ C+ v9 W& \9 b+ o; a
return 0; 1 e; b% h) \/ m! q" K5 m+ a
} 1 z/ w2 S- j3 J$ u5 j0 u
& Q1 D* [: A+ x1 a# T3 I
# Y7 q% g5 w) Z8 l; O6 e 9 G c$ J0 h1 a1 _6 |- w8 e
函数名: closegraph
: ]3 U& Y: r; W8 V$ V功 能: 关闭图形系统 ! N; t& Z& A, T8 Z' _" D8 q/ E
用 法: void far closegraph(void); 1 J& T4 W7 q# ^! H& M: q% E
程序例: 0 J- s# e9 `1 ~6 K
#include
( L3 G; o& N( G/ ?( R( d/ _#include
' |; V8 D' \9 o#include
3 i& V9 I% ^3 d$ n#include
! T$ m- {( i1 o* U7 Jint main(void)
/ s3 T' }. x: W7 f3 A0 A{
" i8 |/ y: g3 c/* request auto detection */
& o9 t3 Y+ O- a; C3 ?$ i, v$ Eint gdriver = DETECT, gmode, errorcode;
* p3 A' n# l* U" W% pint x, y; 0 \6 @& g$ r$ K# k, k
/* initialize graphics mode */ 3 @( v* x2 L, g6 R' K7 L: s4 O4 Y: p, y
initgraph(&gdriver, &gmode, ""); 3 l3 o) A9 }6 `; E/ O
/* read result of initialization */
Y+ J# b4 D: Y. l2 h- E& Y8 `& @errorcode = graphresult(); : c! z7 v( p4 T( Z# d& R' H
if (errorcode != grOk) /* an error
+ S2 l6 X2 i6 e6 b9 V6 p6 Zoccurred */
. h9 f; k+ {: Y- c$ k# u. g{
) {, H5 y- x; d, C# Nprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 7 i9 ?& E- V7 p' Y: }9 i
printf("Press any key to halt:"); " m4 D# I1 c2 \2 J3 p! Y7 z% S
getch();
, Q0 i( ~8 v- u, }4 xexit(1); /* terminate with an error code */
2 X% M% r3 p( r* R& {' b& t4 I4 h}
; D# q- A' v% ax = getmaxx() / 2; 3 Z( T3 S' o( O; N
y = getmaxy() / 2; ; j S, A8 ~& A$ | R
/* output a message */ # Y5 G% s) u1 {2 _3 A5 [# p1 B
settextjustify(CENTER_TEXT, CENTER_TEXT);
5 e; t& |8 t& t9 ~# H% nouttextxy(x, y, "Press a key to close the graphics system:"); 5 [, ?* ?) F8 S( Z& A" T! u
/* wait for a key */
+ J2 S+ v$ f2 f; t, h" a1 Lgetch(); 5 S/ K7 e3 o- H) }" Z" T
/* closes down the graphics system */ ! P' C6 P0 R$ Q% K3 M, ], E. l
closegraph();
1 P- Z) s& c2 C- pprintf("We're now back in text mode.\n"); , W1 f. ]' ?) K. p( D% _
printf("Press any key to halt:");
+ n5 ` x5 O) G F8 f5 F5 rgetch(); ! x* |6 G+ y. H4 [3 x6 U6 r
return 0; $ k7 B, g0 ~) e7 y8 M2 c% L
} 1 w1 L j9 @7 y$ U4 q
4 T/ V* x5 V" y9 \$ K4 U$ o( Q
& G0 y, Q) E" E" \5 S ) }* ?1 O8 ]+ C; L
函数名: clreol / x5 v: K* J3 {
功 能: 在文本窗口中清除字符到行末 : N1 n0 n) q! P& T; L+ v% M8 J
用 法: void clreol(void); # k8 ^. f" R1 E: C
程序例: 7 @7 o" J* X7 `5 _6 M
#include $ h$ B- O2 \" Z1 O) J0 C: t
int main(void) 6 }$ K9 D3 L4 }
{
3 M/ w2 j& l' `6 a8 f* h7 nclrscr(); & Y0 I! E0 [7 U5 _! J$ ?
cprintf("The function CLREOL clears all characters from the\r\n");
& {+ B9 v, u; D. q8 R3 Q! icprintf("cursor position to the end of the line within the\r\n");
2 d ]. ^3 @1 _9 K/ \cprintf("current text window, without moving the cursor.\r\n");
" `/ E d" Q, w" }2 e/ Rcprintf("Press any key to continue . . ."); : F. g+ A6 H" \, ]* }* X' B
gotoxy(14, 4); # ], E: A+ I+ Y
getch(); * t$ n) c7 Z1 m4 ]3 d( K( g
clreol();
) ^/ D" Q' O2 s# O |: egetch();
6 W+ \! |0 ]5 Q- l/ ?return 0; & b- G$ v, }: ]
}
+ C i2 M: h7 C$ {1 v+ b4 T0 f4 t: g& P
% H4 S$ V" K! ^/ U& S
2 p4 X5 t, h9 x: N: |' c函数名: clrscr 1 E8 j# U" E6 F( E1 r: t
功 能: 清除文本模式窗口 + f$ Y/ E- K( E7 n. U
用 法: void clrscr(void); - P2 C* V. a A
程序例:
8 `# ~9 ^$ K7 B/ X#include
. X: a+ j# r O* Z" N! Eint main(void) ( V }$ g3 m& B& w9 a- a! t5 q
{ * ]$ T+ i$ l: Q& Y
int i;
# T7 m" T ]7 Y* F$ Z9 mclrscr(); $ B% i2 n+ C& z2 p
for (i = 0; i < 20; i++)
: w8 n6 _# _8 W9 dcprintf("%d\r\n", i); 0 \4 g( k; k# T
cprintf("\r\nPress any key to clear screen"); % L( t6 k9 u3 ^3 Q1 p
getch(); . \9 n7 ^% ]& k5 Z2 ~4 L
clrscr(); ( L6 |' M; i* `4 i: ^. g
cprintf("The screen has been cleared!");
* W5 g5 p, i9 P* L5 }getch();
( P! F7 A9 a3 @9 I( preturn 0;
/ L2 p0 |0 _/ y9 f. L}
5 Y; l: z. H* n+ q, c( t8 s& W c1 {' a* z
5 C: p7 U) b5 o3 X' J! I2 [
: @7 d6 A) J* U, W函数名: coreleft
& a' Y7 q# x% d; o) G功 能: 返回未使用内存的大小
' L' x) X* E: j: ]' l; M用 法: unsigned coreleft(void); $ l; w( X; z& t* v& t
程序例:
9 m8 o/ V% y) X#include
# n7 ^+ B6 C, @* K0 C#include ; s+ K0 M! Q+ [+ o
int main(void)
. Y% M+ Q4 m0 ~! J! d{ 1 B+ s ?% R! N- Z
printf("The difference between the highest allocated block and\n");
7 y% x1 c" o$ S/ Nprintf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
& Y) y4 Y5 p* I! ~return 0;
3 R+ w$ u% F* t' b) S} ( {* U# P7 ?) P. o. S# R1 |. B
! ^6 X& h, |" i- Q- a, `6 y/ ^函数名: cos 5 ~! Z% T9 T* @4 y& p% ]
功 能: 余弦函数
; w& u% \: A0 y: a( W; @* b# G. |/ i) [用 法: double cos(double x); . I) i$ T3 `1 Y' ~ m2 p; h1 G
程序例: ; ~, g* N9 J, N: |
#include
, M; E/ Y% q( P0 d#include ) C/ e% N6 z# \
int main(void) 8 c. B% m% v2 v2 y% `: A8 ?- r9 V
{ 3 B' C. q! n4 p) c0 C& Y
double result;
/ H9 w: l2 J) X6 P. ddouble x = 0.5; : C9 ~/ X5 t( t0 S
result = cos(x); : c) [, H- Q1 E2 o+ J/ U
printf("The cosine of %lf is %lf\n", x, result); 7 t0 \1 f/ g0 }0 R$ p, W
return 0; ; i" ?2 ~" }9 r& `/ R4 t+ E/ B1 _
} 5 b* s- J. x4 J% x$ L( Y0 F
0 N3 T' U* @4 u/ v7 S
7 T+ @9 q& M0 ?, D0 \
6 y. G& p3 Y- o+ w3 w: R函数名: cosh 6 Q L) R, S2 E2 [" Q0 n9 |# P1 R
功 能: 双曲余弦函数
* H7 d" {3 Y9 \4 w用 法: dluble cosh(double x);
& ^. r O2 O7 |. f1 r v5 J5 H程序例:
! r& J3 p& j/ `( x/ v% P8 N2 v#include 1 c* r6 |7 U( p h; Q
#include 5 d8 y( }) [ N) j
int main(void) - P5 F3 Z0 w3 u$ C
{ 2 v$ H" B/ B, v2 ?$ M1 m
double result;
9 d4 T9 k. r A4 |1 F) M# mdouble x = 0.5;
# e" L; S' H" Lresult = cosh(x);
2 `: s4 D: m* f' d& ~, wprintf("The hyperboic cosine of %lf is %lf\n", x, result); * A) m; C$ h& f q' O& x. w
return 0;
+ N! S$ d' d# f& e. i3 K& K, V6 }} % n9 P4 w0 w5 V. X3 U3 g8 @" Q3 A1 ]4 B
; U1 \+ a/ o* E9 l. a5 p7 u
- x$ s" Z n6 w! b 2 P3 h( P0 X8 v) Y2 q# e/ a
函数名: country
* x y6 e$ f2 t! I9 Q2 N功 能: 返回与国家有关的信息 : h$ T6 m) ^$ _5 C
用 法: struct COUNTRY *country(int countrycode, struct country *country);
]" y5 e4 |& N程序例:
* v& {8 m* v( l& K2 L; C#include 3 R% t: e( j" w, A7 s- Q2 i. \
#include 0 v5 W/ A, p7 G; {! u6 m
#define USA 0 " }& c* y$ b& p
int main(void) # L( M, j! B4 ?& Z5 o
{ + g0 V1 d6 |5 N0 Y5 C
struct COUNTRY country_info;
+ I; Y% I7 ?% I! K; W( m6 mcountry(USA, &country_info); , f; I7 \4 R0 n5 r# X
printf("The currency symbol for the USA is: %s\n",
. V+ D# N6 Z2 |) Jcountry_info.co_curr);
, F, V, M& F' h1 Lreturn 0; 5 W4 p" r S5 r, Y* N& D
} $ x, s9 o1 {7 r: W. i8 r/ D5 L# Y
9 { X) A, ^9 w( a+ q8 f, E6 R- W/ y* y
- @. k# b( t6 x' I4 T, W+ ?: ?函数名: cprintf
! v; I. z4 T1 P. U) w1 U2 R& _' |功 能: 送格式化输出至屏幕 . n3 L Q# g! r' I
用 法: int cprintf(const char *format[, argument, ...]);
k) p0 V9 _8 F程序例: # ~! b" c. j! w
#include 7 d2 U, p! V, \/ k% S, P
int main(void)
& ^! B& }, a; m" G/ ]* H{ : e# [9 P- I7 Y2 o" T
/* clear the screen */ 6 M! f7 ~) ]9 B! ~: {# V
clrscr(); 9 {3 x3 L6 g1 o p* X# x
/* create a text window */ 3 C* o3 v% F5 W5 G* l0 B
window(10, 10, 80, 25); + `; l$ d \& D
/* output some text in the window */
! B& z+ F. ]; H% k* }- S o: Xcprintf("Hello world\r\n");
4 s. F; l: S9 Y7 F% w9 J4 L/* wait for a key */
$ b9 S; {: ?8 q2 w; U' n* i, }: rgetch();
2 ~& a: l3 O$ F# \$ I q( @return 0;
+ g4 R* h) U( z3 i8 [8 |} $ H: M6 w4 w; K% G# O
9 o/ L: a3 }( j4 {+ \
, M) d( o4 E% l( Q# |: z
# ]& M' B6 X5 V) D7 U函数名: cputs
% D! G" b1 {3 J% g功 能: 写字符到屏幕 & Y y; \$ O, y0 A
用 法: void cputs(const char *string);
9 {8 B, _# f. m/ Q9 W程序例:
' ~8 t% Z) P8 c* n) p2 _8 z#include ! j0 y: ]. u2 o3 r
int main(void) 7 W2 z( c3 |% n
{ ; B2 j! ~" |3 F& ?
/* clear the screen */
; H3 D7 Q1 ~7 Y( v! b3 ?clrscr();
+ j- ~# d- O9 O4 r, M0 x( p% F9 S/* create a text window */
0 H y" ^& W z% Xwindow(10, 10, 80, 25); / k5 K1 t0 V& u: U
/* output some text in the window */
$ Q$ s$ C/ E3 H. a2 a, A0 {+ Ccputs("This is within the window\r\n");
6 r2 W* {$ R# h: R a/* wait for a key */ 4 t6 Z7 y2 |$ P6 r
getch();
2 f' B: u$ Z! R9 R/ @6 |return 0;
& q# `# L9 U' H' y- N7 D} " x1 y: w; e, \
9 g8 D; Y/ O2 y; O1 ~" a. K# d# M7 x; b7 [" e+ }0 | R
4 _! B8 b% E5 @# X) a) |0 }
函数名: _creat creat 5 }4 S0 p. H0 K4 C% X+ a! k
功 能: 创建一个新文件或重写一个已存在的文件
$ t5 C- {" D8 J2 B8 [% f用 法: int creat (const char *filename, int permiss);
9 ^8 K1 n* a; \, S+ I0 u+ j* [- y: K程序例: * U8 q9 y# L+ Y' U& K% O. c3 x
#include 7 L$ |6 c8 i7 i8 l; M N
#include / M$ `1 F% r/ a6 @3 K
#include
# u, c, E/ v3 E! B: G#include ( L) T6 \0 `9 M; Z) n+ E
int main(void)
- Q. o# z" O: O/ d" R{
' l& Z/ a/ p3 {9 ~$ H' Pint handle;
5 B4 a* |% Z' o4 i, Wchar buf[11] = "0123456789"; $ |) S9 M* B3 m+ t
/* change the default file mode from text to binary */
* S6 M, L6 p9 R" ~9 Y_fmode = O_BINARY;
7 w( ^: X7 q( m* h# C8 ~/* create a binary file for reading and writing */
$ C! M& K; @2 `+ Rhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
* i) M3 A" {. U# p8 D, M+ m/* write 10 bytes to the file */ * }* c8 Z/ w! Y6 v
write(handle, buf, strlen(buf));
! ?8 [1 v* @/ H. a5 G, ?, g/* close the file */ & |& j( a$ F( r! O
close(handle); w' [- j$ S; h- D% p1 t% ]1 p, b
return 0;
5 K* w* l2 J- [ O, h}
7 C, o; a' O. c) ?8 X6 L " C. I3 i" U% C2 R* s# X' |- a
函数名: creatnew # D% v2 L6 b* n5 h! ~
功 能: 创建一个新文件
3 X0 U+ [) n" }" E, \用 法: int creatnew(const char *filename, int attrib);
/ j3 w9 t/ k+ Z" P g( d8 P V4 J# n# {, d程序例:
+ r7 A# ]2 g: s6 ^' C6 v6 ^#include
& N/ c3 T0 @: J- R- T: e# y% f#include
% e* Z) [+ f* ^#include
: X6 I$ j5 K3 B& w4 ]. Q#include
6 k. z8 J% Y0 J2 ^; o+ b; J#include % E# A2 f+ F9 o$ h+ Y
int main(void)
2 J+ }2 r+ n; s6 u, H) d/ P{ ) F$ D' o, G& ~) t1 F
int handle; 0 v6 T3 r- |# Z8 b3 c
char buf[11] = "0123456789"; 0 H0 q5 B: f) b7 E) t$ ?: U
/* attempt to create a file that doesn't already exist */ 3 s( b3 t9 I5 v% Q
handle = creatnew("DUMMY.FIL", 0);
" [7 c6 x7 f% p- S J) {/ y+ gif (handle == -1) # j4 r& h+ I6 C# l
printf("DUMMY.FIL already exists.\n");
2 E# ]7 C5 J7 l- Y9 P6 Z Z% relse
& A$ W% {* e' x{
; N4 D( c( C: B qprintf("DUMMY.FIL successfully created.\n");
+ A* n5 }( E I* M5 b7 K# E! z+ O9 |write(handle, buf, strlen(buf)); ' ` f6 S+ U' h( C2 F; Y! R
close(handle);
W* X5 h+ ~) B4 D. M% b}
: k' H0 ~/ w9 X, ?3 R0 nreturn 0; % H! u1 Y k- b3 E! ~
}
& q2 k9 i3 ~9 j0 Y; L6 K8 u8 z1 z( I$ K3 d
- c0 g O% }: N1 N1 u
; E' \0 H- K+ l2 n1 h/ f% E函数名: creattemp
- r9 ]8 y$ X6 |- k" ^8 D2 }功 能: 创建一个新文件或重写一个已存在的文件 " i3 m7 z( w; g) t: P. r9 Y
用 法: int creattemp(const char *filename, int attrib); * ]0 t8 B/ k0 u7 `( a
程序例: 6 D- d# l0 [. r0 s# r3 [
#include . w# q8 J* ~$ v5 M% U* E& o' t# ]; Q
#include
+ P& r6 c- f$ o) f/ q1 [) K @#include
' h% o; B$ }& E" }* O. Fint main(void)
3 I: b: I9 i1 E- b) L1 K6 E w7 v{ ( j( s% `$ J) K7 Y: {
int handle;
! W Q4 t. b% M4 Hchar pathname[128]; # r) v& u2 E q9 J5 a
strcpy(pathname, "\\");
/ [7 O' ^1 d$ f- |. U/* create a unique file in the root directory */
! x+ M* c* X2 `# z' x% H4 ahandle = creattemp(pathname, 0); $ Q: A' A: [8 a7 C1 D
printf("%s was the unique file created.\n", pathname);
3 {" N4 y* y2 G$ X3 z+ jclose(handle); ! ~+ f& P1 Z$ R: r
return 0;
& k% N6 G0 ]" ?2 E% G} L, W+ b# O9 C) u
0 A/ H8 k( H' o F
6 S3 u( R8 K/ }. J/ ^% L
9 e; B" H/ p& u0 v, G函数名: cscanf
3 Z) M( R' m2 g* g1 s8 x8 [: `功 能: 从控制台执行格式化输入 / H7 L/ p1 r) x5 |$ K
用 法: int cscanf(char *format[,argument, ...]); ( h( `8 _9 g6 i% [
程序例: / P9 r9 ?! v( N/ r3 u" m! K' A9 C
#include
- K, D8 W& Q) ?9 \int main(void)
" O& Q& V. ]7 L{
0 E" i' ^$ Z- y6 ^5 T9 r: z. ~char string[80]; 7 |& k2 C; d8 W$ [) V
/* clear the screen */ 8 P5 F/ g% x! O1 n( x$ P* N
clrscr();
) Q7 ~" D3 S7 E( @* d/* Prompt the user for input */ $ d9 g, i! X1 W/ n1 }
cprintf("Enter a string with no spaces:"); / p) _3 l5 B6 o! ]/ u. O9 _: ]
/* read the input */
3 a& Q- @4 Y. ucscanf("%s", string); 6 C B9 d4 f3 o. T) w5 L
/* display what was read */
; d I8 X; \, u+ T- p( _/ f2 Ucprintf("\r\nThe string entered is: %s", string);
+ g, v1 m# {- p, Ereturn 0; 4 p9 C# \# |1 T& K* s/ Z8 I4 N4 B
} 7 g) f3 b+ I. s% Y# Q8 x
, P# o# _5 T# E- R& v% _- g( w1 P! q6 B7 X0 a: ?# j0 }
: z7 O& p- Q% P3 U( ?4 ^7 u6 M9 a函数名: ctime
: V8 y/ Y$ J, \& S: b6 _, v功 能: 把日期和时间转换为字符串 / W- V; z5 F! s4 _: S) N1 m
用 法: char *ctime(const time_t *time); . b0 e/ b# ^5 m# S! ?+ Z+ k
程序例: K. b M- j0 ]5 L: X: R n
#include ) I& H2 d: o( m3 u( x
#include
0 z c% A) { i! E% l+ ]int main(void)
' j3 k# _ v% g0 o4 W( t" ~% O{ % w/ s, x i( r- A7 F
time_t t;
+ ]: C. Q% Q0 p$ I) m0 {; u- J& atime(&t);
2 m! J, K K! J. k2 Q+ Bprintf("Today's date and time: %s\n", ctime(&t));
$ @( P; `. n4 v1 o3 @8 q3 l- x: @7 qreturn 0;
+ c% _7 j& h8 R8 M" t: G9 o} 9 h$ s1 M8 q, J. r X( S% x* h
7 f: {. P/ T0 h" l( e; z; m) F5 Y! [; S# j3 r
% t0 a& u3 I/ f2 v0 Q( ^
函数名: ctrlbrk
) W- ^' x' g' ~功 能: 设置Ctrl-Break处理程序 ; a) b1 Z) t9 S& `3 n* t
用 法: void ctrlbrk(*fptr)(void); / p. p/ Y( S" k. I* J2 w }
程序例:
6 M* x! {9 V T- y#include
0 a2 F: X. T+ n% U3 v#include
! X! O- \6 ^- R#define ABORT 0
: E5 }% X0 q" p) h8 B+ F, Gint c_break(void)
, K$ F: ?% _* w{ 3 Y6 e0 i% {8 p- y% l2 E9 |
printf("Control-Break pressed. Program aborting ...\n");
$ [- k# h7 p: E' S Jreturn (ABORT);
& n: B: z9 C6 |& g% v( e9 c0 b& a}
6 a, O, G- l" b# |2 Uint main(void) ) g: D4 n- o: I5 D- M
{ % @- _# f z0 @# j
ctrlbrk(c_break);
9 Q9 ^9 k& o3 t7 Qfor(;;)
" W* O% M. W J0 K3 P5 ` Q{ : T. t) E8 I2 v
printf("Looping... Press to quit:\n"); * t+ z/ N* r6 h
}
4 S6 o0 `1 ^8 c2 N- {# \4 @" L2 ^return 0; + O& R. V; k) O: f3 t, c7 B
} |