
函数声明 | 函数功能 |
|---|---|
| 通过检测硬件确定图形驱动程序和模式 |
| 计算两个时刻之间的时间差 |
| 屏蔽中断 |
| 将两个整数相除, 返回商和余数 |
| 画多边形 |
| 复制文件描述符;若成功为新的文件描述,若出错为-1 |
| 复制文件描述符;若成功为新的文件描述,若出错为-1。 |
函数声明 | 函数功能 |
|---|---|
| 通过检测硬件确定图形驱动程序和模式 |
#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)
{
int gdriver, gmode, errorcode;
detectgraph(&gdriver, &gmode);
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
printf("You have [%s] video display card.\n", dname[gdriver]);
printf("Press any key to halt:");
getch();
return 0;
}
函数声明 | 函数功能 |
|---|---|
| 计算两个时刻之间的时间差 |
#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;
}在上述的示例代码中,
time_t 类型的变量 first 和 second;time(NULL) 函数获取当前的系统时间,并赋值给 first;getchar()函数等待用户输入,模拟延时的功能;time(NULL) 函数获取当前的系统时间,并赋值给 second;difftime() 函数计算 first 和 second 之间的时间差【单位:秒】
函数声明 | 函数功能 |
|---|---|
| 屏蔽中断 |
// 中断服务示例
#include <stdio.h>
#include <dos.h>
#include <conio.h>
// 定义一个宏INTR,代表时钟中断的十六进制向量值
#define INTR 0X1C
// 声明一个函数指针oldhandler,该指针指向一个没有参数且返回void的函数。
// 这种函数通常用于中断处理程序
void interrupt (*oldhandler)(void);
int count=0;
void interrupt handler(void)
{
// 禁用其他中断,确保此中断处理程序执行完毕前不再响应其他中断
disable();
count++;
// 启用其他中断
enable();
// 调用原先的中断处理程序
(*oldhandler)();
}
int main(void)
{
// 获取时钟中断的原始处理程序,并将其存储在oldhandler指向的函数中
oldhandler = getvect(INTR);
// 将时钟中断的处理程序设置为handler函数
setvect(INTR, handler);
while (count < 20)
printf("count is %d\n",count);
// 恢复时钟中断的原始处理程序
setvect(INTR, oldhandler);
return 0;
}注意: 这个程序可能无法在现代操作系统上直接运行,因为其中的一些函数(如disable()、enable()、getvect() 和 setvect())是特定于 DOS 的。如果你想在现代操作系统(如 Linux 或 Windows)上运行这个程序,你可能需要使用更现代的方法来处理中断或使用 DOS 模拟器。
函数声明 | 函数功能 |
|---|---|
| 将两个整数相除, 返回商和余数 |
#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;
}
函数声明 | 函数功能 |
|---|---|
| 画多边形 |
参数说明:
numpoints:多边形顶点的数量polypoints:一个整数数组,包含多边形的各个顶点的坐标。#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;
}
函数声明 | 函数功能 |
|---|---|
| 复制文件描述符;若成功为新的文件描述,若出错为-1 |
dup 返回的新文件描述符一定是当前可用文件描述中的最小数值。
#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";
fp = fopen("STU.FIL", "w");
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(fp);
printf("\nFile was flushed, Press any key to quit:");
getchar();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
fflush(stream);
duphandle = dup(fileno(stream));
printf("duplicate file hanlde : %d", duphandle);
close(duphandle);
}上述代码可以简单总结如下:
"STU.FIL"的文件,并以写入模式打开。"This is a test"写入该文件。"temp.txt" 的文件,并获取其文件句柄。"STU.FIL" 文件。flush函数来刷新文件缓冲区。fflush函数来刷新传入的文件流的缓冲区;dup函数复制文件描述符,并将其存储在duphandle变量中;
函数声明 | 函数功能 |
|---|---|
| 复制文件描述符;若成功为新的文件描述,若出错为-1。 |
dup2 可以用 newhandle 参数指定新的描述符数值。如果 newhandle 已经打开,则先关闭。若 oldhandle = newhandle,则 dup2 返回 newhandle,而不关闭它。
#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";
handle = open("STU.FIL", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
printf("open file handle : %d\n", handle);
oldstdout = dup(STDOUT);
printf("dup file handle : %d", oldstdout);
dup2(handle, STDOUT);
close(handle);
write(STDOUT, msg, strlen(msg));
dup2(oldstdout, STDOUT);
close(oldstdout);
return 0;
}上述代码简单分析如下:
STDOUT,其值为 1,表示标准输出的文件描述符;handle 和 oldstdout,以及字符数组 msg,用于存储要写入文件的字符串;open 函数打开名为 "STU.FIL" 的文件,以创建和读写模式(O_CREAT | O_RDWR)打开,并设置文件权限为可读可写(S_IREAD | S_IWRITE);将返回的文件描述符赋值给 handle,并打印出来;dup 函数备份当前的标准输出(STDOUT),将备份的文件描述符赋值给 oldstdout,并打印出来;dup2 函数将标准输出重定向到 handle 指向的文件,即将后续的输出内容写入到 "STU.FIL" 文件中;handle 指向的文件句柄;write 函数将 msg 字符串写入到标准输出(此时已经重定向到文件),长度为字符串的长度;dup2 函数将标准输出恢复到备份的文件描述符 oldstdout,即将后续的输出内容输出到屏幕上。oldstdout 指向的文件句柄。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。