void detectgraph(int *graphdriver, int *graphmode);
通过检测硬件确定图形驱动程序和模式
1.2 演示示例
#include<graphics.h>#include<stdlib.h>#include<stdio.h>#include<conio.h>/* names of the various cards supported */char*dname[]={"requests detection","a CGA","an MCGA","an EGA","a 64K EGA","a monochrome EGA","an IBM 8514","a Hercules monochrome","an AT&T 6300 PC","a VGA","an IBM 3270 PC"};intmain(void){/* returns detected hardware info. */int gdriver, gmode, errorcode;/* detect graphics hardware available */detectgraph(&gdriver,&gmode);/* initialize graphics and local variables */initgraph(&gdriver,&gmode,"");/* read result of detectgraph call */
errorcode =graphresult();if(errorcode != grOk)/* an error occurred */{printf("Graphics error: %s\n",grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);/* terminate with an error code */}/* display the information detected */printf("You have [%s] video display card.\n", dname[gdriver]);printf("Press any key to halt:");getch();return0;}
1.3 运行结果
2. difftime
2.1 函数说明
函数声明
函数功能
double difftime(time_t time2, time_t time1);
计算两个时刻之间的时间差
2.2 演示示例
#include<stdio.h>#include<time.h>intmain(void){time_t first, second;// time_t 相当于 long
first =time(NULL);// Gets system time getchar();
second =time(NULL);// Gets system time again printf("The difference is: %lf seconds\n",difftime(second, first));return0;}
2.3 运行结果
3. div
3.1 函数说明
函数声明
函数功能
div_t (int number, int denom);
将两个整数相除, 返回商和余数
3.2 演示示例
#include<stdio.h>#include<math.h>intmain(void){div_t x =div(10,3);// 商 和 余数printf("10 div 3 = %d remainder %d\n", x.quot, x.rem);return0;}
3.3 运行结果
4. drawpoly
4.1 函数说明
函数声明
函数功能
void drawpoly(int numpoints, int *polypoints);
画多边形
4.2 演示示例
#include<graphics.h>#include<stdio.h>intmain(void){// request auto detectionint gdriver = DETECT, gmode, errorcode;int maxx, maxy;// our polygon arrayint poly[10];// initialize graphics and local variablesinitgraph(&gdriver,&gmode,"");// read result of initialization
errorcode =graphresult();if(errorcode != grOk)// an error occurred{printf("Graphics error: %s\n",grapherrormsg(errorcode));printf("Press any key to halt:");getch();// terminate with an error codeexit(1);}
maxx =getmaxx();
maxy =getmaxy();
poly[0]=20;
poly[1]= maxy /2;
poly[2]= maxx -20;
poly[3]=20;
poly[4]= maxx -50;
poly[5]= maxy -20;
poly[6]= maxx /2;
poly[7]= maxy /2;// drawpoly doesn't automatically close the polygon, so we close it.
poly[8]= poly[0];
poly[9]= poly[1];// draw the polygondrawpoly(5, poly);// clean upgetch();closegraph();return0;}
4.3 运行结果
5. dup
5.1 函数说明
函数声明
函数功能
int dup(int handle);
复制文件描述符;若成功为新的文件描述,若出错为-1
dup 返回的新文件描述符一定是当前可用文件描述中的最小数值。
5.2 演示示例
#include<sys/types.h>#include<sys/stat.h>#include<string.h>#include<stdio.h>#include<conio.h>#include<fcntl.h>#include<io.h>voidflush(FILE *stream);intmain(void){
FILE *fp;char msg[]="This is a test";/* create a file */
fp =fopen("STU.FIL","w");/* write some data to the file */fwrite(msg,strlen(msg),1, fp);int handle;
handle =open("temp.txt", _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE);printf("file hanlde : %d\n", handle);printf("Press any key to flush STU.FIL:");getchar();/* flush the data to STU.FIL without closing it */flush(fp);printf("\nFile was flushed, Press any key to quit:");getchar();return0;}voidflush(FILE *stream){int duphandle;/* flush TC's internal buffer */fflush(stream);/* make a duplicate file handle */
duphandle =dup(fileno(stream));printf("duplicate file hanlde : %d", duphandle);/* close the duplicate handle to flush the DOS buffer */close(duphandle);}
#include<sys\stat.h>#include<stdio.h>#include<string.h>#include<fcntl.h>#include<io.h>intmain(void){#defineSTDOUT1int handle, oldstdout;char msg[]="This is a test1";/* create a file */
handle =open("STU.FIL", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);printf("open file handle : %d\n", handle);/* create a duplicate handle for standard output */
oldstdout =dup(STDOUT);printf("dup file handle : %d", oldstdout);/*
redirect standard output to STU.FIL by duplicating the file
handle onto the file handle for standard output.
*/dup2(handle, STDOUT);/* close the handle for STU.FIL */close(handle);/* will be redirected into STU.FIL */write(STDOUT, msg,strlen(msg));/* restore original standard output handle */dup2(oldstdout, STDOUT);/* close duplicate handle for STDOUT */close(oldstdout);return0;}