- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
|
< align=center><FONT color=#0000ff size=3><B><FONT color=#cc0000>函数大全(d开头)</FONT></B></FONT>; q4 b; t- y4 x- {
</P>
! I! Y1 U7 o" `( O<p>< ><FONT color=#ff0000>函数名: delay </FONT>
! D) m- Y. S* U2 w& K功 能: 将程序的执行暂停一段时间(毫秒)
) a t I$ e& S6 O用 法: void delay(unsigned milliseconds);
& k3 N4 y" p( ^程序例: ! A# [9 Q4 |! S- q$ p# M3 T
<FONT color=#0000ff>/* Emits a 440-Hz tone for 500 milliseconds */ ; R# n2 D" f$ C- s& V
#include <DOS.H></FONT></P>< ><FONT color=#0000ff>int main(void)
* f3 g/ H3 D, W/ T" s2 E: @) [{ 4 x1 S7 d! Q$ \* U
sound(440); . V: g2 Z' M8 j' }, c5 g
delay(500); ; B8 T; B" J( {0 C( A, E! \& c
nosound(); </FONT></P>< ><FONT color=#0000ff>return 0;
# r: a' _& m, t: q} * u) D( {* s5 D- {, h
% s2 j" q3 B5 k8 w: R
</FONT></P>< ><FONT color=#ff0000>函数名: delline </FONT>
* D+ o: A8 \1 ?. e功 能: 在文本窗口中删去一行
! e7 n& |, d |2 O9 W用 法: void delline(void); , I8 g3 P5 P7 \6 G+ ]# P" l
程序例: </P>< ><FONT color=#0000ff>#include <CONIO.H></FONT></P>< ><FONT color=#0000ff>int main(void) $ f2 X6 ~ }5 L5 G8 J8 Q8 ~4 Z% n0 G
{
6 m; V6 b$ ~$ G% F: y% @clrscr();
9 ~: T2 m3 Z0 J2 O% z4 t8 Gcprintf("The function DELLINE deletes \
( g, O1 D. F# o4 T9 N& Ethe line containing the\r\n"); ; g" h5 n5 F ]1 {5 E& _
cprintf("cursor and moves all lines \ ; D% L" D& b" Z
below it one line up.\r\n"); 8 j( y7 ?0 T6 y, f6 [- L: m$ i
cprintf("DELLINE operates within the \
& C4 Y4 @1 Y, u S4 t8 e- i7 Ycurrently active text\r\n"); & `( m" K5 S' m3 n
cprintf("window. Press any key to \
% H% u# w+ f ]& J5 qcontinue . . .");
1 ~# d! p# [0 m1 ?gotoxy(1,2); /* Move the cursor to the
# ^7 [/ r/ \3 V6 J# M: P8 b0 ?" { Esecond line and first column */
& ?4 C/ b1 h' {, |4 j& k1 rgetch(); </FONT></P>< ><FONT color=#0000ff>delline();
; F8 n1 p, k) c" ~" k9 a( Lgetch(); </FONT></P>< ><FONT color=#0000ff>return 0;
& b. P& {/ M% i5 R) b j% I i B} </FONT>
# `5 Y$ T, u& `</P>< ><FONT color=#ff0000>函数名: detectgraph </FONT>, q: I6 s* s" A; a* F8 c$ A5 D
功 能: 通过检测硬件确定图形驱动程序和模式
5 H* _; N" |' b. o0 b( E- S4 M" j用 法: void far detectgraph(int far *graphdriver, int far *graphmode); & e% w- h+ V2 o# y, L. {! \, G
程序例: </P>< ><FONT color=#0000ff>#include <GRAPHICS.H>) L0 K+ p/ W% \4 H/ L
#include <STDLIB.H>
) [( J; c9 k0 K#include <STDIO.H>
/ C( n$ Y; g9 D4 \% N. Y#include <CONIO.H></FONT></P>< ><FONT color=#0000ff>/* names of the various cards supported */ % ~& N, [; _3 G( ]* R+ [% \
char *dname[] = { "requests detection",
4 R. ^) U1 ~- h3 k& l0 x- Z4 J"a CGA",
) j ]& Q; S6 I1 `1 x"an MCGA",
1 P2 t) x1 c5 q4 [9 x% w% z. o"an EGA", : ~# U0 g( Q5 d: {. z5 P3 H
"a 64K EGA", 5 |8 R, `$ @7 |7 O* d7 J
"a monochrome EGA",
( `& w: U3 ]7 }/ f0 d. k"an IBM 8514",
( W1 C ~0 Q0 \4 c" h4 C1 @6 N5 |"a Hercules monochrome",
) ]4 h4 u* D. i* l8 G2 u! ["an AT&T 6300 PC", - E o \$ ? x. ?/ n' Y! D
"a VGA", % h+ G1 C, P; {* m
"an IBM 3270 PC"
1 C5 s+ {2 M$ d0 x' k}; </FONT></P>< ><FONT color=#0000ff>int main(void)
! B# z2 [( c. ?) u5 [, Z{
" w8 H4 V; j; B6 h& L/* returns detected hardware info. */ & q+ @, p2 S# x" `9 R8 R" ]" e8 Y
int gdriver, gmode, errorcode; </FONT></P>< ><FONT color=#0000ff>/* detect graphics hardware available */ 7 u# d+ m. m r; P/ N, n
detectgraph(&gdriver, &gmode); </FONT></P>< ><FONT color=#0000ff>/* read result of detectgraph call */ - @/ |; {& ^* Q5 d) W( h* M! g% c
errorcode = graphresult(); ) [+ `! n$ b4 j/ o5 r9 |8 l' w
if (errorcode != grOk) /* an error + \% K6 R0 D+ v* x1 e: a x: \' [
occurred */
! k; v) c b4 S: i. j4 J% \{ " c3 G' y6 o- M d4 v6 z7 u/ J2 I
printf("Graphics error: %s\n", \
4 G) @# `' S# a" F3 O$ o( c( @* V2 egrapherrormsg(errorcode));
3 K7 u6 U( ]- bprintf(" ress any key to halt:");
3 t6 D5 q; x; C* s( f. L B/ ?- Kgetch(); 1 C8 V( E7 T2 i( P- ^( k
exit(1); /* terminate with an error
3 I& M7 d0 [. ?+ S0 T: s$ ucode */ . k3 p' T7 i _4 v* [
} </FONT></P>< ><FONT color=#0000ff>/* display the information detected */
# O1 n0 E& p4 h6 h7 O. Rclrscr(); . r& t0 N" m7 N4 w8 q% K/ |
printf("You have %s video display \
2 I( n- I% z+ V6 s @ X0 mcard.\n", dname[gdriver]);
4 \) ]( B- U4 M1 ]' eprintf(" ress any key to halt:");
5 q% D! d& _. y: xgetch();
" {! c+ b) B; j G0 i8 ]( w5 mreturn 0;
' S& o9 P/ {. ]# g, }} </FONT># ^3 o& c, K0 C H
7 ]) t/ }2 _2 [4 s
! t! z+ w/ d2 u- P+ l4 ]</P>< ><FONT color=#ff0000>函数名: difftime </FONT>
0 ?5 g, ]7 v; i w7 A, U/ I功 能: 计算两个时刻之间的时间差
2 j T) W7 H! A) q用 法: double difftime(time_t time2, time_t time1);
5 z/ G- C1 }8 }程序例: </P>< ><FONT color=#0000ff>#include <TIME.H>
: D9 d5 p- L5 ^* i3 q. S/ X8 f j7 f#include <STDIO.H>( j) L0 [9 {2 _7 x
#include <DOS.H>, [. c8 i6 L5 I. a* H2 s: t
#include <CONIO.H></FONT></P>< ><FONT color=#0000ff>int main(void)
4 r. C7 S$ O% ^* P: S{ ( N9 C) n2 p B/ q' f1 J+ T
time_t first, second; </FONT></P>< ><FONT color=#0000ff>clrscr();
; x/ ~" X3 ~; W: j, afirst = time(NULL); /* Gets system 2 q. R# N! c8 e! y/ A# {4 U$ c
time */
f* \* G* `/ D8 }% Fdelay(2000); /* Waits 2 secs */ ! T8 p; n' k. l0 D6 ^
second = time(NULL); /* Gets system time
: q3 X/ f8 D3 Pagain */ </FONT></P>< ><FONT color=#0000ff>printf("The difference is: %f \
% h& F/ d5 D/ Dseconds\n",difftime(second,first)); . Q& l# U& N1 S' W9 |
getch(); </FONT></P>< ><FONT color=#0000ff>return 0;
" b# J% W( w& o9 }& m} 8 r3 T7 B5 f" m5 w# ?* |
</FONT>
o6 c& d1 z& T</P>< ><FONT color=#ff0000>函数名: disable </FONT>
' r5 s6 D5 h' G& h功 能: 屏蔽中断 5 F$ u% \7 k: a. ]0 f% t% O
用 法: void disable(void);
% l* y1 R! F6 L1 \) g" [程序例: </P>< ><FONT color=#0000ff>/***NOTE: This is an interrupt service
2 o4 V3 V7 X: H# X: Kroutine. You cannot compile this program + f% G& v; @; V. [7 {# s
with Test Stack Overflow turned on and - f6 q& p; M) V$ N6 G- ^
get an executable file that operates 0 L8 d7 B6 J" W+ N4 t4 }$ i; H
correctly. */ </FONT></P>< ><FONT color=#0000ff>#include <STDIO.H>
; Y3 S& G: y z#include <DOS.H> U( W1 n# f7 ?5 ~( Z/ }' T( S# Q
#include <CONIO.H></FONT></P>< ><FONT color=#0000ff>#define INTR 0X1C /* The clock tick
* X7 H# [9 W( sinterrupt */ </FONT></P>< ><FONT color=#0000ff>void interrupt ( *oldhandler)(void); </FONT></P>< ><FONT color=#0000ff>int count=0; </FONT></P><P><FONT color=#0000ff>void interrupt handler(void) : K: J: H; z( M+ l2 r
{ " p$ A3 u9 ]6 L; B
/* disable interrupts during the handling of 7 k+ O/ l' d0 e
the interrupt */ 7 Z2 D$ [4 K- T8 w! p8 M; x. z
disable(); M: ~; K5 V$ R- I$ X
/* increase the global counter */ 6 q8 Q2 }& [, h; ]7 t* B" d+ \
count++; % \* x; G3 D/ `- }. \) B
/* reenable interrupts at the end of the 8 l* e4 `5 r& `; u# W3 U! Y& y
handler */
2 q4 |' v+ ~' L! a# I* @4 ]0 ]6 D$ V- Venable(); 9 E/ y$ c3 t- K( @" t$ P3 n. A
/* call the old routine */
3 p/ D+ O4 {$ @6 o$ t! f# Z9 goldhandler(); 9 f! ~* c7 S! B4 i3 S. j+ a1 |
} </FONT></P><P><FONT color=#0000ff>int main(void)
9 E- F. j# y+ |; g8 `+ x{ . q/ _; n% [6 C. f" q: C! a9 f
/* save the old interrupt vector */
- J2 \; i1 q2 ?oldhandler = getvect(INTR); </FONT></P><P><FONT color=#0000ff>/* install the new interrupt handler */ , k# @/ I# e/ X1 s) _' O
setvect(INTR, handler); </FONT></P><P><FONT color=#0000ff>/* loop until the counter exceeds 20 */ 6 R- J$ ?6 [. W1 K, Y) e" a2 U
while (count < 20)
7 q) h, i+ d/ Z( L Z# Cprintf("count is %d\n",count); </FONT></P><P><FONT color=#0000ff>/* reset the old interrupt handler */ ?# q2 M3 A. [7 u( @( k& C! e, f
setvect(INTR, oldhandler); </FONT></P><P><FONT color=#0000ff>return 0; & X: z) r% K0 b6 }+ q2 \, x
} </FONT></P><P><FONT color=#ff0000>函数名: div </FONT>
% K9 u5 R. w; a. P7 y9 t& ^9 R功 能: 将两个整数相除, 返回商和余数
5 B+ ]! G% F3 x) v' c. Y用 法: div_t (int number, int denom);
& I ], @6 S/ A/ W$ D程序例: </P><P>#<FONT color=#0000ff>include <STDLIB.H># {# L7 o/ f3 I* u# h
#include <STDIO.H></FONT></P><P><FONT color=#0000ff>div_t x; </FONT></P><P><FONT color=#0000ff>int main(void) T2 S- u4 w$ O3 @- P8 \4 [) U# t: |
{
) D: W, b _5 Cx = div(10,3); # U- E$ a# G% Z- w( d* w
printf("10 div 3 = %d remainder %d\n", x.quot, x.rem); </FONT></P><P><FONT color=#0000ff>return 0; 2 c r( N" U: U+ t \+ D
}
4 a$ I$ h; E! s* X6 ?, x4 Z/ S$ i</FONT>
* S/ }: z- B; X( l( P/ H</P><P><FONT color=#ff0000>函数名: dosexterr </FONT>. ]+ Z C/ {8 F6 M$ q' W/ Z
功 能: 获取扩展DOS错误信息 / o' @+ N+ e# C' ~) F. s
用 法: int dosexterr(struct DOSERR *dblkp);
- t3 u" Q, T% \0 O s r程序例: </P><P><FONT color=#0000ff>#include <STDIO.H>
5 `( W" J" k6 c- H0 K8 ?& n3 |& O#include <DOS.H></FONT></P><P><FONT color=#0000ff>int main(void) * ^6 |' |* p2 r% y
{
: ]2 d& f' ]: X1 E6 _- d/ HFILE *fp;
4 O- A- J1 V% R2 g9 a$ [struct DOSERROR info; </FONT></P><P><FONT color=#0000ff>fp = fopen("perror.dat","r");
- m% X2 X3 z* t+ Y, P* T) o; a) yif (!fp) perror("Unable to open file for 7 o3 ?1 E: t: ~- L( R
reading"); t4 {7 s) |/ f3 V, s
dosexterr(&info); </FONT></P><P><FONT color=#0000ff>printf("Extended DOS error \ ! E5 ~: y% X8 b$ _9 T8 y9 m$ N
information:\n");
; X2 a/ J0 f6 yprintf(" Extended error: \ ; q% A5 A% _- z- n$ O* l! f( A% H; z" [
%d\n",info.exterror);
* k) p1 z x7 F: {printf(" Class: \ 3 k/ E3 l4 J2 k' L" B6 h
%x\n",info.class);
# h; c; A$ V, o- }printf(" Action: \ 8 Q. a2 l5 F, g! x- _& @- q
%x\n",info.action);
# n" T6 t1 c9 U" C* \9 lprintf(" Error Locus: \ ' t9 I# [/ A1 h; m4 Q' v- p1 H' j
%x\n",info.locus); </FONT></P><P><FONT color=#0000ff>return 0; C: A0 y! F; s, R
} </FONT># h' o( \& T* W9 @
- d8 d+ B. U. l& J3 ]3 d( I
</P><P><FONT color=#ff0000>函数名: dostounix </FONT> }& `" q! V: E( z) }0 f3 v
功 能: 转换日期和时间为UNIX时间格式 F8 r% c! B. ^. r
用 法: long dostounix(struct date *dateptr, struct time *timeptr); 1 S b2 ^/ Y$ e" U
程序例: </P><P><FONT color=#0000ff>#include <TIME.H>" v! a0 Y1 W3 u2 G( v" A d: W* }
#include <STDDEF.H>3 o9 }8 e' b: O) b/ l
#include <DOS.H># y4 V7 t- F3 \2 e# C
#include <STDIO.H></FONT></P><P><FONT color=#0000ff>int main(void) ' z2 y6 h8 ^7 L
{ ; C- g+ P3 X5 c P2 `9 |
time_t t; + f2 _9 R5 c& F
struct time d_time; * V* q# |( g! f
struct date d_date;
$ ~- x: ]" L/ j8 N$ \0 N% z; jstruct tm *local; </FONT></P><P><FONT color=#0000ff>getdate(&d_date);
8 s- S$ W2 M2 k% {4 n o6 U9 [gettime(&d_time); </FONT></P><P><FONT color=#0000ff>t = dostounix(&d_date, &d_time);
6 y+ z% B/ x9 ~" m8 I P& i0 @7 klocal = localtime(&t);
' w$ p% N- Z" v0 @; G* p0 jprintf("Time and Date: %s\n", \ : E1 E/ \( \' N0 m$ }
asctime(local)); </FONT></P><P><FONT color=#0000ff>return 0; , H/ \1 }' I7 i' Z' O2 G w- ?/ F
} </FONT>) L7 C& m9 C" L" r# T0 M q
. h/ R# s- Q W, ^% Y T9 X</P><P><FONT color=#ff0000>函数名: drawpoly </FONT>
& v4 B5 B8 I3 C5 Z$ E% a* a! B功 能: 画多边形
& Y( \# x) z: W0 ?7 h6 r用 法: void far drawpoly(int numpoints, int far *polypoints); ' D2 X% s) ~& @) Z# V
程序例: </P><P><FONT color=#0000ff>#include <GRAPHICS.H>6 c$ P% z) Y% q* i
#include <STDLIB.H>- w6 W7 F2 Z# N$ z; ?* L9 V
#include <STDIO.H>8 S3 [% b+ P& u
#include <CONIO.H></FONT></P><P><FONT color=#0000ff>int main(void)
# e' q5 d$ E" |9 P3 l8 m{ ) \6 U6 v" H; O$ v7 \# B
/* request auto detection */
8 m1 Y3 B: J* |7 _int gdriver = DETECT, gmode, errorcode;
7 E, H9 p3 X* W1 ?# ]int maxx, maxy; </FONT></P><P><FONT color=#0000ff>/* our polygon array */ ' Y$ r% F& S2 A* ]# }
int poly[10]; </FONT></P><P><FONT color=#0000ff>/* initialize graphics and local
8 {$ v" ^9 j: ^$ pvariables */ ( P' W$ F' c1 D; a* R
initgraph(&gdriver, &gmode, ""); </FONT></P><P><FONT color=#0000ff>/* read result of initialization */
5 [, b+ y1 L: h2 terrorcode = graphresult(); * B/ U9 w, T( x
if (errorcode != grOk)
$ C$ c, O* c, S9 c7 Q" k/* an error occurred */
( w% N3 a7 `6 [+ X/ A" ~{
; R4 S( w9 W2 \1 Yprintf("Graphics error: %s\n", \ U: V7 D" Q z; O# n% q
grapherrormsg(errorcode)); 9 o6 C% M* Z& q; B
printf("Press any key to halt:"); 7 T! G; b) S! c# Y; G( P9 e1 y
getch(); ) Q4 a/ d$ L9 A
/* terminate with an error code */
" K- r8 |# ]5 M* E' lexit(1); 5 |9 D9 [: A& M& ?; _$ A4 d
} </FONT></P><P><FONT color=#0000ff>maxx = getmaxx();
8 ^! @3 e3 g) B/ @% M$ s& Y: Y% Imaxy = getmaxy(); </FONT></P><P><FONT color=#0000ff>poly[0] = 20; /* 1st vertext */ - @* H% ] [7 S6 e/ `
poly[1] = maxy / 2; </FONT></P><P><FONT color=#0000ff>poly[2] = maxx - 20; /* 2nd */ 5 ?& [' Y! M5 e# B( E' E( O/ I O
poly[3] = 20; </FONT></P><P><FONT color=#0000ff>poly[4] = maxx - 50; /* 3rd */
( \4 C! s$ `) B$ [" J8 W5 r- l% Npoly[5] = maxy - 20; </FONT></P><P><FONT color=#0000ff>poly[6] = maxx / 2; /* 4th */
# ~! F* D2 R, H' W& H# g% B, |poly[7] = maxy / 2;
6 ]5 i6 Q1 j( B1 a. j- H; E/*
3 _4 i c& {$ p/ I6 o2 Idrawpoly doesn't automatically close
' Q* M, z7 C( Cthe polygon, so we close it. " w3 u3 T2 _+ e0 Q% y6 q
*/
* A& K6 W8 v9 W; q w9 @7 Wpoly[8] = poly[0]; + L0 m+ x9 [4 f/ @: s
poly[9] = poly[1]; </FONT></P><P><FONT color=#0000ff>/* draw the polygon */
, i# S2 \* V1 U( X; }drawpoly(5, poly); </FONT></P><P><FONT color=#0000ff>/* clean up */
$ \$ I& a5 t1 M" ?. u3 agetch();
5 T2 Z% E# u6 }3 X- Fclosegraph(); 2 B4 i9 z' ^, d, j$ _& z: V$ V
return 0;
8 }" F' A' K, y5 Y5 e# f} ! ^/ s6 I8 C( V9 n' e( s
</FONT>! W& }3 P# D' j# ^; z6 \
</P><P><FONT color=#ff0000>函数名: dup </FONT>1 a1 ^% j) h$ q& _. i3 x2 i( r
功 能: 复制一个文件句柄 6 b: G& B. W/ k3 x' F/ Z
用 法: int dup(int handle); ' O+ `& Q+ T3 m; n$ j
程序例: </P><P><FONT color=#0000ff>#include <STRING.H>4 ~0 m' U4 W4 S# o
#include <STDIO.H>
2 C8 O6 n' g/ a. U6 S& ~* q! ?#include <CONIO.H>
$ b: E$ Q: N7 H" t' J#include <IO.H></FONT></P><P><FONT color=#0000ff>void flush(FILE *stream); </FONT></P><P><FONT color=#0000ff>int main(void) 1 W. `: \, a6 G
{ $ ~, f: q- d- u0 O$ E/ a* Z
FILE *fp; w* O$ @# O, Y3 F A; D' I; [) N
char msg[] = "This is a test"; </FONT></P><P><FONT color=#0000ff>/* create a file */ ( S# Y! Y% L/ ~' b
fp = fopen("DUMMY.FIL", "w"); </FONT></P><P><FONT color=#0000ff>/* write some data to the file */ 8 R" _* C4 H- J5 R5 P7 V4 [
fwrite(msg, strlen(msg), 1, fp); </FONT></P><P><FONT color=#0000ff>clrscr();
% c: W4 B8 _/ W/ L5 ~printf("Press any key to flush \
- ]& G' C2 M4 J cDUMMY.FIL:");
; q; Z' j) }$ {' [getch(); </FONT></P><P><FONT color=#0000ff>/* flush the data to DUMMY.FIL without
) F' K% i9 J# |( f, m, Wclosing it */ ) \) F2 ~: n. \! A1 ?7 z
flush(fp); </FONT></P><P><FONT color=#0000ff>printf("\nFile was flushed, Press any \
. k5 B, M# B5 Q( r- D4 o/ ikey to quit:");
( F* X' l( m% @% g6 V4 agetch();
$ N" k7 n3 z6 v% vreturn 0; " O ?: r! Q( ]" L0 H
} </FONT></P><P><FONT color=#0000ff>void flush(FILE *stream) / X: r; V( [6 w A: ~
{ 9 C, [. \ }1 [) J
int duphandle; </FONT></P><P><FONT color=#0000ff>/* flush TC's internal buffer */ 8 B1 c& f* n& R, x% h
fflush(stream); </FONT></P><P><FONT color=#0000ff>/* make a duplicate file handle */
5 H' h' s; ^" a# nduphandle = dup(fileno(stream)); </FONT></P><P><FONT color=#0000ff>/* close the duplicate handle to flush the ( L$ A( }. Y+ i& Q* Z( C2 B! T
DOS buffer */ . Z( P# A8 c: L9 m- P
close(duphandle); : I A: e! a8 d; S" n" l
}
' L' n- e; `, c2 A# y7 O; K6 a3 u3 t/ f) x6 E N6 w, _$ s
</FONT></P><P><FONT color=#ff0000>函数名: dup2 </FONT>, I, f) Y4 }: X7 E& @2 u0 x
功 能: 复制文件句柄 6 I, @+ m( Z- @ u2 y: f
用 法: int dup2(int oldhandle, int newhandle); ) ~5 e7 N7 ^$ _' T0 g5 {! M7 T
程序例: </P><P><FONT color=#0000ff>#include <SYS\STAT.H> I/ Y8 b R* J% C3 G3 {
#include <STRING.H>
( u& a' D. G/ [3 y5 w8 B8 w1 \#include <FCNTL.H>0 j5 `7 V/ t2 u! I- @
#include <IO.H></FONT></P><P><FONT color=#0000ff>int main(void) 4 Z$ f, b$ `0 P+ w0 a2 @% \" r
{ * V1 E9 A& c! W7 }% ]! r
#define STDOUT 1 </FONT></P><P><FONT color=#0000ff>int nul, oldstdout;
- D! q1 u8 h2 Ichar msg[] = "This is a test"; </FONT></P><P><FONT color=#0000ff>/* create a file */
- s% x; `" W/ T# v( p4 Z6 J$ g- Hnul = open("DUMMY.FIL", O_CREAT | O_RDWR, ( Z7 O) {4 ^1 i( H# p+ v1 o, u, V9 S
S_IREAD | S_IWRITE); </FONT></P><P><FONT color=#0000ff>/* create a duplicate handle for standard
4 E: {; Z2 J) Q% E' Goutput */
9 `5 F' l* O: j2 r, T2 P' koldstdout = dup(STDOUT);
1 _: u7 f) I7 y$ _. g% C/* ; S9 S) M6 I! Y/ L% g' C. l' [
redirect standard output to DUMMY.FIL 0 `3 }* a: |7 K! K( T# R, |* r
by duplicating the file handle onto the 7 E4 h% p# ]5 l5 s, [
file handle for standard output. 8 I0 J9 n- c# K) ~
*/
5 [! S' U* F' e# V6 n3 `2 ~0 ]0 w" ndup2(nul, STDOUT); </FONT></P><P><FONT color=#0000ff>/* close the handle for DUMMY.FIL */
, @$ q# M1 I& h8 X6 @( o9 l0 a# nclose(nul); </FONT></P><P><FONT color=#0000ff>/* will be redirected into DUMMY.FIL */
5 n' j3 z& g& m/ _8 V9 Bwrite(STDOUT, msg, strlen(msg)); </FONT></P><P><FONT color=#0000ff>/* restore original standard output 0 C& L5 `( D5 M' O C. B# q
handle */ 9 X1 R2 N) F/ u* U
dup2(oldstdout, STDOUT); </FONT></P><P><FONT color=#0000ff>/* close duplicate handle for STDOUT */ & I, ]8 u) Q# j0 Z
close(oldstdout); </FONT></P><P><FONT color=#0000ff>return 0;
+ x4 T8 p; c* Q$ F- J} " v" v9 I7 Z/ y/ B! o9 T( {5 T
</FONT></P> |
|