函数大全(c开头)
函数名: cabs ' R, Y" C- u: v1 g) S* }% j4 l: e 功 能: 计算复数的绝对值 用 法: double cabs(struct complex z); 程序例:
% Z/ h* z; u$ {$ Q#include
int main(void) { + i; V: U7 v4 F9 T- x9 E3 @ struct complex z; ) R; s' P8 Q1 [! [+ } double val;
z.x = 2.0; z.y = 1.0; ( E- f9 M. ^$ y1 i4 E: m( o$ _. ~ val = cabs(z);
$ }+ l: e5 [* L; eprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); return 0; 0 R/ f: ]5 M" K$ Y% s& l) B0 H } & G; p5 m; v2 P6 O5 n4 H
: h: p; s9 N7 t$ v5 y函数名: calloc 功 能: 分配主存储器 + H2 c8 w7 `, U 用 法: void *calloc(size_t nelem, size_t elsize); 程序例:
) \ \; U F' t! f#include
int main(void) & b* g5 v8 k3 n& p. z { , c: P; _1 E8 p8 z. O+ L9 A4 t/ Q char *str = NULL;
/* allocate memory for string */ + D- V% `' ^- i" J str = calloc(10, sizeof(char));
& a, Q% o. m8 N+ y; i+ q/* copy "Hello" into string */ ( e' F9 r( q3 N4 c strcpy(str, "Hello");
' F/ U; G+ W) A1 t: W( b! b! \5 @/* display string */ printf("String is %s\n", str);
/* free memory */ free(str);
return 0; } - \7 S' B1 u1 m( {/ ^2 i$ g5 R! z & G/ r0 g+ W% v1 |/ o5 Y) Z
+ N9 e5 |0 [5 U9 _9 I. n7 O) {函数名: ceil 3 X5 j8 h I3 Y' e 功 能: 向上舍入 & q) w) b% S- W* _) U: F, j3 E 用 法: double ceil(double x); 5 E' h: p+ N5 R; Y5 `8 R 程序例:
#include
int main(void) { ) R2 x1 R* c6 }) W double number = 123.54; ! i- V, J7 L ~1 u double down, up;
down = floor(number); + ]' s; K x, r, W% x# x4 V: c up = ceil(number);
printf("original number %5.2lf\n", number); * W! y) a% d" _ N! b& s5 O! } printf("number rounded down %5.2lf\n", down); printf("number rounded up %5.2lf\n", up);
1 `/ M* W1 P0 [9 J) k# ]4 mreturn 0; } 4 E5 n7 \# S: s: M: D8 a0 L & T5 I" |! Q p6 K9 {
% e% E% `3 f, Z! C- B4 P# e# I函数名: cgets 功 能: 从控制台读字符串 9 r- m4 o' d9 v4 l5 F 用 法: char *cgets(char *str); 程序例:
) r" M: U8 O! S5 U( P/ h#include
int main(void) 8 B' V' L" p9 D( U/ a' l { 7 ^! a- n- N( h5 x' O+ o char buffer[83]; char *p;
3 q& |2 n0 M9 `' h0 A/* There's space for 80 characters plus the NULL terminator */ $ z# L+ d( X6 C$ Q buffer[0] = 81;
* _- @; A2 Y( { zprintf("Input some chars:"); p = cgets(buffer); printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
/* Leave room for 5 characters plus the NULL terminator */ : M2 N& i: T7 Y; E1 C5 s: b buffer[0] = 6;
printf("Input some chars:"); ( w" P" B- ]! q5 z7 \- G" k p = cgets(buffer); printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); # \$ K2 `1 m- p1 `2 T6 i printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); . U4 d2 ?, j! J1 n0 y return 0; } 3 b3 V. h) z5 K" P: W ' h+ k4 b" w7 [: p5 b) \: S
函数名: chdir 3 o# d' E! o4 G ~3 w% v8 R 功 能: 改变工作目录 用 法: int chdir(const char *path); 7 K: n0 Z4 X; F1 d7 z3 X7 q 程序例:
#include
char old_dir[MAXDIR]; . A; `2 h# t/ l8 Y9 A char new_dir[MAXDIR];
, g* |, A8 T. C: sint main(void) ( ]8 u- m+ Q- v( R0 _& z/ L { . r( G1 g0 D+ d3 H) w if (getcurdir(0, old_dir)) { perror("getcurdir()"); exit(1); 7 L: R+ O# ?: j# W- I) G$ B } printf("Current directory is: \\%s\n", old_dir);
if (chdir("\\")) ' y7 R2 h4 ~; f3 ]/ C/ {. ? { 1 k/ I u; Y2 B# [/ a perror("chdir()"); exit(1); 8 H: [* \) R, P/ R. Y }
$ v1 m" j% Q. A2 Hif (getcurdir(0, new_dir)) { ) f% @" e. p3 \: V0 m1 m4 |6 S8 S1 Y perror("getcurdir()"); 2 D# X5 z% |2 C% t+ d! T2 M# Q$ l exit(1); } # |1 H; Q& D$ C) X/ v* L! y* S7 v printf("Current directory is now: \\%s\n", new_dir);
) C; F! L$ ?+ E8 Gprintf("\nChanging back to orignal directory: \\%s\n", old_dir); + {# N/ y" v8 W" m9 v2 [ if (chdir(old_dir)) { perror("chdir()"); : Q% R- C% L4 U$ \ exit(1); + Z# Q* |4 V# I }
return 0; , D2 {# h9 J* E3 j X } 1 r" X0 ?8 @! O+ [; F ) p u5 L; W# M6 C0 @' q# c( I. t; q
( O }* ] a4 ~" u函数名: _chmod, chmod 功 能: 改变文件的访问方式 用 法: int chmod(const char *filename, int permiss); + U! J0 S) {3 l) F0 @# Q" S 程序例:
7 X$ J9 Z$ f3 G/ }" L3 y1 I4 J, S#include
void make_read_only(char *filename);
- ?( N( n8 D$ m1 m7 d# {# aint main(void) ' ~$ Q: p4 G# i( U' W { ) P4 k% l4 a9 j% C X make_read_only("NOTEXIST.FIL"); make_read_only("MYFILE.FIL"); return 0; }
' w$ }- b. W& |: H+ R% Lvoid make_read_only(char *filename) # ^5 T3 u4 [$ t; L/ \ { int stat;
5 w% Q8 C# ]% q0 N! o( X( m: rstat = chmod(filename, S_IREAD); , A3 n; G, g4 l' W$ i, h" A if (stat) & i$ i0 P, k$ Z printf("Couldn't make %s read-only\n", filename); ( m! i7 l" i) W else printf("Made %s read-only\n", filename); 0 D7 `2 O c4 F } 3 N9 m5 [" ~7 y" L a. p9 Y: X Y9 K! w0 s* Z Z2 E( D8 P
" n" y' O% S* y2 \' h v函数名: chsize 8 Q D. p& }! B9 _& M2 c: e: |# V- Z 功 能: 改变文件大小 用 法: int chsize(int handle, long size); 5 J B7 \3 q& D; h 程序例:
- X9 e( `4 o& z- S) d& T% D#include
int main(void) { H4 a) R( f% s8 W+ g- Y int handle; " C2 R! D8 E' A% B9 r, u char buf[11] = "0123456789";
7 m4 K, P# ] d' J8 @1 Q/ n/* create text file containing 10 bytes */ ; M l. b/ G+ a handle = open("DUMMY.FIL", O_CREAT); 8 I; y% S7 ]5 E; h, n2 H6 f write(handle, buf, strlen(buf));
/* truncate the file to 5 bytes in size */ chsize(handle, 5);
4 @8 r8 I# f1 l1 g+ t; r7 ], W/* close the file */ close(handle); + ?- Z1 ^3 I) E4 a( P6 | return 0; } 2 c( i! f d+ [' C9 B1 j) S 6 H6 Q* u! J, A( K6 g; b: j( G
函数名: circle # ~: o; A" F' m4 F 功 能: 在给定半径以(x, y)为圆心画圆 ; W" Q4 u$ A* n" z1 x 用 法: void far circle(int x, int y, int radius); 程序例:
#include
int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; $ C& Q, e9 K5 K+ p2 \9 L: f int midx, midy; int radius = 100;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
9 L4 \* k5 w& ~6 T; b/* read result of initialization */ . G+ e G8 b- R9 N errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ . p2 M. c' U$ N ~# A' q* K: k { 0 a% K" W) v+ J+ Q printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); 3 E1 {4 h2 P+ V getch(); 2 P+ m3 @- Q3 \) }/ l5 K exit(1); /* terminate with an error code */ * G* ^% c# l+ J5 S }
: R+ V+ u7 ?' B1 ^( D4 dmidx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor());
/* draw the circle */ : P: X7 `) C8 E. T$ h5 T$ c: n circle(midx, midy, radius);
/* clean up */ getch(); closegraph(); / _. E2 H. ^) T; j, H return 0; 9 M* F: y! {2 t5 D } : I/ E$ o% n& h- G2 t+ X- f + ~% g4 m& s: \/ t6 o& }
4 t M: N( ]. s& k; i. v函数名: cleardevice 3 e9 j' h4 w. E# p 功 能: 清除图形屏幕 T4 R9 m3 a( y$ x2 B9 i( B2 X0 W1 G 用 法: void far cleardevice(void); / F& ?5 }6 M" s' z' s) \4 X( ^ 程序例:
#include
int main(void) { /* request auto detection */ ; P& v9 S; {1 V7 _! S9 Z. t7 h int gdriver = DETECT, gmode, errorcode; int midx, midy;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
9 P+ J1 m$ i6 y, _3 \7 f1 Y5 E/* read result of initialization */ 7 g+ S: h N" n9 `% @) P errorcode = graphresult(); " F: E9 F" [7 c% i2 L; G9 C* o3 j if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); ' ]; V# T# }( X0 P$ _9 ?* M& P9 P printf("Press any key to halt:"); getch(); & \$ s: o7 S. b' E. g exit(1); /* terminate with an error code */ % i. H0 V# m9 s% R; s: a+ m }
4 B- M5 z6 \+ S. T! j! D* Dmidx = getmaxx() / 2; % w: C: @) F# ~0 F- b/ p: a9 p midy = getmaxy() / 2; ! A4 Q( E- l0 _) R$ ?1 U+ i( i setcolor(getmaxcolor());
9 x' Q, F% D4 T8 r/ s' _/* for centering screen messages */ settextjustify(CENTER_TEXT, CENTER_TEXT);
/* output a message to the screen */ outtextxy(midx, midy, "press any key to clear the screen:");
: D- C( r% g- ]5 r* `/* wait for a key */ % W8 B" x" A( ?" u getch();
! _) }( I! l5 \& B/* clear the screen */ cleardevice();
+ i- I3 E2 X V) U- ^; n5 \/* output another message */ 5 X8 f; m& f. ~) P2 I outtextxy(midx, midy, "press any key to quit:");
/* clean up */ getch(); closegraph(); return 0; } 0 K7 H) ]% t" J& b % ~( x: [1 Y+ x! Z! Q& ]3 a Z1 w
函数名: clearerr 7 s- k& P0 B" @: [7 p0 H: E 功 能: 复位错误标志 用 法:void clearerr(FILE *stream); 程序例:
#include
int main(void) { FILE *fp; char ch;
6 r, t4 o6 n5 G) e7 _: L9 d/* open a file for writing */ K3 U1 E" J& Z3 l, ~ fp = fopen("DUMMY.FIL", "w");
/* force an error condition by attempting to read */ ch = fgetc(fp); printf("%c\n",ch);
8 o+ z1 r w/ {& A3 Eif (ferror(fp)) { 3 j/ o( A6 V* J( Z, S2 i5 [& \9 } /* display an error message */ : p& N+ G0 B/ ] T& x: k printf("Error reading from DUMMY.FIL\n");
/* reset the error and EOF indicators */ clearerr(fp); % T& e' P5 @( s& e8 \0 Z# n2 J }
fclose(fp); return 0; } ! q7 r/ {- `: f3 h1 p " H. d. {' L: q# i
函数名: clearviewport 功 能: 清除图形视区 9 \$ M w( a2 J0 f8 j/ P' e 用 法: void far clearviewport(void); + f1 u9 l$ J1 s) s% b* k 程序例:
* T" j" ^/ _9 Y g {9 d#include
#define CLIP_ON 1 /* activates clipping in viewport */
6 z' n- M1 @' t/ Zint main(void) { 5 N( k. V7 L* d; `, m9 Z0 @ /* request auto detection */ int gdriver = DETECT, gmode, errorcode; . g, ]+ y2 U; U2 O int ht;
/* initialize graphics and local variables */ , S4 u1 V! s! d: \7 q& N initgraph(&gdriver, &gmode, "");
/* read result of initialization */ 5 i5 V- r* m% L errorcode = graphresult(); Z) Z+ v& l; n$ |/ o9 m' _% F if (errorcode != grOk) /* an error occurred */ ' J2 d7 T U0 F7 v/ k3 b* i { ) l$ T+ v* Q' ? printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); ! @. p/ D. @* g$ V w l; c" ~5 ` exit(1); /* terminate with an error code */ " B& `) S. j' q& Z; b% W1 P }
setcolor(getmaxcolor()); 8 l2 i4 G/ S4 F8 n. e4 O [ ht = textheight("W");
/* message in default full-screen viewport */ * m6 @. G5 J. K5 H! ~) j outtextxy(0, 0, "* <-- (0, 0) in default viewport");
/* create a smaller viewport */ setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
1 z0 Y/ Q4 U" L$ l4 Q) g+ Q \ P6 }/ I/* display some messages */ 3 J- a; a% i/ A- n0 @# f outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); % z. I. b6 o" W6 q! Y outtextxy(0, 2*ht, "Press any key to clear viewport:");
+ W# B) u ?# O& c4 {. L/* wait for a key */ 7 h4 z' Y. q/ L) s1 V; y9 o1 k getch();
/* clear the viewport */ clearviewport();
" y) M r0 }7 p6 w% U3 l) W/* output another message */ outtextxy(0, 0, "Press any key to quit:");
$ c, G2 s5 X3 G* r. g q/ O0 t/* clean up */ * p$ `; B$ f, x$ A5 v' k1 J- l6 ` getch(); $ @8 O& D" d; h4 j closegraph(); return 0; ! @, I% i( s# b0 J( ^( i8 \/ d } 8 H& X+ j+ ~/ H+ B0 J 5 Z& H U4 d3 G P$ |; f3 Z7 K& ?) ?
- P: o3 v! c. x4 g9 X8 j; I5 i函数名: _close, close 功 能: 关闭文件句柄 用 法: int close(int handle); 5 }6 ^6 v: c6 {( }% U; H! ] 程序例:
2 ~. |5 t9 D. w5 a2 w2 N+ n* ^4 Y#include
main() 8 {+ ]) O0 |3 P; ^$ ?% k { int handle; ; g4 W; k; I1 x# ?4 A char buf[11] = "0123456789";
: k7 R( }# W6 j: t7 }+ F0 ]/* create a file containing 10 bytes */ X/ k9 C. S- h: L5 a$ x3 t handle = open("NEW.FIL", O_CREAT); 6 L3 @# u+ m, _: G1 ^# G if (handle > -1) ; M7 |" ?" R( ^# W# ~ { write(handle, buf, strlen(buf));
/* close the file */ close(handle); 6 O( E5 X+ l @9 }5 Z: r7 G4 W } else 1 \ s8 n1 f. a3 R, G+ R( ^ { printf("Error opening file\n"); } return 0; 2 K( c' `8 {5 a4 v& T; {, m } # W, I- M! n. @# X" E6 x U k * _* f7 C L- w# t$ y; }) K- S; E% R
a; u' M7 d, \# ~函数名: clock 功 能: 确定处理器时间 4 Y' D; K* P$ l" b) v 用 法: clock_t clock(void); ; ]3 T1 }- o/ a5 F" b* o 程序例:
#include
int main(void) { ) S8 M/ S! V( C+ p1 Q3 [8 R clock_t start, end; ) ^8 x4 |" G/ M' V4 D7 v( U start = clock();
delay(2000);
end = clock(); ( n5 B0 B; }+ K5 _# u2 u printf("The time was: %f\n", (end - start) / CLK_TCK);
0 o8 o0 `! a+ U( Ireturn 0; * n/ x3 u. z8 G# k1 @5 h } " }$ D! `5 A4 y b* m1 S7 s1 R9 {
+ V) T8 [0 l# v" ^- q R% m, L函数名: closegraph 0 v0 C8 E5 ]' H" H 功 能: 关闭图形系统 用 法: void far closegraph(void); ' S, ~$ }" x3 O) F1 R$ y$ L5 x# a 程序例:
#include
int main(void) . q- [4 j% M8 [& J" R5 ^ { /* request auto detection */ - p' k% ~5 `, B# C; l int gdriver = DETECT, gmode, errorcode; 0 I7 a5 P3 h7 D( e" b4 r, a int x, y;
/* initialize graphics mode */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ 9 n; Z/ N) Y0 s2 B ` errorcode = graphresult();
if (errorcode != grOk) /* an error - | ^: W7 C- r- m$ Q; _( P occurred */ 7 c9 Z$ [: d6 {. b. C { ; ?& q% m( L' u# y" F! @- ^9 k" i# \% I printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ }
7 T) v" j& l) Z. O6 y2 n, nx = getmaxx() / 2; y = getmaxy() / 2;
/* output a message */ : N2 I2 K' h+ L$ Z: `# @% d, X+ L settextjustify(CENTER_TEXT, CENTER_TEXT); outtextxy(x, y, "Press a key to close the graphics system:");
/* wait for a key */ ; V3 K5 f# O8 e. ?7 P7 `' ~- Z- T getch();
" m6 J, Q R2 Y/* closes down the graphics system */ $ p$ E* [( O8 L% ^9 Y. B! p: w closegraph();
: J. a, V& Z6 G0 }4 Xprintf("We're now back in text mode.\n"); ) {2 R( }- s$ }$ t: A1 b4 ~/ ?% N& Q printf("Press any key to halt:"); 3 L: n, v; e3 T' N# e getch(); return 0; }
$ M; Z! r" z/ \/ G! n函数名: clreol 功 能: 在文本窗口中清除字符到行末 用 法: void clreol(void); $ F- \* A) K% k( c/ } 程序例:
8 `( A6 w% q; }* e: } j; O#include
int main(void)
{ ' m Q! \2 H9 V. Q. B2 U# J7 r clrscr(); cprintf("The function CLREOL clears all characters from the\r\n"); cprintf("cursor position to the end of the line within the\r\n"); ( }- s8 A; | `8 z cprintf("current text window, without moving the cursor.\r\n"); cprintf("Press any key to continue . . ."); gotoxy(14, 4); * E/ Q4 @, M/ P$ c8 E' ] getch();
T$ x) b b5 U+ M. O( ?. z& |clreol(); ! O: _3 P4 t4 X getch();
return 0; ( I$ q. k& i2 H, Y0 E T: {5 b } 3 H0 K( z# k& P2 u
# d" _6 a! X! V" M. l函数名: clrscr - R+ V* Y! p! U4 r9 Z: R 功 能: 清除文本模式窗口 + \4 E6 s$ T! G; b& I+ N0 q 用 法: void clrscr(void); 程序例:
#include
int main(void) { $ p" g$ w, l1 a9 u3 g6 Y9 Q int i;
clrscr(); 5 ?. \5 Y1 V8 H2 j5 F for (i = 0; i < 20; i++) cprintf("%d\r\n", i); cprintf("\r\nPress any key to clear screen"); getch();
8 e- ~9 w4 [" \) eclrscr(); cprintf("The screen has been cleared!"); getch();
return 0; ! S9 Q+ K1 j; S7 b V- r, ?" `$ i } - k3 x5 ` D8 n; A( u( v9 i8 Y; E
" b l# |% S8 u( y$ Z函数名: coreleft 功 能: 返回未使用内存的大小 用 法: unsigned coreleft(void); 程序例:
#include
int main(void) 2 P1 l$ q P& d& J* ^ { $ ]5 G! k! w0 t printf("The difference between the highest allocated block and\n"); ( i3 |# ~0 q1 @% l+ O) M. ~8 J& A% X3 z printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
$ o: y+ M- P9 S7 Q e/ {return 0; 6 t" [$ i" i& v } / s# b* b6 c3 o9 f$ v1 T' G- [
" c, l3 I' p; D }9 O# }函数名: cos 功 能: 余弦函数 $ C/ K C, q; F; ], l: E 用 法: double cos(double x); 6 Q8 B% T" e' i* q( Y 程序例:
& k- j8 i5 A4 {+ O3 b#include
int main(void) { double result; 8 o* _; U7 n. E/ ^) p5 k double x = 0.5;
result = cos(x); & x/ i; F0 X5 N2 N printf("The cosine of %lf is %lf\n", x, result); return 0; 3 K. c% @( y0 G' ?& u" p } 1 [1 U1 A( O9 G , d! E0 o( U# k+ X
' @9 [) [8 |" K7 D函数名: cosh : D/ ?! C* @4 ^3 o9 H9 ?# I 功 能: 双曲余弦函数 用 法: dluble cosh(double x); 1 [4 {1 y7 H4 B# L/ i0 d 程序例:
#include
int main(void) { double result; 2 |" Q m" Z8 s# w5 q double x = 0.5;
result = cosh(x); printf("The hyperboic cosine of %lf is %lf\n", x, result); return 0; ( w/ L5 ~. c9 m* |# n3 H8 N } 9 a6 ~, t5 h" o6 a3 Q E, v8 a7 q( O; Y5 v! n
函数名: country * h; x/ T) b' y4 i8 L7 { 功 能: 返回与国家有关的信息 ) `9 a: {' f6 z( j: Q, S 用 法: struct COUNTRY *country(int countrycode, struct country *country); 程序例:
Y F3 U B5 ]& [1 I; N5 w. Z#include
#define USA 0
int main(void) 3 t0 @% J5 C5 j- a# \$ W0 S { struct COUNTRY country_info;
5 {9 P* C/ M! R1 ?, V5 ^country(USA, &country_info); 4 G4 f) \0 }/ `# q$ ~$ @0 ~" p" J0 _ printf("The currency symbol for the USA is: %s\n", . X1 d" T& Q/ b" b( K country_info.co_curr); return 0; ! s, [7 C0 X; S& Z- U } ) v; V' \. u/ e' o, ?8 i! j
$ D# W2 Z5 x6 C( R) r0 m函数名: cprintf 功 能: 送格式化输出至屏幕 % w! Q8 @. B4 Y7 y$ E( _* I+ U 用 法: int cprintf(const char *format[, argument, ...]); 程序例:
& E# F$ u/ K: V8 ?1 l+ @( J#include
int main(void) - C% n8 E/ Y# q' {3 p { . | \! k7 `; q, D5 I3 l /* clear the screen */ 5 |. N: j( i( T clrscr();
0 f; r8 X- |; M s/* create a text window */ window(10, 10, 80, 25);
/* output some text in the window */ # R" \% m6 U+ |- K9 q cprintf("Hello world\r\n");
# h* }) I* S. K/ V/* wait for a key */ # S4 w1 V3 k0 X1 Y, q1 D, ^: p# T- e getch(); return 0; % @- |/ w9 ?* V& M- {7 B }
. w4 ?8 y0 s4 [ V7 s函数名: cputs 功 能: 写字符到屏幕 8 P# r2 l2 s' q# g3 h& t. A+ e 用 法: void cputs(const char *string); 程序例:
8 e$ f0 _5 _7 k, }4 C#include
int main(void) { 5 C+ T# B0 q7 i* y/ J /* clear the screen */ 5 S- y" g1 o- {8 v5 |. f y& { clrscr();
/* create a text window */ % V; n- \( t- _" A9 ~) Q window(10, 10, 80, 25);
/* output some text in the window */ 0 j6 z* U \ r+ c: P5 Z* | cputs("This is within the window\r\n");
/* wait for a key */ getch(); # E: d% R& o/ U) l$ M return 0; } 7 a5 V3 `2 O) {; @4 ~& |# t, u' |) Y# ~
函数名: _creat creat 7 M, C+ G- {- Z! C. j 功 能: 创建一个新文件或重写一个已存在的文件 5 M R8 v4 O% A 用 法: int creat (const char *filename, int permiss); ) n" G6 o. H w# m$ v- Y+ o 程序例:
! X0 V2 {8 S5 q6 E2 r1 r#include
int main(void) / A K) @" k+ h2 j { int handle; $ V+ P# s2 l2 i char buf[11] = "0123456789";
& k8 S" G, v$ T3 g; X/* change the default file mode from text to binary */ % u' V. f& P$ ?: C k' M* P5 q8 r/ U _fmode = O_BINARY;
/* create a binary file for reading and writing */ % y( T/ T2 f" n9 G3 @6 r: [% j handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
/* write 10 bytes to the file */ ) D$ `/ y! a% A6 d3 T write(handle, buf, strlen(buf));
2 J6 g' Y% O/ U g: X/* close the file */ * A8 a6 f6 y9 s% T0 r# ]0 L7 v+ R close(handle); + [! X; X9 @& U% ?7 ]/ H3 K" N4 p return 0; * h/ z( [5 J0 x }
函数名: creatnew 功 能: 创建一个新文件 用 法: int creatnew(const char *filename, int attrib); 程序例:
#include
int main(void) { int handle; char buf[11] = "0123456789";
/ l; \& r/ |8 Q; x8 R/* attempt to create a file that doesn't already exist */ & @ s" _( F. I$ Q* g5 L4 k ~: a handle = creatnew("DUMMY.FIL", 0);
. y0 S0 U' h. D$ C5 M \$ `7 Y2 Yif (handle == -1) printf("DUMMY.FIL already exists.\n"); else $ w, T; M# f$ _+ j7 F { / F' ~# Q @ x- b7 V) a5 s) g8 W printf("DUMMY.FIL successfully created.\n"); 2 \, }4 Z% M$ _4 d; I; `/ j/ [ write(handle, buf, strlen(buf)); 1 F6 G- _5 A* f, f3 X close(handle); . Q& ?* Z0 B) N& f/ r6 N } 8 z# g/ S7 g) w1 ^! Y; a. ~ return 0; 8 O" m1 J; G" g& O9 i- M/ V) r }
; [$ a4 K# `' s9 y- M1 }函数名: creattemp / j: S0 ^$ q+ |3 d' ~ 功 能: 创建一个新文件或重写一个已存在的文件 用 法: int creattemp(const char *filename, int attrib); 程序例:
#include
int main(void) * Z6 P! D) B7 }) u8 c& ?$ m { , B* A1 C5 X4 ]2 S int handle; char pathname[128];
]9 d; N$ x% C m. [/ d8 l9 \strcpy(pathname, "\\");
/* create a unique file in the root directory */ 4 @3 P1 R+ i+ o" ~ handle = creattemp(pathname, 0);
printf("%s was the unique file created.\n", pathname); close(handle); return 0; } * I) J0 Y! I& l8 c; e$ ~ * u! x+ g1 k% e/ S7 x
5 f: K% @9 ^+ }& g$ @1 j* ? x函数名: cscanf 功 能: 从控制台执行格式化输入 用 法: int cscanf(char *format[,argument, ...]); 6 F+ N( y. q7 k( O% O' y# h 程序例:
#include
int main(void) { 0 {& t8 Z8 ~9 l2 `6 U2 V char string[80];
! V4 n- z. B/ J, Z4 d/* clear the screen */ clrscr();
0 K( i5 ?4 W6 l6 X" Z; k/* Prompt the user for input */ ; K4 \- i5 Y. U" } cprintf("Enter a string with no spaces:");
" F# T- j/ {" _/* read the input */ cscanf("%s", string);
& j: \, W, C7 s" w/* display what was read */ : t# u8 @+ _3 R; Z4 x& Z" l cprintf("\r\nThe string entered is: %s", string); return 0; + G0 H. A: X2 T! N } : S0 H. L" P( |: j. J
函数名: ctime 功 能: 把日期和时间转换为字符串 用 法: char *ctime(const time_t *time); 6 F" V9 E8 y% P 程序例:
, Y2 G! {- M& z& j#include
int main(void) { time_t t;
time(&t); ; L% u9 g* J7 S printf("Today's date and time: %s\n", ctime(&t)); $ Z9 s$ t1 Z2 P2 i. U* \ return 0; $ r( [5 F# L c1 U* ]! U } - t. x! R7 _ e, C4 y3 Y7 e 8 F$ e" E: [2 [: r# I. m
函数名: ctrlbrk , r4 [8 E* _8 T' S) g 功 能: 设置Ctrl-Break处理程序 % i0 M# i: V+ z& |. C- y# N; ?" T 用 法: void ctrlbrk(*fptr)(void); 程序例:
#include
#define ABORT 0
2 }3 W$ ?8 Y$ A5 j+ g2 mint c_break(void) & r; @: L% |$ q { printf("Control-Break pressed. Program aborting ...\n"); return (ABORT); }
) W" R( H4 O2 k$ S& S4 ?int main(void) . F* k8 H# ^. x1 ]8 D8 e9 r% C% F
{
ctrlbrk(c_break); 6 `6 g- ^# o9 j9 }
for(;;) 0 L" u! Y6 J2 ~! I Z
{ 9 m6 v* {; ]9 w
printf("Looping... Press
[em49]
谢谢啊
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |