> 文章列表 > C语言函数大全--g开头的函数(下)

C语言函数大全--g开头的函数(下)

C语言函数大全--g开头的函数(下)

C语言函数大全

本篇介绍C语言函数大全–g开头的函数(下)

17. getmodename

17.1 函数说明

函数声明 函数功能
char * getmodename(int mode_name); 获取指定的图形模式名

17.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main(void)
{int gdriver = DETECT, gmode, errorcode;int midx, midy, mode;char numname[80], modename[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;mode = getgraphmode();sprintf(numname, "%d is the current mode number.", mode);sprintf(modename, "%s is the current graphics mode.", getmodename(mode));settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, numname);outtextxy(midx, midy+2*textheight("W"), modename);getch();closegraph();return 0;
}

17.3 运行结果

C语言函数大全--g开头的函数(下)

18. getmoderange

18.1 函数说明

函数声明 函数功能
void getmoderange(int graphdriver, int *lomode, int *himode); 获取给定图形驱动程序的模式范围

18.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main()
{int gdriver = DETECT, gmode, errorcode;int midx, midy, low, high;char mrange[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;getmoderange(gdriver, &low, &high);sprintf(mrange, "This driver supports modes %d~%d", low, high);settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, mrange);getch();closegraph();return 0;
}

18.3 运行结果

C语言函数大全--g开头的函数(下)

19. getpalette

19.1 函数说明

函数声明 函数功能
void getpalette(struct palettetype *palette); 获取有关当前调色板的信息

19.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main()
{int gdriver = DETECT, gmode, errorcode;struct palettetype pal;char psize[80], pval[20];int i, ht;int y = 10;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}getpalette(&pal);sprintf(psize, "The palette has %d modifiable entries.", pal.size);outtextxy(0, y, psize);if (pal.size != 0){ht = textheight("W");y += 2*ht;outtextxy(0, y, "Here are the current values:");y += 2*ht;for (i=0; i < pal.size; i++, y+=ht){sprintf(pval, "palette[%02d]: 0x%02X", i, pal.colors[i]);outtextxy(0, y, pval);}}getch();closegraph();return 0;
}

19.3 运行结果

C语言函数大全--g开头的函数(下)

20. getpixel

20.1 函数说明

函数声明 函数功能
int getpixel(int x, int y); 获取得指定像素的颜色

20.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>#define PIXEL_COUNT 1000
#define DELAY_TIME  100int main(void)
{int gdriver = DETECT, gmode, errorcode;int midx, midy, x, y, color, maxx, maxy, maxcolor;char mPixel[50];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;maxx = getmaxx() + 1;maxy = getmaxy() + 1;maxcolor = getmaxcolor() + 1;while (!kbhit()){srand((unsigned)time(NULL));x = rand() % maxx;y = rand() % maxy;color = rand() % maxcolor;putpixel(x, y, color);sprintf(mPixel, "color of pixel at (%d,%d) = %d", x, y, getpixel(x, y));settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, mPixel);delay(DELAY_TIME);cleardevice();}getch();closegraph();return 0;
}

20.3 运行结果

C语言函数大全--g开头的函数(下)

21. gets

21.1 函数说明

函数声明 函数功能
从标准输入流中读取字符串,直至遇到到换行符或EOF时停止,并将读取的结果存放在 buffer 指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为 '\\0' 空字符,并由此来结束字符串。

注意: gets 函数可以无限读取,易发生溢出。如果溢出,多出来的字符将被写入到堆栈中,这就覆盖了堆栈原先的内容,破坏一个或多个不相关变量的值。

21.2 演示示例

#include <stdio.h>int main()
{char string[80];printf("Input a string:");gets(string);printf("The string input was: %s\\n", string);return 0;
}

21.3 运行结果

C语言函数大全--g开头的函数(下)

22. gettextsettings

22.1 函数说明

函数声明 函数功能
void gettextsettings(struct textsettingstype *textinfo); 获取有关当前图形文本字体的信息

22.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>// 文本字体
char *font[] = { "DEFAULT_FONT","TRIPLEX_FONT","SMALL_FONT","SANS_SERIF_FONT","GOTHIC_FONT"};// 文本方向
char *dir[] = { "HORIZ_DIR", "VERT_DIR" };// 文本水平对齐方式
char *hjust[] = { "LEFT_TEXT", "CENTER_TEXT", "RIGHT_TEXT" };// 文本垂直对齐方式
char *vjust[] = { "BOTTOM_TEXT", "CENTER_TEXT", "TOP_TEXT" };int main()
{int gdriver = DETECT, gmode, errorcode;struct textsettingstype textinfo;int midx, midy, ht;char fontstr[80], dirstr[80], sizestr[80];char hjuststr[80], vjuststr[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;// 获取有关当前图形文本字体的信息gettextsettings(&textinfo);sprintf(fontstr, "%s is the text style.", font[textinfo.font]);sprintf(dirstr, "%s is the text direction.", dir[textinfo.direction]);sprintf(sizestr, "%d is the text size.", textinfo.charsize);sprintf(hjuststr, "%s is the horizontal justification.", hjust[textinfo.horiz]);sprintf(vjuststr, "%s is the vertical justification.", vjust[textinfo.vert]);ht = textheight("W");settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, fontstr);outtextxy(midx, midy+2*ht, dirstr);outtextxy(midx, midy+4*ht, sizestr);outtextxy(midx, midy+6*ht, hjuststr);outtextxy(midx, midy+8*ht, vjuststr);getch();closegraph();return 0;
}

22.3 运行结果

C语言函数大全--g开头的函数(下)

23. getviewsettings

23.1 函数说明

函数声明 函数功能
void getviewsettings(struct viewporttype *viewport); 获取有关当前视区的信息

23.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>char *clip[] = { "OFF", "ON" };int main()
{int gdriver = DETECT, gmode, errorcode;struct viewporttype viewinfo;int midx, midy, ht;char topstr[80], botstr[80], clipstr[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;// 获取有关当前视区的信息getviewsettings(&viewinfo);sprintf(topstr, "(%d, %d) is the upper left viewport corner.", viewinfo.left, viewinfo.top);sprintf(botstr, "(%d, %d) is the lower right viewport corner.", viewinfo.right, viewinfo.bottom);sprintf(clipstr, "Clipping is turned %s.", clip[viewinfo.clip]);settextjustify(CENTER_TEXT, CENTER_TEXT);ht = textheight("W");outtextxy(midx, midy, topstr);outtextxy(midx, midy+2*ht, botstr);outtextxy(midx, midy+4*ht, clipstr);getch();closegraph();return 0;
}

23.3 运行结果

C语言函数大全--g开头的函数(下)

24. getw

24.1 函数说明

函数声明 函数功能
int getw(FILE *strem); 从 stream 所指向文件读取下一个整数

24.2 演示示例

#include <stdio.h>
#include <stdlib.h>#define FNAME "test.txt"int main(void)
{FILE *fp;int word;fp = fopen(FNAME, "wb");if (fp == NULL){printf("Error opening file %s\\n", FNAME);exit(1);}word = 94;putw(word,fp);if (ferror(fp))printf("Error writing to file\\n");elseprintf("Successful write\\n");fclose(fp);fp = fopen(FNAME, "rb");if (fp == NULL){printf("Error opening file %s\\n", FNAME);exit(1);}word = getw(fp);if (ferror(fp))printf("Error reading file\\n");elseprintf("Successful read: word = %d\\n", word);fclose(fp);unlink(FNAME);return 0;
}

24.3 运行结果

C语言函数大全--g开头的函数(下)

25. getx,gety

25.1 函数说明

函数声明 函数功能
int getx(void); 获取当前图形位置的 x 坐标
获取当前图形位置的 y 坐标

25.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main(void)
{int gdriver = DETECT, gmode, errorcode;char msg[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}moveto(getmaxx() / 2, getmaxy() / 2);sprintf(msg, "<-(%d, %d) is the here.", getx(), gety());outtext(msg);getch();closegraph();return 0;
}

25.3 运行结果

C语言函数大全--g开头的函数(下)

26. gmtime

26.1 函数说明

函数声明 函数功能
struct tm *gmtime(long *clock); 把日期和时间转换为格林尼治标准时间(GMT)

26.2 演示示例

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>// 太平洋标准时间和夏令时
char *tzstr = "TZ=PST8PDT";int main(void)
{time_t t;struct tm *gmt, *area;putenv(tzstr); // 用来改变或增加环境变量的内容tzset(); // UNIX时间兼容函数// 获取当前的系统时间,其值表示从协调世界时(Coordinated Universal Time)// 1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。t = time(NULL); area = localtime(&t); // 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间// asctime 把timeptr指向的tm结构体中储存的时间转换为字符串printf("Local time is: %s", asctime(area));// 把日期和时间转换为格林尼治标准时间(GMT)gmt = gmtime(&t);printf("GMT is:        %s", asctime(gmt));return 0;
}

26.3 运行结果

C语言函数大全--g开头的函数(下)

27. graphdefaults

27.1 函数说明

函数声明 函数功能
void graphdefaults(void); 将所有图形设置复位为它们的缺省值

27.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>int main(void)
{int gdriver = DETECT, gmode, errorcode;int maxx, maxy;initgraph(&gdriver, &gmode, "c:\\\\bor\\\\Borland\\\\bgi");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}maxx = getmaxx();maxy = getmaxy();setlinestyle(DOTTED_LINE, 0, 3);line(0, 0, maxx, maxy);outtextxy(maxx/2, maxy/3, "Before default values are restored.");getch();// 将所有图形设置复位为它们的缺省值graphdefaults();cleardevice();line(0, 0, maxx, maxy);outtextxy(maxx/2, maxy/3, "After restoring default values.");getch();closegraph();return 0;
}

27.3 运行结果

C语言函数大全--g开头的函数(下)

28. grapherrormsg

28.1 函数说明

函数声明 函数功能
char * grapherrormsg(int errorcode); 返回一个错误信息串的指针

28.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>#define NONSENSE -50int main(void)
{// FORCE AN ERROR TO OCCURint gdriver = NONSENSE, gmode, errorcode;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}line(0, 0, getmaxx(), getmaxy());getch();closegraph();return 0;
}

28.3 运行结果

C语言函数大全--g开头的函数(下)

29. graphresult

29.1 函数说明

函数声明 函数功能
int graphresult(void); 返回最后一次不成功的图形操作的错误代码

29.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>int main(void)
{// request auto detectionint gdriver = DETECT, gmode, errorcode;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}line(0, 0, getmaxx(), getmaxy());getch();closegraph();return 0;
}

29.3 运行结果

C语言函数大全--g开头的函数(下)

30. getmaxwidth,getmaxheight

30.1 函数说明

函数声明 函数功能
int getmaxwidth(void); 获取屏幕的最大宽度
int getmaxheight(void); 获取屏幕的最大高度

30.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main()
{int gdriver = DETECT, gmode, errorcode;int midx, midy;char ch[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;sprintf(ch, "maxwidth = %d, maxheight = %d", getmaxwidth(), getmaxheight());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;
}

30.3 运行结果

C语言函数大全--g开头的函数(下)

31. getdisplaycolor

31.1 函数说明

函数声明 函数功能
int getdisplaycolor( int color ); 根据 color ,返回要显示的颜色值

注意: color = -1 , 则返回 WHITE = 15 的颜色值;color < - 1 或 color > 15,则输出一个8位整数。

31.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main()
{int gdriver = DETECT, gmode, errorcode;int midx, midy;char ch[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;sprintf(ch, "color = %d, displaycolor(-1) = %d, displaycolor(16) = %d", getcolor(), getdisplaycolor(-1), getdisplaycolor(16));settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;
}

31.3 运行结果

C语言函数大全--g开头的函数(下)

32. getwindowwidth,getwindowheight

32.1 函数说明

函数声明 函数功能
int getwindowwidth(); 获取图形界面窗口宽度
int getwindowheight(void); 获取图形界面窗口高度

32.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main()
{int gdriver = DETECT, gmode, errorcode;int midx, midy, low, high;char ch[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;sprintf(ch, "windowwidth = %d, windowheight = %d", getwindowwidth(), getwindowheight());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;
}

32.3 运行结果

C语言函数大全--g开头的函数(下)

33. getrefreshingbgi

33.1 函数说明

函数声明 函数功能
bool getrefreshingbgi(void); 获取刷新基础图形界面标识

33.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main()
{int gdriver = DETECT, gmode, errorcode;int midx, midy, low, high;char ch[80];initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}midx = getmaxx() / 2;midy = getmaxy() / 2;sprintf(ch, "refreshingbgi = %d", getrefreshingbgi());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;
}

33.3 运行结果

C语言函数大全--g开头的函数(下)

参考

  1. [API Reference Document]
  2. [gets]