函数大全(c开头)
函数名: cabs 4 _5 ]4 {# y+ Y5 j& B! Z& s. Z% G& T 功 能: 计算复数的绝对值 ! Y3 O8 m2 a2 Q6 t( [! _ 用 法: double cabs(struct complex z); `# c/ |1 g# I3 R0 U$ {' i N 程序例:
6 Z% Y8 W6 O1 Q E) a#include
int main(void) & y0 t2 h3 m1 U# P- ~5 O { struct complex z; double val;
z.x = 2.0; 5 ^8 B/ s/ J8 H7 K* i/ p z.y = 1.0; val = cabs(z);
9 N+ p6 w% r5 ~4 ?8 {/ R Y% \printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); - l8 W$ R3 a4 a) W* R9 \9 j% h return 0; 0 t) K& y% O- ~- c } ' c8 d0 E' |$ X0 ?5 D( [! J
8 h7 z- R/ Q) |( }函数名: calloc 6 a6 x; q: N1 O$ G) A 功 能: 分配主存储器 用 法: void *calloc(size_t nelem, size_t elsize); 程序例:
#include
int main(void) { char *str = NULL;
/* allocate memory for string */ ; F/ W* }5 q& [3 R& A4 C str = calloc(10, sizeof(char));
/* copy "Hello" into string */ : ~& e( D$ h* N$ U6 b0 R strcpy(str, "Hello");
* k! X/ @* M! }- U0 O! ~4 z/* display string */ 4 U5 {9 Q9 g; |% |* ]* W1 Y printf("String is %s\n", str);
/* free memory */ # M3 \: f, t9 z( ^* X free(str);
2 z' L$ }$ r! d! kreturn 0; } ! g2 f) T7 U, V* P* U' B
函数名: ceil * y& X- t) E9 C- k 功 能: 向上舍入 用 法: double ceil(double x); 程序例:
#include
int main(void) { J1 k# J& t3 C! } double number = 123.54; double down, up;
down = floor(number); up = ceil(number);
6 Q$ P% y* k% R! b! E/ {printf("original number %5.2lf\n", number); printf("number rounded down %5.2lf\n", down); printf("number rounded up %5.2lf\n", up);
return 0; }
函数名: cgets 功 能: 从控制台读字符串 # u, J: w: _4 v 用 法: char *cgets(char *str); # o! s2 _: `7 s 程序例:
`% e+ O4 ?# R K0 Y5 W#include
int main(void) { char buffer[83]; char *p;
/* There's space for 80 characters plus the NULL terminator */ 8 `8 |& M9 C6 [% J5 V( H buffer[0] = 81;
?% I0 f, c4 Hprintf("Input some chars:"); 3 ~. S9 X7 {: w$ W/ [ 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);
! X5 ]9 A7 S. V7 Q/* Leave room for 5 characters plus the NULL terminator */ : C: {. C$ K& P* \ buffer[0] = 6;
* N: s# d" L. c/ m7 Lprintf("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); 0 `& S7 d/ P+ F; b) V return 0; 7 X, w9 i" o8 A- d r6 I8 P7 ^ } ( D& }* U$ i5 p# P, s! D7 H 2 W' T8 e2 v4 V7 q9 E) Y
2 ], z/ n3 K ]函数名: chdir M9 B1 O; m+ l) H/ m D 功 能: 改变工作目录 , ]. k O+ E, a, y) p r 用 法: int chdir(const char *path); 程序例:
#include
char old_dir[MAXDIR]; + N' S" y9 @, H/ l char new_dir[MAXDIR];
5 h' W$ Z1 Y9 C, c+ l* Sint main(void) 4 n8 J5 t+ t( U3 g. a: y6 k { 9 s" t8 j/ E' m. W8 g9 R( G2 D if (getcurdir(0, old_dir)) * e, x4 K8 D8 w/ x+ l& L, \, C2 h { perror("getcurdir()"); exit(1); ?! G6 `% L0 {9 l' K: y M3 u O } , V! ~, T; ?1 q3 p4 q7 A printf("Current directory is: \\%s\n", old_dir);
' w8 h& q( _8 ?/ P- o6 g/ k( B5 \/ Wif (chdir("\\")) ! y n; G0 u* c) c! f% [) p { perror("chdir()"); exit(1); . Q" F1 Q8 ?. D0 ]& D: H' U* }6 s }
, W6 D5 Y/ d; n4 i5 cif (getcurdir(0, new_dir)) { perror("getcurdir()"); exit(1); } printf("Current directory is now: \\%s\n", new_dir);
printf("\nChanging back to orignal directory: \\%s\n", old_dir); 7 G* r: B$ L- a if (chdir(old_dir)) { & V* O; u( d7 C1 {8 t( n, f perror("chdir()"); - N7 D& \/ t4 ]; Q exit(1); 9 v' I& ~. w' Y; u. Z }
return 0; . K+ [, M4 }% B } 2 A6 s+ L" t9 Q5 [3 X0 o% F
函数名: _chmod, chmod + k% M4 q" r; q* h 功 能: 改变文件的访问方式 6 @ ^; s" |4 W# f4 n0 p 用 法: int chmod(const char *filename, int permiss); 程序例:
3 m6 X. G' ?0 d4 b#include
void make_read_only(char *filename);
int main(void) " [7 Q/ `# C) i6 } { make_read_only("NOTEXIST.FIL"); $ r4 w* F' a$ ] make_read_only("MYFILE.FIL"); R# v$ U% t7 P/ r# H+ u3 a return 0; }
+ ^. i4 f- f9 ?% l) M9 I' i* vvoid make_read_only(char *filename) - e3 x( T5 n, v& I/ _) y3 p# ~ { 7 y) G/ B5 H4 Y( U. T int stat;
stat = chmod(filename, S_IREAD); 2 o3 J* F, n6 |0 Q! [) P6 ` if (stat) printf("Couldn't make %s read-only\n", filename); else / I+ y2 ~/ M& I) y0 [0 _: v( \* s printf("Made %s read-only\n", filename); } . V7 g- `& F$ G `" W+ n # p. e4 R n# K$ }
函数名: chsize 5 [ E7 _4 C8 ]1 e/ E3 [* H$ k+ S 功 能: 改变文件大小 用 法: int chsize(int handle, long size); 程序例:
0 w0 h) r6 n3 x/ f#include
int main(void) " f2 q3 ?- r& Y6 V# h4 }/ ]6 L! }9 [$ q4 H { ! {+ V6 {% z8 r7 M int handle; char buf[11] = "0123456789";
4 i! m \' G7 ^# ]8 ]; }/* create text file containing 10 bytes */ handle = open("DUMMY.FIL", O_CREAT); 8 x+ \5 |" ]$ E, ^/ X$ n8 _ X write(handle, buf, strlen(buf));
! Z+ `; p! T1 t4 N/* truncate the file to 5 bytes in size */ chsize(handle, 5);
/* close the file */ 5 Y) w! P3 Z4 Y* U3 G* T- O+ ?6 B close(handle); return 0; } ( h( F F* _# I# m9 g: C
函数名: circle 功 能: 在给定半径以(x, y)为圆心画圆 用 法: void far circle(int x, int y, int radius); 7 w; v& q( O9 m7 ?$ U1 F9 ?) ? 程序例:
#include
int main(void) & @# g) {. F; H { $ [' a3 g; l7 l! w \! ^- ~ /* request auto detection */ : h4 E9 j( M' { o" |0 r3 u" y$ X int gdriver = DETECT, gmode, errorcode; int midx, midy; int radius = 100;
2 i5 T9 w% ~) S9 n- f' Z/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ ; j7 P( B; G# v6 \$ L errorcode = graphresult(); + b2 D/ k; d3 }2 B( H if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); ( V1 m& W1 v' i/ F- ~" { printf("Press any key to halt:"); getch(); 3 R& M: }% Q: Z2 h exit(1); /* terminate with an error code */ }
midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor());
8 G- I2 K' j7 }" ?+ C/* draw the circle */ circle(midx, midy, radius);
/* clean up */ getch(); closegraph(); return 0; } ' D1 M9 M9 d0 p \- H
函数名: cleardevice 功 能: 清除图形屏幕 用 法: void far cleardevice(void); + u9 `- w, ?' k7 m& j 程序例:
% H6 F( G+ t! _* {- Q! ^#include
int main(void) { /* request auto detection */ 1 j& l) y6 Q# ^" i) g. i0 D" k6 M* N int gdriver = DETECT, gmode, errorcode; 7 C% x. ~' B8 X+ z1 B, g- t& @0 c int midx, midy;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ * n; L0 r& `" W. b; p3 o errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ - h! W9 S9 {' q3 \$ u1 e* [ { 2 O) Q2 f' u4 ], D4 X printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ }
midx = getmaxx() / 2; ! r1 x' i6 F) P* k0 n { midy = getmaxy() / 2; 3 E' q5 d, s- G/ X, n' ~% o8 M! z8 C setcolor(getmaxcolor());
/* for centering screen messages */ settextjustify(CENTER_TEXT, CENTER_TEXT);
/* output a message to the screen */ % a. W0 `8 X* G; I" _/ [. y. J outtextxy(midx, midy, "press any key to clear the screen:");
/* wait for a key */ 0 h6 s0 Z) S& ~! ^. U getch();
8 a% y; h0 c0 s4 k( E- g: T/* clear the screen */ 3 c# X" a7 h" I9 F7 O9 L cleardevice();
/* output another message */ outtextxy(midx, midy, "press any key to quit:");
% |$ C; X* B H' b5 H$ r- u/ |/* clean up */ + Z: q( I# r/ F getch(); 2 Z$ ?2 J$ o. b/ {4 H) v closegraph(); return 0; + r6 e2 {" A6 e6 S } " I1 Z' k) q9 g2 y
: H) m; X. A5 J4 b l7 [函数名: clearerr 功 能: 复位错误标志 - V: z0 {0 L3 r9 g+ D1 l+ O 用 法:void clearerr(FILE *stream); ) A% [. x z) j, ?, d+ l 程序例:
+ ~ D: @) i; u+ A8 h% s4 _& W# R#include
int main(void) { % A+ |/ V0 c1 s7 ?$ H FILE *fp; char ch;
/* open a file for writing */ / M. H: e9 e0 ?) d- J fp = fopen("DUMMY.FIL", "w");
/* force an error condition by attempting to read */ ch = fgetc(fp); printf("%c\n",ch);
& `9 o3 ]% h5 i0 kif (ferror(fp)) 6 [$ e' i! k2 ? { 7 D4 J) P. y6 u) Y1 h /* display an error message */ printf("Error reading from DUMMY.FIL\n");
/* reset the error and EOF indicators */ clearerr(fp); }
% [# v1 e" k2 v0 S" y, U* ~+ Afclose(fp); return 0; + r E: f3 {' [; `' W9 g: C6 ^" ] } * t! V& R* `4 } o0 {7 k8 F/ D) Z2 C6 Y
, e6 d$ Q: Y2 j& m函数名: clearviewport 3 S6 D/ V0 {. j/ Q 功 能: 清除图形视区 用 法: void far clearviewport(void); 程序例:
#include
#define CLIP_ON 1 /* activates clipping in viewport */
int main(void) % ]7 B7 h5 e5 p3 t Y3 f7 x { ! K- c7 M/ ]! W. A. \ /* request auto detection */ 3 z" b3 E, M: S- I, {& D int gdriver = DETECT, gmode, errorcode; 1 e/ t6 W2 Q( K( `5 A int ht;
( ?* E& O9 r- W$ \' l1 P! K/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
0 } V* `- v2 b8 X) C/* read result of initialization */ 4 q& {) }' I; K. H: h1 W7 m errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { 5 U" Q9 O1 b4 q0 B5 X+ ^( w4 F printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ 6 r3 E8 J# f5 ~% m+ v" Q }
8 m* _) S, f2 d' G8 Y/ ^" U3 Wsetcolor(getmaxcolor()); , r. o$ Q: G3 q: m ht = textheight("W");
/* message in default full-screen viewport */ . }; {% f$ X8 @7 ~% L& T outtextxy(0, 0, "* <-- (0, 0) in default viewport");
/* create a smaller viewport */ . C8 l6 r/ x6 U1 z3 x( ` setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
& q9 }: J7 j% W* o5 T1 T/* display some messages */ outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); + J# h% m2 V' b+ o2 E' R4 | outtextxy(0, 2*ht, "Press any key to clear viewport:");
/* wait for a key */ ) t! X1 m# D( Z8 N. ], i getch();
7 l5 @. l$ \) q4 W6 l: H) D/* clear the viewport */ ( ^+ M) k7 r' e: E clearviewport();
, h% u: k J. @! D! V( T% Z* u/* output another message */ outtextxy(0, 0, "Press any key to quit:");
/* clean up */ getch(); 0 d$ H# y5 _/ P# h4 |& I closegraph(); return 0; } ) r% r/ D! p8 {
函数名: _close, close 功 能: 关闭文件句柄 用 法: int close(int handle); * v! i5 r5 f! R5 H( A 程序例:
#include
main() $ C; o$ B8 I) R7 n; \. M { int handle; 5 ]0 M( Y$ r# _: Y! B+ f char buf[11] = "0123456789";
, |, h0 B4 \5 J/* create a file containing 10 bytes */ handle = open("NEW.FIL", O_CREAT); if (handle > -1) { write(handle, buf, strlen(buf));
/* close the file */ close(handle); } : |6 F3 [ W. z3 ` else { printf("Error opening file\n"); } return 0; + c" u& v, `% ]9 X' y } 5 ?7 i4 G& i4 N$ q! J$ D u$ S + o8 X }: T' u# ]. @
函数名: clock 功 能: 确定处理器时间 用 法: clock_t clock(void); 程序例:
& D' z* ?. r. o7 N; c#include
int main(void) { clock_t start, end; start = clock();
delay(2000);
end = clock(); printf("The time was: %f\n", (end - start) / CLK_TCK);
2 v" b/ Z/ F/ hreturn 0; : D& \1 k, Z( z* m4 ? } $ G! C/ Y8 v5 Q/ M
9 z' e& M+ B9 q0 W函数名: closegraph 4 l" p* m4 F4 N 功 能: 关闭图形系统 ( k- K( m1 n7 q z 用 法: void far closegraph(void); 程序例:
( d7 u' H- ~1 q2 S/ i+ _#include
int main(void) : `3 i$ q& k. t& t# c4 [# f { 0 ~% M- m) _: j* a; G/ s /* request auto detection */ int gdriver = DETECT, gmode, errorcode; 4 Z7 H8 U' c/ j- T4 X+ ~ int x, y;
/* initialize graphics mode */ 9 @* a& H& C4 ]" M( {+ m1 g8 ?& @ initgraph(&gdriver, &gmode, "");
% f8 i7 X) v1 q: W# J! l/* read result of initialization */ 3 k+ I$ `0 E# L, b errorcode = graphresult();
* w9 P1 C6 J3 b$ eif (errorcode != grOk) /* an error occurred */ { . q. D1 D3 v2 w! G# z; g printf("Graphics error: %s\n", grapherrormsg(errorcode)); 3 O* z8 l, s; W( c/ I U5 Q0 N printf("Press any key to halt:"); getch(); & L0 X& r: R& d+ Y7 a2 F+ Y exit(1); /* terminate with an error code */ 7 J! l: Q0 O. q. {# j8 H. x" ~ }
; Q: n/ a5 h# b. e1 }1 _ K' ?x = getmaxx() / 2; y = getmaxy() / 2;
: R8 O$ ]$ ]9 u1 c* u/* output a message */ 2 v; U7 W8 M0 c# F+ Q/ g) \ settextjustify(CENTER_TEXT, CENTER_TEXT); 4 A. j( Y0 {. Y* ]+ z outtextxy(x, y, "Press a key to close the graphics system:");
/* wait for a key */ ) ?2 z7 d6 R. A1 m+ j# i getch();
# J% s6 ~+ E( \7 D) m- h/* closes down the graphics system */ closegraph();
9 w) J/ S" D) [printf("We're now back in text mode.\n"); . ?# T, y! G$ c/ w printf("Press any key to halt:"); % X% S. Z& A8 |& l9 i getch(); return 0; }
6 l" b- z/ J' Y% f函数名: clreol 功 能: 在文本窗口中清除字符到行末 . K" H3 @; U5 j- r2 I 用 法: void clreol(void); 程序例:
#include
int main(void)
{ clrscr(); cprintf("The function CLREOL clears all characters from the\r\n"); , G& J* E2 e+ F3 ~, C5 X cprintf("cursor position to the end of the line within the\r\n"); ; W6 U! v, S- H( I4 @8 D' W cprintf("current text window, without moving the cursor.\r\n"); ! j) |" n( q1 @2 ~/ c5 P* Z cprintf("Press any key to continue . . ."); 8 ?8 @2 g& S& D# z' z1 a6 _4 H+ c gotoxy(14, 4); ( L2 M' D$ }! \" X; \ getch();
# x) o; y; E) f. e( a5 J% Z2 Dclreol(); getch();
. F) k' Q4 w1 J" k' sreturn 0; }
函数名: clrscr ( `. `; z8 H, Z/ Y0 q7 e 功 能: 清除文本模式窗口 用 法: void clrscr(void); 4 {7 ~9 Y4 l* k 程序例:
#include
int main(void) { % x% d. h+ L# q2 d6 D/ C int i;
& O6 g/ }1 r D$ {; X, {clrscr(); for (i = 0; i < 20; i++) cprintf("%d\r\n", i); ( z: R" E/ v, p+ R) N3 g cprintf("\r\nPress any key to clear screen"); getch();
0 |, U. c% ^) @* G! Z. R" w& Z' J& eclrscr(); " j' G/ w. b3 v, Q5 | cprintf("The screen has been cleared!"); getch();
return 0; & `4 u% Z- h% J, V* l } 7 h! w* U) G- m, M9 [ * y5 L: Z) t2 ~. i9 W/ |5 I
5 L, k" N+ Y1 G/ E& {" E函数名: coreleft # ^: @1 M8 Z; i. v0 o7 ? 功 能: 返回未使用内存的大小 . D$ [* a/ O3 G. j3 s 用 法: unsigned coreleft(void); 程序例:
#include
int main(void) . A7 m, h3 B. }7 S { 3 X* i! I. L, I# @ printf("The difference between the highest allocated block and\n"); printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
return 0; }
+ _3 M# O7 z9 B" Q% f+ t函数名: cos 0 D# _. a. j8 W6 d2 a 功 能: 余弦函数 - |; J' n; `! v" T) q2 ~ 用 法: double cos(double x); 9 n; Z9 H( U# u. G 程序例:
#include
int main(void) { 2 p3 W( ^' s" t b5 ?% u( @4 | double result; & q1 b9 o) ~( k0 {* b double x = 0.5;
% v! u @ M& k- z( A6 \3 f; Iresult = cos(x); printf("The cosine of %lf is %lf\n", x, result); # i" q2 C$ a) u" N return 0; 4 a+ l& B P4 t' X" I- e } 7 b+ J6 L. v( ~1 p4 X( V! i3 R
5 S4 D3 l/ N- t6 u% @6 w$ s+ F }; k函数名: cosh % L# E$ @' b! K0 T B2 r J 功 能: 双曲余弦函数 用 法: dluble cosh(double x); , q2 R% [1 G: J; K- V2 j 程序例:
#include
int main(void) { double result; 9 q4 r! _- @8 c& ^7 e1 m; r double x = 0.5;
result = cosh(x); 4 Z+ s% C. g7 _ printf("The hyperboic cosine of %lf is %lf\n", x, result); return 0; 3 n+ h8 g+ |9 ~+ x: _ }
3 r5 s" l5 x6 p @ [- {& d8 r函数名: country 功 能: 返回与国家有关的信息 - D: B) s! m8 }& J" @/ V( k 用 法: struct COUNTRY *country(int countrycode, struct country *country); - ?( j2 j9 t, i- g1 K) C' P- u9 E 程序例:
#include
#define USA 0
int main(void) { struct COUNTRY country_info;
% F( g2 w( j9 \+ p3 u7 h% qcountry(USA, &country_info); printf("The currency symbol for the USA is: %s\n", country_info.co_curr); return 0; }
$ l0 p' D0 _/ E& ^! T函数名: cprintf ; H5 ^: D3 R" q3 A0 `: E! @% P) m2 I' q5 n 功 能: 送格式化输出至屏幕 $ C7 m- h u) f: D+ w8 X 用 法: int cprintf(const char *format[, argument, ...]); 程序例:
#include
int main(void) ) [. u C3 T; a0 \$ y { 9 F$ O+ m7 F9 X /* clear the screen */ clrscr();
/* create a text window */ $ c" M) v; s4 X1 n* Q' i window(10, 10, 80, 25);
' f- o% w) V) E- c! H/* output some text in the window */ A7 r* O9 Z6 g: W% w$ F cprintf("Hello world\r\n");
! O6 c& I K" V/* wait for a key */ getch(); 1 O6 W+ N" c Y$ J4 X return 0; # Z- ?6 o- K6 C2 z2 u } ' r/ [7 L' c6 v9 W* T @) r
函数名: cputs 功 能: 写字符到屏幕 ) O) J9 v5 Q3 d: L& C$ x5 W( Z+ ^ 用 法: void cputs(const char *string); k( I3 L( q, S7 f 程序例:
#include
int main(void) { ( T* C) I4 H, ~. i /* clear the screen */ $ h( h! Q' s. I4 j clrscr();
/* create a text window */ . V' j" C9 [0 R5 q x3 L window(10, 10, 80, 25);
; R4 j6 }4 j3 Y5 J/* output some text in the window */ cputs("This is within the window\r\n");
/* wait for a key */ " u- |/ x2 `5 X7 k. l getch(); return 0; } $ l# h0 ~$ @' x) g0 `
函数名: _creat creat # P/ I3 r0 `2 A, G% L8 w6 o3 | 功 能: 创建一个新文件或重写一个已存在的文件 用 法: int creat (const char *filename, int permiss); 程序例:
- O! s- d; R/ N* V6 x#include
int main(void) { 0 ?) V- l0 K0 O7 y: ~* e int handle; char buf[11] = "0123456789";
/* change the default file mode from text to binary */ _fmode = O_BINARY;
/* create a binary file for reading and writing */ # R4 \( g! K* H7 Z4 m2 O" Y handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
0 X0 F4 ]5 ?1 t/ }/* write 10 bytes to the file */ write(handle, buf, strlen(buf));
/* close the file */ { J0 a1 u6 B" r4 M7 r close(handle); return 0; }
$ M4 O* I, C4 y0 \* N# n函数名: creatnew & t4 c/ W9 [6 D( G7 d. T 功 能: 创建一个新文件 用 法: int creatnew(const char *filename, int attrib); 程序例:
0 v) a, E9 N: z7 ?" `6 C8 u#include
int main(void) ) x: ~9 z6 Q# q$ k# u. F$ T { 7 u: p5 f7 s1 M" p# y. W: B" w int handle; char buf[11] = "0123456789";
/* attempt to create a file that doesn't already exist */ handle = creatnew("DUMMY.FIL", 0);
& M, W4 e* i- x/ s* Cif (handle == -1) printf("DUMMY.FIL already exists.\n"); else , l, v; f4 ^- ~/ p2 N! Z { printf("DUMMY.FIL successfully created.\n"); 4 d' `+ C0 b" K' H) K+ F( E4 B write(handle, buf, strlen(buf)); close(handle); } return 0; " w1 x* B& G# O$ H& J: y1 M! X4 W } $ G1 O! l, I/ v # u8 x0 |" y$ t% p6 F/ m* _" ~" L
函数名: creattemp 功 能: 创建一个新文件或重写一个已存在的文件 $ h W. H4 S% l) ^( q) m- Q 用 法: int creattemp(const char *filename, int attrib); , w+ p. u6 M1 v% u 程序例:
#include
int main(void) { int handle; 4 `1 L. K: Q1 p5 @3 s! q char pathname[128];
0 O. S+ r0 w: V: d# f( h" Ustrcpy(pathname, "\\");
1 ^- i" y/ Q2 u+ T8 I/* create a unique file in the root directory */ 6 [, C4 Z4 W6 P% _' v% T3 Y1 T handle = creattemp(pathname, 0);
printf("%s was the unique file created.\n", pathname); close(handle); return 0; ) ~ _1 ^$ e9 ~2 _3 Q# d) D/ f } ! ~2 @! W* G9 p# X/ R* n8 N ; V7 h- `: X( V; a
% D/ m, b- w0 C: K函数名: cscanf - p, K2 j/ |# g1 z# G7 F 功 能: 从控制台执行格式化输入 用 法: int cscanf(char *format[,argument, ...]); 2 i# y! \8 f+ u( w( _$ V 程序例:
; K; e/ x# J( K# k* P3 {#include
int main(void) { char string[80];
/* clear the screen */ 8 [6 v+ `: \6 w9 W" m& W clrscr();
0 b0 s4 Z! s3 D- y/* Prompt the user for input */ cprintf("Enter a string with no spaces:");
& D( \, e$ ~8 L/* read the input */ 3 P/ }) a, h4 {9 o! V; X" Z' n, q1 @ cscanf("%s", string);
; D7 k7 h0 _; n" R; Z% s7 Y/* display what was read */ cprintf("\r\nThe string entered is: %s", string); return 0; } ! T% }4 |* u ?) m7 L m
函数名: ctime 功 能: 把日期和时间转换为字符串 用 法: char *ctime(const time_t *time); 程序例:
# U9 {7 I" q: v3 O#include
int main(void) { ! B: \3 v: E( `, N( ]7 N" ^9 D: S time_t t;
P5 J8 r- S0 u* n/ l+ N) L2 c) otime(&t); printf("Today's date and time: %s\n", ctime(&t)); return 0; } ( F. J9 f; T# a/ d . r4 A a; P3 {' h- d7 S
0 c7 w1 L4 n7 x" u! h) w2 {% t: i函数名: ctrlbrk 功 能: 设置Ctrl-Break处理程序 用 法: void ctrlbrk(*fptr)(void); 程序例:
#include
#define ABORT 0
int c_break(void) # B0 N* V( s5 x t' x5 A' u { 3 l9 Y5 ~! N: l9 A; p printf("Control-Break pressed. Program aborting ...\n"); return (ABORT); }
& Y" S! {% ]. ~: s! T, E3 n& B6 ~int main(void) " B! ^( p1 I5 i+ \
{ ' Y+ y4 E* `8 d
ctrlbrk(c_break);
for(;;) 1 z. V2 s- I# a/ \
{ . z; I4 d% m) F0 \8 X
printf("Looping... Press
[em49]
谢谢啊
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |