📄 王大刚--c语言编程宝典--s.htm
字号:
<P>int main(void) <BR>{ <BR> struct date reset;
<BR> struct date save_date; <BR>
<P> getdate(&save_date); <BR> printf("Original
date:\n"); <BR> system("date"); <BR>
<P> reset.da_year = 2001; <BR> reset.da_day = 1;
<BR> reset.da_mon = 1; <BR> setdate(&reset);
<BR>
<P> printf("Date after setting:\n"); <BR>
system("date"); <BR>
<P> setdate(&save_date); <BR> printf("Back to
original date:\n"); <BR> system("date"); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setdisk <BR>功 能: 设置当前磁盘驱动器 <BR>用 法: int setdisk(int
drive); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <dir.h> <BR>
<P>int main(void) <BR>{ <BR> int save, disk, disks; <BR>
<P> /* save original drive */ <BR> save =
getdisk(); <BR>
<P> /* print number of logic drives */ <BR> disks
= setdisk(save); <BR> printf("%d logical drives on the
system\n\n", disks); <BR>
<P> /* print the drive letters available */ <BR>
printf("Available drives:\n"); <BR> for (disk = 0;disk <
26;++disk) <BR> { <BR>
setdisk(disk); <BR> if (disk == getdisk())
<BR> printf("%c: drive is
available\n", disk + 'a'); <BR> } <BR>
setdisk(save); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setdta <BR>功 能: 设置磁盘传输区地址 <BR>用 法: void setdta(char
far *dta); <BR>程序例: <BR>
<P>#include <process.h> <BR>#include <string.h> <BR>#include
<stdio.h> <BR>#include <dos.h> <BR>
<P>int main(void) <BR>{ <BR> char line[80], far *save_dta;
<BR> char buffer[256] = "SETDTA test!"; <BR>
struct fcb blk; <BR> int result; <BR>
<P> /* get new file name from user */ <BR>
printf("Enter a file name to create:"); <BR> gets(line); <BR>
<P> /* parse the new file name to the dta */ <BR>
parsfnm(line, &blk, 1); <BR> printf("%d %s\n",
blk.fcb_drive, blk.fcb_name); <BR>
<P> /* request DOS services to create file */ <BR>
if (bdosptr(0x16, &blk, 0) == -1) <BR> {
<BR> perror("Error creating file");
<BR> exit(1); <BR> } <BR>
<P> /* save old dta and set new dta */ <BR>
save_dta = getdta(); <BR> setdta(buffer); <BR>
<P> /* write new records */ <BR> blk.fcb_recsize =
256; <BR> blk.fcb_random = 0L; <BR> result =
randbwr(&blk, 1); <BR> printf("result = %d\n", result);
<BR>
<P> if (!result) <BR>
printf("Write OK\n"); <BR> else <BR> {
<BR> perror("Disk error");
<BR> exit(1); <BR> } <BR>
<P> /* request DOS services to close the file */
<BR> if (bdosptr(0x10, &blk, 0) == -1) <BR> {
<BR> perror("Error closing file");
<BR> exit(1); <BR> } <BR>
<P> /* reset the old dta */ <BR> setdta(save_dta);
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setfillpattern <BR>功 能: 选择用户定义的填充模式 <BR>用 法: void far
setfillpattern(char far *upattern, int color); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int maxx, maxy; <BR>
<P> /* a user defined fill pattern */ <BR> char
pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00}; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> maxx = getmaxx(); <BR> maxy = getmaxy();
<BR> setcolor(getmaxcolor()); <BR>
<P> /* select a user defined fill pattern */ <BR>
setfillpattern(pattern, getmaxcolor()); <BR>
<P> /* fill the screen with the pattern */ <BR>
bar(0, 0, maxx, maxy); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setfillstyle <BR>功 能: 设置填充模式和颜色 <BR>用 法: void far
setfillstyle(int pattern, int color); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<string.h> <BR>#include <stdio.h> <BR>#include <conio.h>
<BR>
<P>/* the names of the fill styles supported */ <BR>char *fname[] = {
"EMPTY_FILL",
<BR>
"SOLID_FILL",
<BR>
"LINE_FILL",
<BR>
"LTSLASH_FILL",
<BR>
"SLASH_FILL",
<BR>
"BKSLASH_FILL",
<BR>
"LTBKSLASH_FILL", <BR> "HATCH_FILL",
<BR>
"XHATCH_FILL",
<BR>
"INTERLEAVE_FILL",
<BR>
"WIDE_DOT_FILL",
<BR>
"CLOSE_DOT_FILL", <BR> "USER_FILL"
<BR>
}; <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int style, midx, midy; <BR> char stylestr[40]; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> midx = getmaxx() / 2; <BR> midy = getmaxy() /
2; <BR>
<P> for (style = EMPTY_FILL; style < USER_FILL; style++)
<BR> { <BR> /* select the fill
style */ <BR> setfillstyle(style,
getmaxcolor()); <BR>
<P> /* convert style into a string */
<BR> strcpy(stylestr, fname[style]); <BR>
<P> /* fill a bar */
<BR> bar3d(0, 0, midx-10, midy, 0, 0); <BR>
<P> /* output a message */
<BR> outtextxy(midx, midy, stylestr); <BR>
<P> /* wait for a key */
<BR> getch();
<BR> cleardevice(); <BR> } <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setftime <BR>功 能: 设置文件日期和时间 <BR>用 法: int setftime(int
handle, struct ftime *ftimep); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <process.h> <BR>#include
<fcntl.h> <BR>#include <io.h> <BR>
<P>int main(void) <BR>{ <BR> struct ftime filet;
<BR> FILE *fp; <BR>
<P> if ((fp = fopen("TEST.$$$", "w")) == NULL)
<BR> { <BR> perror("Error:");
<BR> exit(1); <BR> } <BR>
<P> fprintf(fp, "testing...\n"); <BR>
<P> /* load ftime structure with new time and date */
<BR> filet.ft_tsec = 1; <BR> filet.ft_min = 1;
<BR> filet.ft_hour = 1; <BR> filet.ft_day = 1;
<BR> filet.ft_month = 1; <BR> filet.ft_year = 21;
<BR>
<P> /* show current directory for time and date */
<BR> system("dir TEST.$$$"); <BR>
<P> /* change the time and date stamp*/ <BR>
setftime(fileno(fp), &filet); <BR>
<P> /* close and remove the temporary file */ <BR>
fclose(fp); <BR>
<P> system("dir TEST.$$$"); <BR>
<P> unlink("TEST.$$$"); <BR> return 0; <BR>}
<BR> <BR> <BR>
<P>函数名: setgraphbufsize <BR>功 能: 改变内部图形缓冲区的大小 <BR>用 法:
unsigned far setgraphbufsize(unsigned bufsize); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>#define BUFSIZE 1000 /* internal graphics buffer size */ <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int x, y, oldsize; <BR> char msg[80]; <BR>
<P> /* set the size of the internal graphics buffer */
<BR> /* before making a call to
initgraph. */
<BR> oldsize = setgraphbufsize(BUFSIZE); <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> x = getmaxx() / 2; <BR> y = getmaxy() / 2;
<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -