📄 王大刚--c语言编程宝典--d.htm
字号:
<P>int main(void) <BR>{ <BR> x = div(10,3); <BR>
printf("10 div 3 = %d remainder %d\n", x.quot, x.rem); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: dosexterr <BR>功 能: 获取扩展DOS错误信息 <BR>用 法: int
dosexterr(struct DOSERR *dblkp); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <dos.h> <BR>
<P>int main(void) <BR>{ <BR> FILE *fp; <BR> struct
DOSERROR info; <BR>
<P> fp = fopen("perror.dat","r"); <BR> if (!fp)
perror("Unable to open file for <BR> reading");
<BR> dosexterr(&info); <BR>
<P> printf("Extended DOS error \ <BR>
information:\n"); <BR> printf(" Extended error: \
<BR> %d\n",info.exterror); <BR>
printf("
Class: \ <BR> %x\n",info.class); <BR>
printf("
Action: \ <BR> %x\n",info.action); <BR>
printf(" Error Locus: \ <BR>
%x\n",info.locus); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: dostounix <BR>功 能: 转换日期和时间为UNIX时间格式 <BR>用 法: long
dostounix(struct date *dateptr, struct time *timeptr); <BR>程序例: <BR>
<P> #include <time.h> <BR> #include <stddef.h>
<BR> #include <dos.h> <BR> #include <stdio.h> <BR>
<P> int main(void) <BR> { <BR> time_t t;
<BR> struct time d_time; <BR> struct
date d_date; <BR> struct tm *local; <BR>
<P> getdate(&d_date); <BR>
gettime(&d_time); <BR>
<P> t = dostounix(&d_date, &d_time);
<BR> local = localtime(&t); <BR>
printf("Time and Date: %s\n", \ <BR> asctime(local));
<BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: drawpoly <BR>功 能: 画多边形 <BR>用 法: void far drawpoly(int
numpoints, int far *polypoints); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int maxx, maxy; <BR>
<P> /* our polygon array */ <BR> int poly[10];
<BR>
<P> /* initialize graphics and local
<BR> variables */ <BR>
initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
<BR> /* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n", \
<BR> grapherrormsg(errorcode));
<BR> printf("Press any key to halt:");
<BR> getch(); <BR> /* terminate
with an error code */ <BR> exit(1);
<BR> } <BR>
<P> maxx = getmaxx(); <BR> maxy = getmaxy(); <BR>
<P> poly[0] = 20; /*
1st vertext */ <BR> poly[1] = maxy / 2; <BR>
<P> poly[2] = maxx - 20; /* 2nd */ <BR> poly[3] =
20; <BR>
<P> poly[4] = maxx - 50; /* 3rd */ <BR> poly[5] =
maxy - 20; <BR>
<P> poly[6] = maxx / 2; /* 4th */ <BR>
poly[7] = maxy / 2; <BR>/* <BR> drawpoly doesn't automatically
close <BR> the polygon, so we close it. <BR>*/
<BR> poly[8] = poly[0]; <BR> poly[9] = poly[1];
<BR>
<P> /* draw the polygon */ <BR> drawpoly(5, poly);
<BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: dup <BR>功 能: 复制一个文件句柄 <BR>用 法: int dup(int handle);
<BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>#include
<conio.h> <BR>#include <io.h> <BR>
<P>void flush(FILE *stream); <BR>
<P>int main(void) <BR>{ <BR> FILE *fp; <BR> char
msg[] = "This is a test"; <BR>
<P> /* create a file */ <BR> fp =
fopen("DUMMY.FIL", "w"); <BR>
<P> /* write some data to the file */ <BR>
fwrite(msg, strlen(msg), 1, fp); <BR>
<P> clrscr(); <BR> printf("Press any key to flush
\ <BR> DUMMY.FIL:"); <BR> getch(); <BR>
<P> /* flush the data to DUMMY.FIL without
<BR> closing it */ <BR>
flush(fp); <BR>
<P> printf("\nFile was flushed, Press any \ <BR>
key to quit:"); <BR> getch(); <BR> return 0; <BR>}
<BR>
<P>void flush(FILE *stream) <BR>{ <BR> int duphandle; <BR>
<P> /* flush TC's internal buffer */ <BR>
fflush(stream); <BR>
<P> /* make a duplicate file handle */ <BR>
duphandle = dup(fileno(stream)); <BR>
<P> /* close the duplicate handle to flush the
<BR> DOS buffer */ <BR>
close(duphandle); <BR>} <BR> <BR> <BR>
<P>函数名: dup2 <BR>功 能: 复制文件句柄 <BR>用 法: int dup2(int oldhandle,
int newhandle); <BR>程序例: <BR>
<P>#include <sys\stat.h> <BR>#include <string.h> <BR>#include
<fcntl.h> <BR>#include <io.h> <BR>
<P>int main(void) <BR>{ <BR> #define STDOUT 1 <BR>
<P> int nul, oldstdout; <BR> char msg[] = "This is
a test"; <BR>
<P> /* create a file */ <BR> nul =
open("DUMMY.FIL", O_CREAT | O_RDWR, <BR>
S_IREAD | S_IWRITE); <BR>
<P> /* create a duplicate handle for standard
<BR> output */ <BR> oldstdout =
dup(STDOUT); <BR> /* <BR>
redirect standard output to DUMMY.FIL <BR>
by duplicating the file handle onto the <BR>
file handle for standard output. <BR> */ <BR>
dup2(nul, STDOUT); <BR>
<P> /* close the handle for DUMMY.FIL */ <BR>
close(nul); <BR>
<P> /* will be redirected into DUMMY.FIL */ <BR>
write(STDOUT, msg, strlen(msg)); <BR>
<P> /* restore original standard output
<BR> handle */ <BR>
dup2(oldstdout, STDOUT); <BR>
<P> /* close duplicate handle for STDOUT */ <BR>
close(oldstdout); <BR>
<P> return 0; <BR>} <BR> <BR>
<HR width="94%" color=#ee9b73 SIZE=1>
</TD>
<TD class=tt3 vAlign=bottom width="8%" bgColor=#e0e0e0><STRONG><A
href="http://www.hjflying.8u8.com/cl/024.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/022.htm">前一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/index.html">回目录</A><BR><A
href="http://www.hjflying.8u8.com/index.htm">回首页</A><BR></STRONG></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -