函数大全(c开头)
函数名: cabs % T; j; p$ ?3 I4 G" c' ` 功 能: 计算复数的绝对值 用 法: double cabs(struct complex z); 9 n' g s8 q& L+ f& ]! L8 R 程序例:
#include
int main(void) ( Q1 j2 T$ R7 I q { struct complex z; + S* j1 O; V. m9 g) k3 B, l; b double val;
1 Y0 S+ a- r/ r) yz.x = 2.0; z.y = 1.0; val = cabs(z);
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); $ e1 [% a/ X# o6 u1 S9 X- l return 0; & V* w. A# t4 T% P" M8 ~( r7 V }
函数名: calloc 7 f! j& k0 ?' R 功 能: 分配主存储器 . x$ g8 J; Y1 F. \8 G 用 法: void *calloc(size_t nelem, size_t elsize); ! c ~% w: [$ g' Y$ Y 程序例:
) b5 e @5 y$ K/ m& S; ~9 ]& |" U#include
int main(void) ) I4 e6 I( b5 x( v. g( }* S { 3 s9 s% C4 p0 i" | char *str = NULL;
/* allocate memory for string */ str = calloc(10, sizeof(char));
/* copy "Hello" into string */ : |( e- M/ J: R strcpy(str, "Hello");
/* display string */ printf("String is %s\n", str);
/* free memory */ ' C. X5 j& r1 i6 V, C free(str);
return 0; * N Y t/ F" V+ c5 k }
函数名: ceil 功 能: 向上舍入 , D, V' l7 P* I* b 用 法: double ceil(double x); 程序例:
#include
int main(void) ' s, c6 q4 X. c5 S8 }4 x& h { double number = 123.54; double down, up;
7 D7 |- B5 Q1 A5 D$ g/ B9 p; jdown = floor(number); 2 i5 p4 p5 m, d: o up = ceil(number);
$ [+ z5 Y4 ]/ X7 a) Y Y9 uprintf("original number %5.2lf\n", number); printf("number rounded down %5.2lf\n", down); ' N; \* V4 r' J7 Q6 b! P printf("number rounded up %5.2lf\n", up);
return 0; } 7 V, }& i* {( i 1 e% C: m8 Z: Z- @% w) V
函数名: cgets 功 能: 从控制台读字符串 用 法: char *cgets(char *str); 2 u: i7 A! r& x- s 程序例:
' Y) u2 [4 q5 A! ~#include
int main(void) { char buffer[83]; char *p;
/* There's space for 80 characters plus the NULL terminator */ buffer[0] = 81;
* p; f- ]+ `' x' lprintf("Input some chars:"); p = cgets(buffer); $ k; h- t4 e' _5 Y1 z( y+ |9 U printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 7 w: M) {# I0 r. q8 X printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
/* Leave room for 5 characters plus the NULL terminator */ # i7 b% p* b. z) k! N$ R buffer[0] = 6;
/ t: U/ G$ N6 t! a# Z/ Aprintf("Input some chars:"); p = cgets(buffer); 1 H* m" j' V u- ? printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 1 T+ e; f2 u2 W8 X7 ~5 Q5 G printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 3 ^: c( g/ t5 E1 X d' X" Y return 0; 0 V# a# E, z" `" f2 A/ ` } / t, y4 Z9 q: ?% Z( g) W
函数名: chdir 功 能: 改变工作目录 用 法: int chdir(const char *path); ! B, L1 ?/ J5 t( J+ }. r' k 程序例:
6 |. e0 Z7 ?$ f& T' ^$ U#include
char old_dir[MAXDIR]; char new_dir[MAXDIR];
int main(void) { if (getcurdir(0, old_dir)) ( m5 b( T% m+ p/ F$ [* x- \ R { perror("getcurdir()"); exit(1); } printf("Current directory is: \\%s\n", old_dir);
( x( X% s3 M# K, s6 y5 R( U5 k/ Eif (chdir("\\")) { : |, W$ ^( H: m1 p* u, X! n perror("chdir()"); + r A# w0 Q- Z. M, U exit(1); }
L" K: l: r' b0 g6 m' p# r& iif (getcurdir(0, new_dir)) ! G' G% L; C9 b9 M { % s9 K4 X2 a+ r4 y3 S, W' W perror("getcurdir()"); exit(1); , Y3 Q( }/ W, B0 H7 F } * a9 n0 }" p0 Q% }* G printf("Current directory is now: \\%s\n", new_dir);
printf("\nChanging back to orignal directory: \\%s\n", old_dir); p1 P7 G% Q5 f if (chdir(old_dir)) ! l+ \2 D* j1 ~* |+ R9 V { : ?6 [4 Z) S7 o1 V- U5 ^ t perror("chdir()"); " T7 q1 e! _' [$ G exit(1); }
return 0; 0 V& h) T4 {% o+ [0 H } * H" a/ _, @! G7 Q
函数名: _chmod, chmod & J! h7 t$ |' \- J% l 功 能: 改变文件的访问方式 & T* `1 c, E( w+ c5 h 用 法: int chmod(const char *filename, int permiss); 1 i- ]- h* ~' k. \- L* f1 K& b+ b1 x 程序例:
7 ]/ I1 I: ~; I) g9 k6 V- \#include
void make_read_only(char *filename);
int main(void) $ V# M9 [! S( ] { make_read_only("NOTEXIST.FIL"); Z0 W. ^) u1 h r/ I# X' N0 _* O make_read_only("MYFILE.FIL"); return 0; }
void make_read_only(char *filename) { int stat;
3 s8 n% b: M$ _3 zstat = chmod(filename, S_IREAD); if (stat) printf("Couldn't make %s read-only\n", filename); else printf("Made %s read-only\n", filename); } $ c! z/ P5 D g# i 6 P" T! P. a V. \$ G$ C" b) T3 H
函数名: chsize 功 能: 改变文件大小 用 法: int chsize(int handle, long size); 程序例:
#include
int main(void) { int handle; ! ?* Y; Q0 }' ~* u' n char buf[11] = "0123456789";
/* create text file containing 10 bytes */ % k" z s& ~/ c0 y$ ^, f( ?7 s handle = open("DUMMY.FIL", O_CREAT); write(handle, buf, strlen(buf));
/* truncate the file to 5 bytes in size */ 3 F* T) a; J; E! \% d5 x% H chsize(handle, 5);
3 q7 G% e9 A/ T2 d4 k* Y/* close the file */ . @7 J( E# u' D- S# @* E/ L close(handle); 9 t6 `% N/ Q. C" K: B return 0; ( u. v6 T- g3 ~) r } & e% u, M9 e9 c
4 U& K2 l4 k+ Q& \* r0 F# i函数名: circle , y: A% d x! y% \ X% [( R 功 能: 在给定半径以(x, y)为圆心画圆 用 法: void far circle(int x, int y, int radius); 程序例:
0 |7 ?2 d# B ?! c9 W+ R#include
int main(void) { / y& J% y! b9 ?+ m/ f6 E2 n /* request auto detection */ . q. z, d! _4 J/ k$ V( } int gdriver = DETECT, gmode, errorcode; int midx, midy; int radius = 100;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
. y. t. |* J# ]9 x# @/* read result of initialization */ errorcode = graphresult(); : _) ] g* h) W& t# g. ~ if (errorcode != grOk) /* an error occurred */ { 6 ?9 _$ Y n# o( u' p- t$ {4 L3 d; C printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); N q2 @: H' o) R0 N0 I exit(1); /* terminate with an error code */ }
/ s, p7 Y/ Q9 ^. V+ jmidx = getmaxx() / 2; 0 i" P, M8 b, F: i. O3 G* p# T midy = getmaxy() / 2; ! x* s6 u2 G7 i6 X setcolor(getmaxcolor());
5 l. O; R. v& U& D9 J Y- A; M6 b/* draw the circle */ circle(midx, midy, radius);
/* clean up */ getch(); closegraph(); return 0; ; _/ {; f+ C; D% W) f4 t4 Z }
函数名: cleardevice 功 能: 清除图形屏幕 ; ^+ j' Z( P* \" m/ d 用 法: void far cleardevice(void); / v8 X9 z+ ?9 h- g 程序例:
% I) V* D% F# c5 g$ K2 E. V: F4 Y Y#include
int main(void) { : |1 P8 h+ H7 `) h+ r /* request auto detection */ ( ^# G! _( C, i int gdriver = DETECT, gmode, errorcode; 0 D' q, N( l9 r6 P8 z) X int midx, midy;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
; i" b4 c8 n# ^2 f/* read result of initialization */ errorcode = graphresult(); " L3 y3 @( {4 p- }+ J/ ^ if (errorcode != grOk) /* an error occurred */ { " X; r4 g5 T2 G" `6 ? printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); 8 L- _1 W2 N* p3 e8 r# F% ]: Q# H exit(1); /* terminate with an error code */ 7 T) E3 P0 D, D }
midx = getmaxx() / 2; ) D" ?4 m( ~. ~, J0 W% n midy = getmaxy() / 2; 9 ~8 G4 z9 g' ]7 j- a2 j* O setcolor(getmaxcolor());
/* for centering screen messages */ , z% u& T r9 o( I) a( _7 G( w& o settextjustify(CENTER_TEXT, CENTER_TEXT);
* K1 W( T) p- a4 m5 x/* output a message to the screen */ outtextxy(midx, midy, "press any key to clear the screen:");
; p" B3 H7 m& H/ J8 V3 Z+ x! t/* wait for a key */ getch();
. ^1 P& J% F o q/ X X D2 p! M/* clear the screen */ cleardevice();
* W4 g4 r4 c5 Q g( ?/* output another message */ 9 O: u( A+ c9 u- t; s, W% [2 b$ ` outtextxy(midx, midy, "press any key to quit:");
/* clean up */ + w& k+ q+ k3 M% c2 x getch(); closegraph(); return 0; } 9 S- K3 }! p0 w% H0 d [1 E 5 c# C: U( a1 u# e- ^
函数名: clearerr 功 能: 复位错误标志 . }1 E- D& S9 _ 用 法:void clearerr(FILE *stream); . {/ _1 K7 s3 n0 t; b6 H1 M 程序例:
- t" l3 m3 ?+ ]: n#include
int main(void) { - E$ Y' _' |1 q& _1 e# _ FILE *fp; . I9 o' w/ p: F% Q7 ? char ch;
/* open a file for writing */ fp = fopen("DUMMY.FIL", "w");
/* force an error condition by attempting to read */ 5 N3 B# R; C2 U2 G' i ch = fgetc(fp); printf("%c\n",ch);
~. p. ^8 V3 w; j( d/ Wif (ferror(fp)) ( O* r% g) T& i( x$ ` { p/ V) v9 S- l* d /* display an error message */ - `7 u) `5 }- F$ M9 r) F* k printf("Error reading from DUMMY.FIL\n");
/* reset the error and EOF indicators */ " e5 ~0 a7 o/ I clearerr(fp); ! l: K$ V5 |: H0 P" J }
fclose(fp); $ k3 ^! ~ ]& t. c return 0; } 3 B0 q0 E+ n+ K z; G
% |* `' Z7 d8 q' i/ Z函数名: clearviewport 功 能: 清除图形视区 用 法: void far clearviewport(void); ( f) u0 d$ {* c4 n 程序例:
: [1 @# ^* h! w) J- `#include
#define CLIP_ON 1 /* activates clipping in viewport */
3 E6 M* S' Y) O8 N5 U& y4 I4 cint main(void) ) b" ~& u& k# i4 @ { /* request auto detection */ 2 X7 r" O; Z+ k int gdriver = DETECT, gmode, errorcode; int ht;
/* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "");
- I- P1 w/ |7 _+ U( ?! ?2 z; a% ?/* read result of initialization */ errorcode = graphresult(); " q, c5 M8 T# S/ `& e6 w( Q if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); 3 T- b$ a, I K% M( m) q exit(1); /* terminate with an error code */ }
; y( c9 t1 O* Y- S2 z& D7 P! msetcolor(getmaxcolor()); ht = textheight("W");
9 Z# R5 n {: h+ ~8 U! \/* message in default full-screen viewport */ + ]4 u4 R: b0 P* h+ G outtextxy(0, 0, "* <-- (0, 0) in default viewport");
) P$ h- V+ X# N1 Q" Q/* create a smaller viewport */ l0 m/ f) o c6 d setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
+ O+ d' X; f3 s$ ]9 t+ @/* display some messages */ 4 R1 m$ c9 L- x" y7 {; |' n) Y, X outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); outtextxy(0, 2*ht, "Press any key to clear viewport:");
/* wait for a key */ & o- X0 W7 u. b+ D3 `7 t getch();
/* clear the viewport */ ' ?* G8 g1 }* z- ?' R5 } clearviewport();
/* output another message */ outtextxy(0, 0, "Press any key to quit:");
/* clean up */ 0 Y3 u+ p: ]1 s; i+ ? getch(); closegraph(); return 0; 8 B9 k! Y* @, a/ n a } " ~2 i; W' b# f, G. X8 P) S& u; v% ` 8 t1 T9 K0 t$ |9 M; z : L0 ^# G+ X g/ m
. D3 I8 N9 Y% i函数名: _close, close 2 v' p0 D+ j F+ Z3 |- F& m 功 能: 关闭文件句柄 , I- _8 Z8 q$ a% s; Q0 Q 用 法: int close(int handle); * q! d+ C. @( t! ?0 R* T% v6 Q 程序例:
#include
main() : h( X b3 P7 v$ @ { int handle; char buf[11] = "0123456789";
/* create a file containing 10 bytes */ handle = open("NEW.FIL", O_CREAT); # p, w f3 O. L5 M if (handle > -1) { write(handle, buf, strlen(buf));
/* close the file */ close(handle); / ^7 a6 k8 j0 i% x! u } else + l+ E9 X" Q2 r3 {- L2 R { printf("Error opening file\n"); ! q; q) Q) f9 k; G+ q. H5 o } . L1 d! I! \/ b# s c return 0; % F' e* `' N8 e/ j$ \; x; S5 x } 7 C7 K' G8 k, N8 P9 F; H* [8 ]& y 5 C# y" G0 ~6 _ S0 J3 ^ 9 p/ I+ L9 v3 ]" L# m+ [3 K5 M
函数名: clock * a+ F f+ [( f2 R 功 能: 确定处理器时间 3 q: N' t5 x8 m+ I( { 用 法: clock_t clock(void); 8 z/ L& y1 G t* Z% }3 ]) A 程序例:
2 I, [7 G* q1 a( o! E#include
int main(void) { 2 U. \/ \2 S. h- t clock_t start, end; start = clock();
# ?: z6 R6 H1 g, q" t7 h+ `delay(2000);
5 R5 b5 [2 z+ Send = clock(); printf("The time was: %f\n", (end - start) / CLK_TCK);
return 0; / T6 O$ P. u+ y6 z5 ?# c } & B7 F9 Q2 ]+ i- v! V! m 3 J; F; V+ X5 g& z( A( y
# R- w- a/ ^7 S, ]函数名: closegraph ! n; `" X: F* h( k 功 能: 关闭图形系统 用 法: void far closegraph(void); , @6 |# x+ N1 U5 j" [' y o+ z 程序例:
3 x1 m3 G( t, ~0 E d' E#include
int main(void) { , v2 ~! |4 z, S% Z7 V" b6 I9 D6 j+ U* | /* request auto detection */ 4 D4 R: c' Z: F P$ t' U int gdriver = DETECT, gmode, errorcode; / H9 |1 _0 @. A! I: v int x, y;
/* initialize graphics mode */ - @$ R O5 E2 h# w( t) a- s) x initgraph(&gdriver, &gmode, "");
+ m' S, y+ ^+ @* Q( F S/* read result of initialization */ 7 F/ i$ v% x5 z, e& Y8 \# ?+ p errorcode = graphresult();
- N$ _, P$ L: z' N$ E# z# ` eif (errorcode != grOk) /* an error : K8 [' X3 S! l: T$ ^4 r9 c5 b: u occurred */ { 0 P& O0 k9 F9 N% c- R% z* u) n F5 c printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); $ ] b8 K' O( n$ E+ c" b6 M' q getch(); exit(1); /* terminate with an error code */ }
8 Z% o4 [6 U1 @7 A/ t; x+ E0 \8 Xx = getmaxx() / 2; " }0 x- i+ F z1 p# S y = getmaxy() / 2;
+ E& b6 f; x' q/* output a message */ . J$ T- ]9 o1 j! f7 A settextjustify(CENTER_TEXT, CENTER_TEXT); 0 _) ?2 a* ^, X/ c u outtextxy(x, y, "Press a key to close the graphics system:");
+ |$ g8 b; B/ x4 @5 B2 r4 t- r9 h/* wait for a key */ 5 {5 ]5 b: K6 g/ O3 }4 L getch();
5 a6 C+ Z0 D; a% n/* closes down the graphics system */ closegraph();
printf("We're now back in text mode.\n"); printf("Press any key to halt:"); 3 h$ j3 \( M0 e, @. m getch(); ( ?6 D$ Q( E6 b7 _6 F) p2 a4 r return 0; } : N/ U3 B, v0 r' E
函数名: clreol 功 能: 在文本窗口中清除字符到行末 用 法: void clreol(void); 3 R/ [. Y4 x) M# C/ }' ~/ q7 P- A 程序例:
#include
int main(void)
4 C( Y! S- @+ N" G3 ?1 y+ |: N{ ( s2 @- V# g" M clrscr(); 3 p0 V* A! P- g# ?/ D4 ]3 E0 z1 l cprintf("The function CLREOL clears all characters from the\r\n"); 9 \2 o: V0 p, K$ [ cprintf("cursor position to the end of the line within the\r\n"); cprintf("current text window, without moving the cursor.\r\n"); cprintf("Press any key to continue . . ."); % Z3 S9 s+ _; T* B1 ^ gotoxy(14, 4); 1 ?3 {9 p" m6 y. s5 v9 [ getch();
8 }4 Q2 j9 ?2 A7 t M5 S9 yclreol(); / R. I& Q0 l% C4 N7 c. w6 h; ^ getch();
return 0; + H( ]2 @- I0 l } " G. j/ Q: N5 L, a% W 6 q* D E) i9 ]3 u5 Q
7 Y* ~: N- g9 ]6 _: h& {: c函数名: clrscr 功 能: 清除文本模式窗口 ' T0 u2 K/ N, M7 @ 用 法: void clrscr(void); 6 t) o& Q9 n5 O 程序例:
# x1 d3 a G, N! m#include
int main(void) { int i;
: Q# }$ f' a$ m1 J: m. W. Aclrscr(); for (i = 0; i < 20; i++) cprintf("%d\r\n", i); ! J6 E3 F1 a" V" {1 O" l3 n% i: U G cprintf("\r\nPress any key to clear screen"); $ N/ |$ \; x* e2 [ U8 ]" Q getch();
5 n7 }( D( k1 s5 l- @clrscr(); ! `- q0 k. N1 ~. y Y- Z cprintf("The screen has been cleared!"); & G" [! j4 s( {+ N- a/ U% B, z getch();
return 0; 1 K5 T6 s' i: c L. D } 7 Y6 Q6 Z# L& \1 R7 t6 t ! t9 U. F, P7 y9 [$ [9 Z
函数名: coreleft / ]2 F: Q9 c2 O 功 能: 返回未使用内存的大小 用 法: unsigned coreleft(void); 程序例:
. Y0 n- y2 M: f {6 _% b& Y1 [; Y, D( r/ t#include
int main(void) { : o, A2 _6 J" R 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; }
函数名: cos * W2 k8 R) }+ p5 j6 v) C$ j( v/ S 功 能: 余弦函数 用 法: double cos(double x); ( U: H" q, [0 x% _4 I 程序例:
#include
int main(void) ) j' R/ z. r) Y" `$ R { ) b3 b8 y3 {! O1 J double result; 5 n- E+ L; R+ Y1 A. h5 e' _! N double x = 0.5;
1 s" c0 n" d1 {6 K% U$ Qresult = cos(x); printf("The cosine of %lf is %lf\n", x, result); return 0; } # j% U7 p5 L* N8 \0 D' L
函数名: cosh 功 能: 双曲余弦函数 用 法: dluble cosh(double x); 程序例:
#include
int main(void) . k- ]3 {& S, [' F) K2 T6 L6 i j { 4 s8 E4 l( h6 O: l( ^8 A- { double result; ' x7 q6 @% n" v$ D/ t2 t8 ?0 |! s% P double x = 0.5;
result = cosh(x); 8 X e; F$ R6 R$ w" f printf("The hyperboic cosine of %lf is %lf\n", x, result); 4 K2 _/ b0 v# m. k# m, O T+ R( T return 0; 1 N/ P; |' R- {# e. j( ~ } . l8 `- A( Q! O7 g8 V! A
函数名: country - s( o8 _5 ~0 z+ y1 E 功 能: 返回与国家有关的信息 + n+ r. m J+ m2 D 用 法: struct COUNTRY *country(int countrycode, struct country *country); 程序例:
5 @+ N; u" ?0 s: T# E n+ S4 I6 W#include
#define USA 0
% R$ u* p, a8 l# B/ _9 u9 Iint main(void) { struct COUNTRY country_info;
country(USA, &country_info); printf("The currency symbol for the USA is: %s\n", country_info.co_curr); 9 ^5 t! b( o4 ]3 Y return 0; * f3 x' C: r: M' c4 }0 Z } & U {! B$ N4 o4 Z 0 L/ e2 Q8 o+ b
9 d! A, f" @# }8 |7 V函数名: cprintf 功 能: 送格式化输出至屏幕 用 法: int cprintf(const char *format[, argument, ...]); 程序例:
#include
int main(void) { 3 X- Z% H( F) d1 E1 J /* clear the screen */ 3 v: ` k; m; G1 x; n9 W clrscr();
* m3 s1 x R0 S# p6 ]/* create a text window */ + P7 E! ]6 `# C) f2 F. [ window(10, 10, 80, 25);
/ P' c& ^3 S0 T* f# k& Y! I: R/* output some text in the window */ cprintf("Hello world\r\n");
; y! [3 s" A6 A3 r. W/* wait for a key */ / l9 h8 z; N& n+ H/ c% E getch(); return 0; 9 K% ~4 [- O8 y1 i# M2 b3 Q } O7 ~! q N9 c* Q: F- u; Z
1 _! j" \: C' L$ _5 r; _函数名: cputs 功 能: 写字符到屏幕 . u( I1 n& T w0 E6 i 用 法: void cputs(const char *string); ! V+ I) Y) s9 U, d! ? 程序例:
, s3 v% U: P! H R#include
int main(void) $ N& ?# L6 `2 V4 j6 d" p. A* P { 6 e3 |# k4 A, C- T, L: o /* clear the screen */ : W. V; J! N# m1 _% i+ O4 | clrscr();
/* create a text window */ window(10, 10, 80, 25);
/* output some text in the window */ cputs("This is within the window\r\n");
/* wait for a key */ z+ v# h0 F2 r% q$ O getch(); return 0; % o1 }' g) s' k, |+ q* j1 ? }
$ ^) l; ]! C$ d/ n b' E' K2 Z/ O函数名: _creat creat " h/ q8 y$ z% O 功 能: 创建一个新文件或重写一个已存在的文件 用 法: int creat (const char *filename, int permiss); * l1 a$ D' i, j8 \$ i& ^0 F' J 程序例:
#include
int main(void) { int handle; char buf[11] = "0123456789";
3 F/ _3 p. f, H7 ]! J/* change the default file mode from text to binary */ _fmode = O_BINARY;
/* create a binary file for reading and writing */ handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
/* write 10 bytes to the file */ write(handle, buf, strlen(buf));
& X* [7 ]5 D4 |. ]; Q/* close the file */ close(handle); 5 W) ^. N! z! V+ R; B- k return 0; }
& w5 L0 [6 d& p$ ?( {7 m8 f函数名: creatnew 功 能: 创建一个新文件 用 法: int creatnew(const char *filename, int attrib); 程序例:
& B: j; |+ S; O; ]#include
int main(void) { int handle; . h/ p3 J9 E# w1 \/ m, ? char buf[11] = "0123456789";
/* attempt to create a file that doesn't already exist */ handle = creatnew("DUMMY.FIL", 0);
- [! Q N4 N# j4 w) w$ D# Dif (handle == -1) / ~/ E: e1 `; P; G v& A% e5 x printf("DUMMY.FIL already exists.\n"); 3 l+ [' R B7 E, T! w. R else { printf("DUMMY.FIL successfully created.\n"); ) O; M9 J+ _1 i8 ~$ y* v; I write(handle, buf, strlen(buf)); 4 U! i6 W5 V6 b- T% v close(handle); 9 I' F9 @, p8 L/ a, G. J } 9 `- `9 D' N3 r6 b return 0; " J* ?1 ?; E! b: O- ^, B } ! H4 G* |) T$ Q8 m. D7 F! } + K* O3 t$ G0 D# b9 ` 0 Q5 u4 f' F* q/ b4 l
函数名: creattemp 功 能: 创建一个新文件或重写一个已存在的文件 用 法: int creattemp(const char *filename, int attrib); 程序例:
#include
int main(void) { : @, L- V. F4 r int handle; & p5 c/ `. h4 M, z3 ]3 b0 T char pathname[128];
$ w( D* Q6 c0 |8 `% p% k$ Zstrcpy(pathname, "\\");
$ {4 {. c# Y& A' H/* create a unique file in the root directory */ handle = creattemp(pathname, 0);
printf("%s was the unique file created.\n", pathname); close(handle); 1 L. o1 I) z, S, T return 0; } $ P ~4 D/ C5 @( e% V" A $ Y6 Q( y8 f/ F/ x 7 L8 B1 Z8 t) B, Q9 ^; d
函数名: cscanf T4 |1 L# }: T6 {& F 功 能: 从控制台执行格式化输入 用 法: int cscanf(char *format[,argument, ...]); 程序例:
#include
int main(void) . M8 @; o- E5 ]6 q3 \" m { # q4 c$ L% d9 X0 h b& H char string[80];
; T6 j5 b: m! Y+ a) u/ z/* clear the screen */ clrscr();
; ]; ^. Y- Q5 g1 Z/* Prompt the user for input */ ; o: b5 {4 B4 D$ x5 Z' s0 G5 q, _ cprintf("Enter a string with no spaces:");
& a3 U( N9 Y$ k, M/* read the input */ cscanf("%s", string);
5 U6 s) T/ P [$ E( h8 W) N' }# r4 l/* display what was read */ 0 S( S3 r( v: X cprintf("\r\nThe string entered is: %s", string); 1 A8 P* f4 `& F3 p return 0; } 7 Y; {8 A" G. A/ y: U, ^
函数名: ctime 功 能: 把日期和时间转换为字符串 1 E% f: _2 M7 s( k; T 用 法: char *ctime(const time_t *time); 程序例:
#include
int main(void) { ; V8 V. T+ }) S; N- @ time_t t;
- i1 b; _; S3 W1 z1 ?time(&t); printf("Today's date and time: %s\n", ctime(&t)); - `7 |+ _2 C$ W _. p9 b- K return 0; } 4 ` d5 x1 U1 O9 ]9 L# Z+ o
函数名: ctrlbrk 功 能: 设置Ctrl-Break处理程序 6 K8 r: ]) L1 m 用 法: void ctrlbrk(*fptr)(void); 程序例:
#include
#define ABORT 0
int c_break(void) % B9 u, _) l8 _) d/ d! p { % E! C2 `( O& ]. W# o) g printf("Control-Break pressed. Program aborting ...\n"); return (ABORT); }
int main(void) ; U0 o) D$ X) L8 R e7 V
{
ctrlbrk(c_break); 4 ?. [4 A% ]9 i" x/ K- k. S
for(;;)
{
printf("Looping... Press
[em49]
谢谢啊
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |