C语言库函数大全及应用实例五

简介: [编程资料]C语言库函数大全及应用实例五 函数名: getcurdir 功 能: 取指定驱动器的当前目录 用 法: int getcurdir(int drive, char *direc); 程序例...
                                                img_bc31e47721e0c70a99a9112a5a2c8ee7.gif[编程资料]C语言库函数大全及应用实例五

函数名: getcurdir
功 能: 取指定驱动器的当前目录
用 法: int getcurdir(int drive, char *direc);
程序例: <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

#i nclude
#i nclude
#i nclude

char *current_directory(char *path)
{
strcpy(path, "X:\\"); /* fill string with form of response: X:\ */
path[0] = 'A' + getdisk(); /* replace X with current drive letter */
getcurdir(0, path+3); /* fill rest of string with current directory */
return(path);
}

int main(void)
{
char curdir[MAXPATH];

current_directory(curdir);
printf("The current directory is %s\n", curdir);

return 0;
}

函数名: getcwd
功 能: 取当前工作目录
用 法: char *getcwd(char *buf, int n);
程序例:

#i nclude
#i nclude

int main(void)
{
char buffer[MAXPATH];

getcwd(buffer, MAXPATH);
printf("The current directory is: %s\n", buffer);
return 0;
}

函数名: getdate
功 能: 取DOS日期
用 法: void getdate(struct *dateblk);
程序例:

#i nclude
#i nclude

int main(void)
{
struct date d;

getdate(&d);
printf("The current year is: %d\n",
d.da_year);
printf("The current day is: %d\n",
d.da_day);
printf("The current month is: %d\n",
d.da_mon);
return 0;
}

函数名: getdefaultpalette
功 能: 返回调色板定义结构
用 法: struct palettetype *far getdefaultpalette(void);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

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

/* structure for returning palette copy */
struct palettetype far *pal=(void *) 0;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n",
grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

setcolor(getmaxcolor());

/* return a pointer to the default palette */
pal = getdefaultpalette();

for (i=0; i<16; i++)
{
printf("colors[%d] = %d\n", i,
pal->colors[i]);
getch();
}

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

函数名: getdisk
功 能: 取当前磁盘驱动器号
用 法: int getdisk(void);
程序例:

#i nclude
#i nclude

int main(void)
{
int disk;

disk = getdisk() + 'A';
printf("The current drive is: %c\n",
disk);
return 0;
}

函数名: getdrivername
功 能: 返回指向包含当前图形驱动程序名字的字符串指针
用 法: char *getdrivename(void);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

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

/* stores the device driver name */
char *drivername;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n",
grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

setcolor(getmaxcolor());

/* get name of the device driver in use */
drivername = getdrivername();

/* for centering text on the screen */
settextjustify(CENTER_TEXT, CENTER_TEXT);

/* output the name of the driver */
outtextxy(getmaxx() / 2, getmaxy() / 2,
drivername);

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

函数名: getdta
功 能: 取磁盘传输地址
用 法: char far *getdta(void);
程序例:

#i nclude
#i nclude

int main(void)
{
char far *dta;

dta = getdta();
printf("The current disk transfer \
address is: %Fp\n", dta);
return 0;
}

函数名: getenv
功 能: 从环境中取字符串
用 法: char *getenv(char *envvar);
程序例:

#i nclude
#i nclude

int main(void)
{
char *s;

s=getenv("COMSPEC"); /* get the comspec environment parameter */
printf("Command processor: %s\n",s); /* display comspec parameter */

return 0;
}


函数名: getfat, getfatd
功 能: 取文件分配表信息
用 法: void getfat(int drive, struct fatinfo *fatblkp);
程序例:

#i nclude
#i nclude

int main(void)
{
struct fatinfo diskinfo;
int flag = 0;

printf("Please insert disk in drive A\n");
getchar();

getfat(1, &diskinfo);
/* get drive information */

printf("\nDrive A: is ");
switch((unsigned char) diskinfo.fi_fatid)
{
case 0xFD:
printf("360K low density\n");
break;

case 0xF9:
printf("1.2 Meg high density\n");
break;

default:
printf("unformatted\n");
flag = 1;
}

if (!flag)
{
printf(" sectors per cluster %5d\n",
diskinfo.fi_sclus);
printf(" number of clusters %5d\n",
diskinfo.fi_nclus);
printf(" bytes per sector %5d\n",
diskinfo.fi_bysec);
}

return 0;
}

函数名: getfillpattern
功 能: 将用户定义的填充模式拷贝到内存中
用 法: void far getfillpattern(char far *upattern);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};

/* 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();
exit(1); /* terminate with an error code */
}

maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());

/* select a user defined fill pattern */
setfillpattern(pattern, getmaxcolor());

/* fill the screen with the pattern */
bar(0, 0, maxx, maxy);

getch();

/* get the current user defined fill pattern */
getfillpattern(pattern);

/* alter the pattern we grabbed */
pattern[4] -= 1;
pattern[5] -= 3;
pattern[6] += 3;
pattern[7] -= 4;

/* select our new pattern */
setfillpattern(pattern, getmaxcolor());

/* fill the screen with the new pattern */
bar(0, 0, maxx, maxy);

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

函数名: getfillsettings
功 能: 取得有关当前填充模式和填充颜色的信息
用 法: void far getfillsettings(struct fillsettingstype far *fillinfo);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

/ the names of the fill styles supported */
char *fname[] = { "EMPTY_FILL",
"SOLID_FILL",
"LINE_FILL",
"LTSLASH_FILL",
"SLASH_FILL",
"BKSLASH_FILL",
"LTBKSLASH_FILL",
"HATCH_FILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDE_DOT_FILL",
"CLOSE_DOT_FILL",
"USER_FILL"
};

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct fillsettingstype fillinfo;
int midx, midy;
char patstr[40], colstr[40];

/* 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();
exit(1); /* terminate with an error code */
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;

/* get information about current fill pattern and color */
getfillsettings(&fillinfo);

/* convert fill information into strings */
sprintf(patstr, "%s is the fill style.", fname[fillinfo.pattern]);
sprintf(colstr, "%d is the fill color.", fillinfo.color);

/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, patstr);
outtextxy(midx, midy+2*textheight("W"), colstr);

/* clean up */
getch();
closegraph();
return 0;
}
函数名: getftime
功 能: 取文件日期和时间
用 法: int getftime(int handle, struct ftime *ftimep);
程序例:

#i nclude
#i nclude

int main(void)
{
FILE *stream;
struct ftime ft;

if ((stream = fopen("TEST.$$$",
"wt")) == NULL)
{
fprintf(stderr,
"Cannot open output file.\n");
return 1;
}
getftime(fileno(stream), &ft);
printf("File time: %u:%u:%u\n",
ft.ft_hour, ft.ft_min,
ft.ft_tsec * 2);
printf("File date: %u/%u/%u\n",
ft.ft_month, ft.ft_day,
ft.ft_year+1980);
fclose(stream);
return 0;
}

函数名: getgraphmode
功 能: 返回当前图形模式
用 法: int far getgraphmode(void);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, mode;
char numname[80], modename[80];

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n",
grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;

/* get mode number and name strings */
mode = getgraphmode();
sprintf(numname,
"%d is the current mode number.",
mode);
sprintf(modename,
"%s is the current graphics mode",
getmodename(mode));

/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, numname);
outtextxy(midx, midy+2*textheight("W"),
modename);

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

函数名: getftime
功 能: 取文件日期和时间
用 法: int getftime(int handle, struct ftime *ftimep);
程序例:

#i nclude
#i nclude

int main(void)
{
FILE *stream;
struct ftime ft;

if ((stream = fopen("TEST.$$$",
"wt")) == NULL)
{
fprintf(stderr,
"Cannot open output file.\n");
return 1;
}
getftime(fileno(stream), &ft);
printf("File time: %u:%u:%u\n",
ft.ft_hour, ft.ft_min,
ft.ft_tsec * 2);
printf("File date: %u/%u/%u\n",
ft.ft_month, ft.ft_day,
ft.ft_year+1980);
fclose(stream);
return 0;
}


函数名: getgraphmode
功 能: 返回当前图形模式
用 法: int far getgraphmode(void);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, mode;
char numname[80], modename[80];

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n",
grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

midx = getmaxx() / 2;
midy = getmaxy() / 2;

/* get mode number and name strings */
mode = getgraphmode();
sprintf(numname,
"%d is the current mode number.",
mode);
sprintf(modename,
"%s is the current graphics mode",
getmodename(mode));

/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, numname);
outtextxy(midx, midy+2*textheight("W"),
modename);

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

函数名: getimage
功 能: 将指定区域的一个位图存到主存中
用 法: void far getimage(int left, int top, int right, int bottom,
void far *bitmap);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

void save_screen(void far *buf[4]);
void restore_screen(void far *buf[4]);

int maxx, maxy;

int main(void)
{
int gdriver=DETECT, gmode, errorcode;
void far *ptr[4];

/* auto-detect the graphics driver and mode */
initgraph(&gdriver, &gmode, "");
errorcode = graphresult(); /* check for any errors */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
maxx = getmaxx();
maxy = getmaxy();

/* draw an image on the screen */
rectangle(0, 0, maxx, maxy);
line(0, 0, maxx, maxy);
line(0, maxy, maxx, 0);

save_screen(ptr); /* save the current screen */
getch(); /* pause screen */
cleardevice(); /* clear screen */
restore_screen(ptr); /* restore the screen */
getch(); /* pause screen */

closegraph();
return 0;
}

void save_screen(void far *buf[4])
{
unsigned size;
int ystart=0, yend, yincr, block;

yincr = (maxy+1) / 4;
yend = yincr;
size = imagesize(0, ystart, maxx, yend); /* get byte size of image */

for (block=0; block<=3; block++)
{
if ((buf[block] = farmalloc(size)) == NULL)
{
closegraph();
printf("Error: not enough heap space in save_screen().\n");
exit(1);
}

getimage(0, ystart, maxx, yend, buf[block]);
ystart = yend + 1;
yend += yincr + 1;
}
}

void save_screen(void far *buf[4])
{
unsigned size;
int ystart=0, yend, yincr, block;

yincr = (maxy+1) / 4;
yend = yincr;
size = imagesize(0, ystart, maxx, yend); /* get byte size of image */

for (block=0; block<=3; block++)
{
if ((buf[block] = farmalloc(size)) == NULL)
{
closegraph();
printf("Error: not enough heap space in save_screen().\n");
exit(1);
}

getimage(0, ystart, maxx, yend, buf[block]);
ystart = yend + 1;
yend += yincr + 1;
}
}

void restore_screen(void far *buf[4])
{
int ystart=0, yend, yincr, block;

yincr = (maxy+1) / 4;
yend = yincr;

for (block=0; block<=3; block++)
{
putimage(0, ystart, buf[block], COPY_PUT);
farfree(buf[block]);
ystart = yend + 1;
yend += yincr + 1;
}
}

目录
相关文章
|
5天前
|
存储 编译器 C语言
C语言:字符函数 & 字符串函数 & 内存函数
C语言:字符函数 & 字符串函数 & 内存函数
12 2
|
7天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
25 5
|
8天前
|
机器学习/深度学习 算法 数据挖掘
【C 言专栏】C 语言与机器学习的应用
【5月更文挑战第6天】C语言在机器学习中扮演关键角色,以其高效性、灵活性和可移植性实现底层算法、嵌入式系统和高性能计算。在神经网络、决策树和聚类算法等领域的实现中不可或缺。C语言被用于TensorFlow和OpenCV等知名库的底层,常与C++、Python结合使用。尽管面临开发难度和适应新算法的挑战,但C语言在机器学习领域的价值和潜力将持续展现,为科技进步贡献力量。
【C 言专栏】C 语言与机器学习的应用
|
8天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
15 3
|
10天前
|
存储 缓存 算法
【C 言专栏】C 语言中的数据结构应用
【5月更文挑战第4天】本文探讨了C语言中的核心数据结构,包括数组、链表(单链表和双链表)、栈、队列、二叉树(如二叉搜索树和二叉堆)以及图结构。这些数据结构在程序设计中扮演着关键角色,如数组的快速访问、链表的动态管理、栈和队列的处理流程控制、树和图的复杂关系表示。理解并选择适当的数据结构可优化程序性能,而内存管理和算法优化则进一步提升效率。通过案例分析和展望未来发展趋势,本文旨在帮助读者深化对C语言数据结构的理解和应用。
【C 言专栏】C 语言中的数据结构应用
|
13天前
|
缓存 安全 编译器
【C 言专栏】C 语言函数的高效编程技巧
【5月更文挑战第1天】本文探讨了C语言中函数的高效编程技巧,包括函数的定义与作用(如代码复用和提高可读性)、设计原则(单一职责和接口简洁)、参数传递方式(值传递、指针传递和引用传递)、返回值管理、调用约定、嵌套与递归调用,以及函数优化技巧和常见错误避免。掌握这些技巧能提升C语言代码的质量和效率。
【C 言专栏】C 语言函数的高效编程技巧
|
13天前
|
机器学习/深度学习 算法 C语言
【C言专栏】递归算法在 C 语言中的应用
【4月更文挑战第30天】本文介绍了递归算法在C语言中的应用,包括基本概念(通过调用自身解决子问题)、特点(调用自身、终止条件、栈空间)和实现步骤(定义递归函数、分解问题、设置终止条件、组合解)。文中通过阶乘计算和斐波那契数列两个案例展示了递归的使用,强调了递归可能导致的栈溢出问题及优化需求。学习递归有助于理解和应用“分而治之”策略。
|
14天前
|
存储 算法 程序员
【C言专栏】C 语言结构体的应用与实践
【4月更文挑战第30天】C语言中的结构体是自定义数据类型的关键,它组合不同类型的數據以创建新类型,尤其适合处理复杂对象如学生信息。通过定义结构体如`struct Student`,包含名字、学号和成绩,可以方便地实例化和访问成员。结构体在链表实现、函数参数传递和数组中都有广泛应用,如表示链表节点和处理批量数据。理解并熟练运用结构体对于C语言编程至关重要,能提升代码效率和可读性。
|
4月前
|
存储 C语言
C 语言函数完全指南:创建、调用、参数传递、返回值解析
函数是一段代码块,只有在被调用时才会运行。 您可以将数据(称为参数)传递给函数。 函数用于执行某些操作,它们对于重用代码很重要:定义一次代码,并多次使用。
96 3
|
21天前
|
存储 C语言
C语言函数的返回值
C语言函数的返回值
9 0