函数大全(c开头)
函数名: cabs 功 能: 计算复数的绝对值 ( }9 f% f' i. L+ V; F3 Z) y) w7 | 用 法: double cabs(struct complex z); / e2 ]+ d: T; b2 p0 e 程序例:
1 A' I. D& y. e2 S: b( T#include
int main(void) 5 R7 b7 f4 F: f { ; n8 D* f, N9 B$ U1 h# R5 Y8 a6 L struct complex z; ! \3 Y! s* Q8 \- D0 \+ d M' X double val;
j- [3 H! \4 [4 @# L C Zz.x = 2.0; z.y = 1.0; 9 X. Y7 ^, M( i: X val = cabs(z);
9 i8 v6 B6 x1 V# F1 d3 aprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); 4 a' j# f/ t1 c2 c% U4 L return 0; & a7 t- f+ [# e* {7 ?( ] } ! [( B n/ H( Y7 |! s( j' E% C
2 B6 y: u0 N4 X) V8 Q! i: A/ u函数名: calloc 功 能: 分配主存储器 用 法: void *calloc(size_t nelem, size_t elsize); 程序例:
#include
int main(void) { / i; U( ?" E7 y' | char *str = NULL;
/* allocate memory for string */ $ f0 q3 P6 U' {" T2 c3 m3 c str = calloc(10, sizeof(char));
9 p# }; X+ s$ U; r' i$ ?1 p/* copy "Hello" into string */ 4 Z: f/ a8 L1 I" t' R- ^+ o8 ` strcpy(str, "Hello");
% v# M; I- Q: n: d$ r/* display string */ printf("String is %s\n", str);
/ ^6 Q. H6 i# u5 g* ?( ^' d+ S/* free memory */ + m; ~+ m8 {: n' m$ x. M, c& ]) t free(str);
return 0; $ p' d) a8 p. R3 ?, B7 J# ~7 ^! { } $ n* j8 Y8 D& P+ b4 Z; k
函数名: ceil 功 能: 向上舍入 用 法: double ceil(double x); 6 a0 s1 N/ p+ M1 c% p6 Y" m 程序例:
( s/ Z# O) g, v#include
int main(void) 8 V2 T8 Z/ i" m/ ? { * O0 F# _1 s: f5 ]0 {" B& U double number = 123.54; double down, up;
* R: |5 Y6 q* I! Wdown = floor(number); up = ceil(number);
" w0 x: p# H) T0 y& \. |, O- ]printf("original number %5.2lf\n", number); * X# C5 [& b, p/ k printf("number rounded down %5.2lf\n", down); ' P3 K- k2 |7 p9 ?. q printf("number rounded up %5.2lf\n", up);
return 0; } ( C: a$ x, X, B. A- N# B . X8 W" f2 S; y- j7 \" h
+ E% o6 x' ~- q1 o函数名: cgets , ~% c r) o8 ], U- ^ 功 能: 从控制台读字符串 用 法: char *cgets(char *str); 8 l J/ b8 b. l 程序例:
#include
int main(void) { ' z/ j& ?+ ?3 S i& z- n char buffer[83]; char *p;
3 w$ T/ @% o, B" X, o/* There's space for 80 characters plus the NULL terminator */ ! Y+ X5 ~8 ^1 Y% ]! C2 h buffer[0] = 81;
printf("Input some chars:"); p = cgets(buffer); printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); $ @' l7 I8 f0 K- f( l! d: _ printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
4 V+ z- W, l) R6 F- m7 G! h/* Leave room for 5 characters plus the NULL terminator */ buffer[0] = 6;
+ v: u' g; B/ g7 kprintf("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); return 0; } 9 H& W& r) n$ l! M, k
函数名: chdir 功 能: 改变工作目录 + a' w0 z7 `# l5 _ 用 法: int chdir(const char *path); , m. P" [2 c7 a 程序例:
6 h* c% k7 d) @* ]2 w" X( P#include
char old_dir[MAXDIR]; char new_dir[MAXDIR];
int main(void) 0 x" S. S( v' X5 H( W) \ { if (getcurdir(0, old_dir)) & Z7 [( b3 O0 G% B { perror("getcurdir()"); $ i) U* H {7 `' j6 n6 o exit(1); & D& k' z- o: ^ w- `1 x; g } printf("Current directory is: \\%s\n", old_dir);
$ e# X4 R7 T/ w4 f6 x6 Zif (chdir("\\")) $ j! q9 d/ \5 F4 M `! f6 M' v' ` { + C# j, t9 w6 ~ perror("chdir()"); , O, z# b8 }0 ?/ q+ @ exit(1); * N0 ]: c* C* x: Q5 ?( Q( z3 K }
E. F9 J; m7 G( F. q- oif (getcurdir(0, new_dir)) % P* w+ e8 \: x( ` { 4 q1 {2 Y% w7 L1 z4 u- F* r% A perror("getcurdir()"); exit(1); % T) K1 U& V$ F- e- y& X% R; N } printf("Current directory is now: \\%s\n", new_dir);
printf("\nChanging back to orignal directory: \\%s\n", old_dir); ' C) \8 k$ A& ^5 R. O7 ] if (chdir(old_dir)) { perror("chdir()"); - ^0 _/ d7 s& I exit(1); }
$ K# c. b# _/ g& K9 zreturn 0; } . Y1 g1 ?9 U' G& ]- ?! O ; d4 o9 y# s2 a) H$ X
函数名: _chmod, chmod 功 能: 改变文件的访问方式 + ^! \2 k i' A; ?0 M" d4 Y 用 法: int chmod(const char *filename, int permiss); 程序例:
8 \7 @$ I2 R8 S; x$ E1 W \#include
void make_read_only(char *filename);
' a% s9 P% s8 {+ i- kint main(void) { J% p# B* |9 B% L2 o" ~6 O make_read_only("NOTEXIST.FIL"); . Y7 o+ B6 U& b3 j5 f# }, z% w/ j" Q% P make_read_only("MYFILE.FIL"); return 0; ; n+ `2 ?" m1 E }
% O7 _2 |9 n. Zvoid make_read_only(char *filename) ; j8 }/ e r/ N0 A8 I' W: e { int stat;
stat = chmod(filename, S_IREAD); % j. @ [5 s' \% T$ A' b0 K7 k f if (stat) 1 z5 p7 h+ W: H% e; P/ e* p printf("Couldn't make %s read-only\n", filename); else printf("Made %s read-only\n", filename); } # @2 N0 X: C2 |( z* C9 Q& g8 [
函数名: chsize * Z, G. _ B1 H& _# E 功 能: 改变文件大小 5 D& P$ {& E8 @6 T% @ 用 法: int chsize(int handle, long size); 程序例:
#include
int main(void) { int handle; ! y' n6 n- ~, G; v char buf[11] = "0123456789";
/* create text file containing 10 bytes */ handle = open("DUMMY.FIL", O_CREAT); 9 ^) T% L9 R1 P2 f; w: j8 ?( K write(handle, buf, strlen(buf));
, ]/ q b2 w4 Z5 Y; u Y- c2 b/* truncate the file to 5 bytes in size */ chsize(handle, 5);
/* close the file */ close(handle); return 0; : K" z8 q/ g X0 @3 v9 A } $ s8 z% h1 u' p* i$ r% V8 V3 { ( A, t* K1 J& x" `! }! g3 l
函数名: circle 功 能: 在给定半径以(x, y)为圆心画圆 & }3 ~8 d: _+ s 用 法: void far circle(int x, int y, int radius); 1 v2 W* `. b( d1 N, C+ Q$ s2 J 程序例:
6 N, ~( f& @) F' e z3 {, G& A8 _& n#include
int main(void) { ' u8 a1 T1 H5 h: \ /* request auto detection */ int gdriver = DETECT, gmode, errorcode; ' F! c9 b: h* U! Q int midx, midy; int radius = 100;
, G# ~7 v& g( B% _$ z" `6 Q+ I/* initialize graphics and local variables */ ! c2 H, b g& P5 V& _7 y/ y0 S initgraph(&gdriver, &gmode, "");
3 a9 I2 z' b4 r5 c/* read result of initialization */ errorcode = graphresult(); , Q4 x$ A; H- K' Y% X: a if (errorcode != grOk) /* an error occurred */ { * |0 } d( X* f4 } printf("Graphics error: %s\n", grapherrormsg(errorcode)); " T8 \# ~6 Y5 ~0 Q4 p printf("Press any key to halt:"); # _$ b5 t: T0 E, q* q% J getch(); exit(1); /* terminate with an error code */ ' g: V4 {# n: a7 _ }
. w2 H. q6 O- n- cmidx = getmaxx() / 2; 5 Z, Z6 p9 o8 B1 m# W L midy = getmaxy() / 2; 4 \( U$ |+ ?! X setcolor(getmaxcolor());
/* draw the circle */ circle(midx, midy, radius);
* Q& I( `4 q& ^" P1 E9 k5 Y/* clean up */ ; x" K# J3 a/ ~6 A9 ~' o5 U getch(); closegraph(); return 0; 5 D& n; V) Y; N; g4 q e } " k& @9 X% Q! z% }; y l. ^/ L L7 n _0 t7 V2 i4 } ; m$ p4 ?% @! w( X6 ?
* `' M& j! t0 m函数名: cleardevice , {# s; u8 y+ j 功 能: 清除图形屏幕 用 法: void far cleardevice(void); 程序例:
#include
int main(void) ) V* z; h! w' k; |( [4 j { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; 7 X( S4 ?; L/ w, m! C2 F int midx, midy;
7 \/ f- D2 _' @( t, j/* initialize graphics and local variables */ 2 Q1 e/ G$ \% C: T6 E+ Y/ s7 j initgraph(&gdriver, &gmode, "");
1 O1 Q4 C2 t; \6 W3 [6 @/* read result of initialization */ 9 y+ O9 M9 i0 J errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { ~2 g$ \7 e/ ~5 z! ] printf("Graphics error: %s\n", grapherrormsg(errorcode)); - m) d1 a% i# n. ^4 L printf("Press any key to halt:"); getch(); ' p8 e9 u$ W9 m2 q3 R% v: ? exit(1); /* terminate with an error code */ ( Q0 K6 X W G/ `0 l: R9 b }
midx = getmaxx() / 2; P/ y( i5 ?* V) g$ R# q3 y midy = getmaxy() / 2; setcolor(getmaxcolor());
/* for centering screen messages */ settextjustify(CENTER_TEXT, CENTER_TEXT);
( \, X; m2 Z e6 V/* output a message to the screen */ , i: c8 _0 M# x. _& ` outtextxy(midx, midy, "press any key to clear the screen:");
$ F) l; `+ ~& x! Q) L! u* B1 T/* wait for a key */ 2 e$ A% ~9 G# b. [" A e getch();
/ k# y/ w2 @0 P3 k' n" g9 x/* clear the screen */ 2 a, D) w3 B: w6 X" | cleardevice();
, U6 c. c& k4 p9 o# L7 ~/* output another message */ outtextxy(midx, midy, "press any key to quit:");
, V- x1 d* S4 n# I! Y+ V, x/* clean up */ : z" U4 F, `% V' ^% t9 u. b N getch(); $ a' x' C9 _# _. R closegraph(); return 0; } 8 ]9 B0 m2 A: E/ ?0 F " f W! {+ l* y0 X1 j' l1 T 3 j+ R, G" B M( G- z
函数名: clearerr ( i! `" g; u3 I% v& T3 f 功 能: 复位错误标志 ! j' e. }* T$ |' U; F, K( } 用 法:void clearerr(FILE *stream); 程序例:
#include
int main(void) { FILE *fp; char ch;
, m6 o: V/ J' a) D1 w/* open a file for writing */ & Y# C- j; ^9 D. N fp = fopen("DUMMY.FIL", "w");
* u, p& B* ` F" l. c0 D/* force an error condition by attempting to read */ H, {( Z; z7 [. [) u ch = fgetc(fp); printf("%c\n",ch);
if (ferror(fp)) { $ h1 J# C7 e+ e t7 ]4 T3 Y /* display an error message */ printf("Error reading from DUMMY.FIL\n");
/* reset the error and EOF indicators */ clearerr(fp); }
fclose(fp); ) w* y. \9 Z ^1 a2 E) K8 u return 0; 3 V. G. r# L; f } 3 B! S& c' A. ^% S0 _' m
4 q: J" H( R, w+ s5 N2 l. c函数名: clearviewport : W, F: b$ ^: L& n) Z0 R1 | 功 能: 清除图形视区 用 法: void far clearviewport(void); 程序例:
% E/ G) a+ T; v; F#include
#define CLIP_ON 1 /* activates clipping in viewport */
int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; 2 N" E+ x" Q: D! o int ht;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
/* read result of initialization */ " u, z3 K, @( F( q" | errorcode = graphresult(); 2 S+ v6 f% {- ^& @5 Z) b if (errorcode != grOk) /* an error occurred */ { . C+ c; ]* v! X' X1 a5 | printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); 6 s. f s( C) g z5 o$ G getch(); exit(1); /* terminate with an error code */ }
: l; l" L" l8 E, x, X' \setcolor(getmaxcolor()); ht = textheight("W");
/* message in default full-screen viewport */ 0 m. L9 z5 ]' g3 I0 D outtextxy(0, 0, "* <-- (0, 0) in default viewport");
, L" R4 C$ L0 M" l& D6 t& n2 G/* create a smaller viewport */ ' d3 k6 g! g$ q; | W setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
/* display some messages */ outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); outtextxy(0, 2*ht, "Press any key to clear viewport:");
; y. w3 E$ U; } d# J# I* Y/* wait for a key */ getch();
% x5 g# B: S9 I1 v U$ G s% f' ^4 N* V/* clear the viewport */ 9 M% @; w; h2 r {. a$ @ R clearviewport();
8 G) l) ?1 ~5 A. p# k4 a4 p5 B/* output another message */ outtextxy(0, 0, "Press any key to quit:");
) V& d& O# j3 G+ k% x/* clean up */ getch(); % z' A' O6 T9 N* A# T+ Y( m% v closegraph(); 5 a$ z0 w0 p1 W; G' f) Z return 0; 9 N/ u4 k) w5 ? } # Q4 F/ B2 i" j" N' h- q% S2 e ) n8 i( m# ^- N" Y* z) l! k) V, x; q
函数名: _close, close 功 能: 关闭文件句柄 : z, o2 e1 L6 t5 Z# ^! ?/ z 用 法: int close(int handle); 程序例:
#include
main() { int handle; ' i/ [! ?, V, k1 } char buf[11] = "0123456789";
( d! t6 b k) U7 M) d/* create a file containing 10 bytes */ handle = open("NEW.FIL", O_CREAT); . G, ^: k9 v+ w if (handle > -1) ! A7 j; v. L4 g/ {* X { write(handle, buf, strlen(buf));
, G X' I% D( N. O# P' L ~* t/* close the file */ close(handle); 9 O( T, I1 L( E } + i( |$ e( z3 n else / }& I; s; V1 D% r% ~9 e; z { - c: V5 t! z' I5 p printf("Error opening file\n"); } + n8 y7 P3 g/ A7 |) `0 E% l return 0; } 3 }( P) [! _6 p5 {4 h( D& S
- f, R0 J" h$ H9 X函数名: clock 功 能: 确定处理器时间 : ~3 S% `* a- W( F: Z6 B 用 法: clock_t clock(void); 程序例:
) F) X! q; u1 H0 g T% r#include
int main(void) * y3 r, |% D- f { clock_t start, end; ( g* t) X* `: ~ start = clock();
+ d1 O" M/ N$ z, ?# P! r8 q1 B1 pdelay(2000);
end = clock(); $ \9 B" c. |( B( i; n: p- m- ~0 Z printf("The time was: %f\n", (end - start) / CLK_TCK);
0 r0 h9 z. a: o7 {" areturn 0; } " K7 v i- j7 ?$ S' M" d 6 V8 m, a& D. O: a 4 e6 { U1 G9 x, @7 j6 h
函数名: closegraph 功 能: 关闭图形系统 用 法: void far closegraph(void); 7 o! d. p5 m# ~+ S: S7 r+ x 程序例:
#include
int main(void) 9 O' Z. M# i# \5 l3 g% p% Z { : |2 v$ S. ~7 x4 M1 V /* request auto detection */ int gdriver = DETECT, gmode, errorcode; ( m* p8 s; P1 S6 z/ M2 x$ j K int x, y;
9 w9 w0 P2 Z& E" D$ z1 H/* initialize graphics mode */ initgraph(&gdriver, &gmode, "");
: Q+ c1 ^# b; B; G8 D4 V% c2 _/* read result of initialization */ errorcode = graphresult();
8 h2 ~7 k0 \2 J& Oif (errorcode != grOk) /* an error $ W8 B! _) _4 R$ A# D& y occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); $ D/ c/ B! \6 g5 ^* h6 `0 N5 c printf("Press any key to halt:"); getch(); ) ^3 A( \- H: C$ v2 v3 }' n exit(1); /* terminate with an error code */ }
x = getmaxx() / 2; y = getmaxy() / 2;
+ K( ^# o$ v$ F7 B5 ]5 o+ N/* output a message */ settextjustify(CENTER_TEXT, CENTER_TEXT); $ a6 J7 ?7 D9 L" M5 \5 m& I5 f outtextxy(x, y, "Press a key to close the graphics system:");
, d* t. ], z2 ~9 P4 D% K+ y; ^/* wait for a key */ ! J7 A8 K' t; a, N1 M& y' F3 O: ^ getch();
1 W( ]6 k7 l- m; a1 V9 e) O/* closes down the graphics system */ closegraph();
printf("We're now back in text mode.\n"); 4 ~ Z# D" R9 U7 K0 M printf("Press any key to halt:"); % K; O2 P# @ ~4 Z# [. _8 Z getch(); return 0; } 3 S/ c/ |% n9 N4 f W$ ? J7 V6 u ' D+ y9 C; L& j6 V5 r# z
; Q$ | q: ~& {# H, Q8 a7 Q函数名: clreol $ [1 U/ c4 U+ X 功 能: 在文本窗口中清除字符到行末 用 法: void clreol(void); 程序例:
9 u5 {: L: f/ I, @0 `5 n#include
int main(void)
) y) M0 m# C2 O' \, s{ clrscr(); 4 s3 w0 r" O/ v cprintf("The function CLREOL clears all characters from the\r\n"); cprintf("cursor position to the end of the line within the\r\n"); ; r2 d; T+ u, _2 K M cprintf("current text window, without moving the cursor.\r\n"); 4 c: z: R5 i9 I6 Y cprintf("Press any key to continue . . ."); , {5 H% b7 u# U2 b& `7 X gotoxy(14, 4); getch();
clreol(); getch();
return 0; ! ?/ A8 h7 a* U } , K$ D& _4 o! s. T1 M) r: q5 H2 M
函数名: clrscr % f6 W% Q! z4 [; G* T/ k' B4 D# ? 功 能: 清除文本模式窗口 ' [$ C3 v& W( K; G+ m$ @$ J0 U! r 用 法: void clrscr(void); 程序例:
4 ^/ M6 V4 n. t: F9 h#include
int main(void) { int i;
clrscr(); for (i = 0; i < 20; i++) - _6 W- [$ U' Y" R8 P, F cprintf("%d\r\n", i); cprintf("\r\nPress any key to clear screen"); getch();
, a# E: G+ U9 x" L: K6 Wclrscr(); ! {) A* y) Z3 m9 n) ^1 J4 k& L cprintf("The screen has been cleared!"); getch();
; ~4 {1 C8 u( T h0 N, m$ f. @return 0; # s8 `5 i' `% j3 k } 3 G) \8 H/ i# T4 _1 ~* _ 0 W0 U( ^& [3 H4 Y2 ~# p( c
函数名: coreleft : u8 ?# F8 E% i5 g5 t 功 能: 返回未使用内存的大小 . m1 u s; s$ W& L" y/ A7 }; |. q1 p' K 用 法: unsigned coreleft(void); 程序例:
#include
int main(void) { 4 `( \& l- ~0 {/ V7 k) H printf("The difference between the highest allocated block and\n"); 5 v8 m. l; }4 u/ z" B X4 o$ Q1 J printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
return 0; } 0 t8 T- t; d$ h1 e
函数名: cos 功 能: 余弦函数 用 法: double cos(double x); . a9 w% ]: u9 ]0 o# g 程序例:
#include
int main(void) ! o/ i% _. y; h { double result; % |- e/ d- A7 g Z$ m: n9 X4 N8 Q double x = 0.5;
result = cos(x); printf("The cosine of %lf is %lf\n", x, result); , A4 K4 w- O; _4 `2 _; s return 0; }
5 M0 Y% |9 c9 f: M% o( W函数名: cosh ' h% o$ K/ S! B* T0 p0 X0 m 功 能: 双曲余弦函数 - T: V, z8 U* T- R3 u+ E0 q 用 法: dluble cosh(double x); 程序例:
) K% m) Z. X. b0 i+ Q/ ]/ k } c#include
int main(void) : |: N$ @/ j# z0 U1 H' Z { + F7 G6 M C N8 a$ ]% s! f double result; double x = 0.5;
6 m, U5 m5 ^ o3 W0 Qresult = cosh(x); printf("The hyperboic cosine of %lf is %lf\n", x, result); % G& e9 g, [6 W2 a, x return 0; } & g! G! k0 z# C& ]4 Z ; X0 |( x0 p% o! D$ k. }3 j
, G0 f& k& k9 g0 z' y) Y6 n' w函数名: country + L( K' H! z% w$ q- M 功 能: 返回与国家有关的信息 用 法: struct COUNTRY *country(int countrycode, struct country *country); 程序例:
#include
#define USA 0
int main(void) & {% \1 v- |# u4 v2 r$ k { ) T- _. Q6 C& }% r# \2 `) K% e struct COUNTRY country_info;
' L; P* O) l8 ]country(USA, &country_info); ( O. b6 b% K/ k& F+ K3 Q printf("The currency symbol for the USA is: %s\n", , A5 I: F( @: @& A( n' Q4 x5 _ country_info.co_curr); 3 ?8 b, ^! a, T, z% Z6 } return 0; 8 ]. B W T5 ^: e& ^, t8 n) l4 e } . d! C. N. T/ |8 l. h* R 6 g4 t0 u$ s! I5 t. j! G# L- H
函数名: cprintf + J/ q! U9 q q% U% s8 S( W0 [ 功 能: 送格式化输出至屏幕 用 法: int cprintf(const char *format[, argument, ...]); 程序例:
#include
int main(void) ( X0 L2 d9 W# x7 o) |- r { ) m+ Z8 E: p @0 o0 d: c /* clear the screen */ 3 g) [9 a) h& c2 h, n/ S clrscr();
/* create a text window */ / a7 w2 p2 v y window(10, 10, 80, 25);
/* output some text in the window */ 3 M: d, U3 W f' u cprintf("Hello world\r\n");
/* wait for a key */ 3 N$ K5 ?+ S; S) h getch(); return 0; } 6 c7 z% T: I3 \5 M& Z m
函数名: cputs 功 能: 写字符到屏幕 / m5 S1 ^! [, ?5 D- m 用 法: void cputs(const char *string); 0 ]' b. e4 }5 }1 U, { 程序例:
6 A) ?, M7 @& y$ m9 w) b' R#include
int main(void) { : \+ I. W3 z; L( d& p# O. x /* clear the screen */ clrscr();
1 m) U& q* Z9 j$ _2 B3 @- E9 i! n, X9 @/* create a text window */ / j- G/ g1 m; y7 m window(10, 10, 80, 25);
/* output some text in the window */ cputs("This is within the window\r\n");
/* wait for a key */ getch(); return 0; } 5 Q6 L6 i/ S0 G6 b% o4 u
9 j: ~# v; a/ U7 w! b, P3 O函数名: _creat creat 功 能: 创建一个新文件或重写一个已存在的文件 ; F0 v) G( y$ N 用 法: int creat (const char *filename, int permiss); 程序例:
1 T) P$ @' f4 O#include
int main(void) { int handle; 2 _; \( H l7 ~0 @+ n$ O1 b char buf[11] = "0123456789";
/* change the default file mode from text to binary */ " M" q) v& W6 }8 s4 Q0 Y _fmode = O_BINARY;
; U0 Q9 {3 j2 {/* create a binary file for reading and writing */ handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
8 v4 {' G$ q4 @' s4 O$ R' S/* write 10 bytes to the file */ write(handle, buf, strlen(buf));
/* close the file */ 5 @: L ~. V- J3 J; h6 W/ | close(handle); 0 e* f2 h+ i) S/ ^# [ return 0; D2 L, z$ j- @5 M% \7 k: l } 9 }. `; m+ k0 S* @. E
函数名: creatnew 功 能: 创建一个新文件 ( d& a4 k- H p3 h4 G0 T 用 法: int creatnew(const char *filename, int attrib); " u3 ^1 ~% @( b7 n( J* Q 程序例:
#include
int main(void) & ?1 A$ G% d9 U( u { int handle; 4 y g- {: {4 q char buf[11] = "0123456789";
( @, C: x5 S6 p' m- Y3 I- p! `) m: D/* attempt to create a file that doesn't already exist */ 0 w9 U G9 F: ?; H handle = creatnew("DUMMY.FIL", 0);
if (handle == -1) printf("DUMMY.FIL already exists.\n"); else $ o1 l+ E! U4 g! `2 t { printf("DUMMY.FIL successfully created.\n"); : k1 M6 E3 {" C: |6 T write(handle, buf, strlen(buf)); " }, t2 E% u! v( y, p, B: P2 B w6 o close(handle); 5 C& S6 X: p8 V) @3 f( E } 2 X% r; h9 ^0 j/ p9 K* c return 0; } 2 u* l" ?& J0 u4 m5 Z" n! r: p
函数名: creattemp & z% P W# o* H( \ 功 能: 创建一个新文件或重写一个已存在的文件 用 法: int creattemp(const char *filename, int attrib); 程序例:
" G% h; z% ~% s" W( G; w#include
int main(void) { int handle; char pathname[128];
strcpy(pathname, "\\");
/* create a unique file in the root directory */ ! \" Y/ g( C, q# q$ Z& Z& a handle = creattemp(pathname, 0);
printf("%s was the unique file created.\n", pathname); close(handle); 5 {/ r# N' [$ k return 0; . N1 V& |1 S4 c+ m } ) B/ \7 X; Y9 C6 V
' @9 @" z# _' s# X9 @2 S! s函数名: cscanf 功 能: 从控制台执行格式化输入 用 法: int cscanf(char *format[,argument, ...]); $ g1 ~; \6 [% x3 n- g/ s9 L8 l, u Q 程序例:
7 F5 R6 A6 U9 ^) r* G#include
int main(void) ' J# S9 g |/ Z5 B' i' T! Y% c { char string[80];
4 S" z5 m* S& e0 _' D8 F! ~/* clear the screen */ clrscr();
# ]3 @. G' J( h$ ]/* Prompt the user for input */ cprintf("Enter a string with no spaces:");
/* read the input */ ! p7 R Y4 J5 S! X7 x1 `2 k8 @9 b cscanf("%s", string);
- t% W J& a; O- }- A/* display what was read */ cprintf("\r\nThe string entered is: %s", string); 7 L& ^8 _9 ]8 F3 q return 0; 8 D9 M( E \" ]* G0 e B. X2 @ } 9 _0 ?% v" |% {! D 6 ~) |1 n! G6 v, a, j" T6 Y& ~
函数名: ctime / O( S4 g! q5 h 功 能: 把日期和时间转换为字符串 # i; {# v9 D: ]+ f& r 用 法: char *ctime(const time_t *time); ; S- J% M( f8 G 程序例:
#include
int main(void) 6 H) Q" A6 Z5 R8 Z { ( X0 n h7 z& ^' u* t time_t t;
D' x+ h# o- I$ W# G6 M; z9 ~time(&t); - |' l8 t) ~' \) F printf("Today's date and time: %s\n", ctime(&t)); ) u' ~0 I; U1 K return 0; }
函数名: ctrlbrk 功 能: 设置Ctrl-Break处理程序 7 w* a0 L; V& X3 c6 X 用 法: void ctrlbrk(*fptr)(void); ! U/ R- U, l# t: o0 {3 ? 程序例:
9 a3 D* @) E& ^* n: O# T#include
#define ABORT 0
7 b7 a/ V6 z* i2 eint c_break(void) & I! e4 q$ }) ]) d7 l9 k- |1 F3 d { 5 Y* D. R) L5 V+ I3 S' _& x. C printf("Control-Break pressed. Program aborting ...\n"); - W3 l4 b7 e, L( W0 B2 @8 _ return (ABORT); 8 h& E2 g( u! M9 J2 |: O }
) z8 H* z6 q4 y; b: Q9 Bint main(void)
{ ! R& S/ ]9 c) U; Y
ctrlbrk(c_break); ) r6 z* p" \6 m) T# p
for(;;) & P& k/ V% {( U: b. d& v7 O
{
printf("Looping... Press
[em49]
谢谢啊
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |