当前位置 : 主页 > 编程语言 > c语言 >

C语言函数大全--d开头的函数

来源:互联网 收集:自由互联 发布时间:2023-09-07
C语言函数大全 本篇介绍C语言函数大全--d开头的函数 1. detectgraph 1.1 函数说明 函数声明 函数功能 void detectgraph(int *graphdriver, int *graphmode); 通过检测硬件确定图形驱动程序和模式 1.2 演示

C语言函数大全

本篇介绍C语言函数大全--d开头的函数

1. detectgraph

1.1 函数说明
函数声明 函数功能 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"
};

int main(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();
	return 0;
}

1.3 运行结果

在这里插入图片描述

2. difftime

2.1 函数说明
函数声明 函数功能 double difftime(time_t time2, time_t time1); 计算两个时刻之间的时间差
2.2 演示示例
#include <stdio.h>
#include <time.h>

int main(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));
    return 0;
}
2.3 运行结果

在这里插入图片描述

3. div

3.1 函数说明
函数声明 函数功能 div_t (int number, int denom); 将两个整数相除, 返回商和余数
3.2 演示示例
#include <stdio.h>
#include <math.h>

int main(void)
{
   div_t x = div(10,3);
   // 商 和 余数
   printf("10 div 3 = %d remainder %d\n", x.quot, x.rem);
   return 0;
}
3.3 运行结果

在这里插入图片描述

4. drawpoly

4.1 函数说明
函数声明 函数功能 void drawpoly(int numpoints, int *polypoints); 画多边形
4.2 演示示例
#include <graphics.h>
#include <stdio.h>

int main(void)
{
	// request auto detection
	int gdriver = DETECT, gmode, errorcode;
	int maxx, maxy;

	// our polygon array
	int poly[10];

	// initialize graphics and local variables
	initgraph(&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 code
        exit(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 polygon
	drawpoly(5, poly);

	// clean up
	getch();
	closegraph();
	return 0;
}

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>

void flush(FILE *stream);

int main(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();
    return 0;
}

void flush(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);
}
5.3 运行结果

在这里插入图片描述

6. dup2

6.1 函数说明
函数声明 函数功能 int dup2(int oldhandle, int newhandle); 复制文件描述符;若成功为新的文件描述,若出错为-1。

dup2 可以用 newhandle 参数指定新的描述符数值。如果 newhandle 已经打开,则先关闭。若 oldhandle = newhandle,则 dup2 返回 newhandle,而不关闭它。

6.2 演示示例
#include <sys\stat.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>

int main(void)
{
    #define STDOUT 1

    int 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);

    return 0;
}
6.3 运行结果

在这里插入图片描述

参考

  1. [API Reference Document]
上一篇:C++函数高级
下一篇:没有了
网友评论