紫影基地

 找回密码
 立即注册
查看: 114|回复: 0

C语言库函数及示例

[复制链接]
阅读字号:

2002

主题

2117

帖子

21万

积分

超级版主

Rank: 8Rank: 8

积分
210303
发表于 2024-4-20 23:46:05 | 显示全部楼层 |阅读模式
  1. 函数名: abort
  2. 功 能: 异常终止一个进程
  3. 用 法: void abort(void);
  4. 程序例:
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. int main(void)
  8. {
  9. printf("Calling abort()n");
  10. abort();
  11. return 0; /* This is never reached */
  12. }



  13. 函数名: abs
  14. 功 能: 求整数的绝对值
  15. 用 法: int abs(int i);
  16. 程序例:
  17. #include <stdio.h>
  18. #include <math.h>

  19. int main(void)
  20. {
  21. int number = -1234;

  22. printf("number: %d absolute value: %dn", number, abs(number));
  23. return 0;
  24. }



  25. 函数名: absread, abswirte
  26. 功 能: 绝对磁盘扇区读、写数据
  27. 用 法: int absread(int drive, int nsects, int sectno, void *buffer);
  28. int abswrite(int drive, int nsects, in tsectno, void *buffer);
  29. 程序例:
  30. /* absread example */

  31. #include <stdio.h>
  32. #include <conio.h>
  33. #include <process.h>
  34. #include <dos.h>

  35. int main(void)
  36. {
  37. int i, strt, ch_out, sector;
  38. char buf[512];

  39. printf("Insert a diskette into drive A and press any keyn");
  40. getch();
  41. sector = 0;
  42. if (absread(0, 1, sector, &buf) != 0)
  43. {
  44. perror("Disk problem");
  45. exit(1);
  46. }
  47. printf("Read OKn");
  48. strt = 3;
  49. for (i=0; i<80; i++)
  50. {
  51. ch_out = buf[strt+i];
  52. putchar(ch_out);
  53. }
  54. printf("n");
  55. return(0);
  56. }




  57. 函数名: access
  58. 功 能: 确定文件的访问权限
  59. 用 法: int access(const char *filename, int amode);
  60. 程序例:
  61. #include <stdio.h>
  62. #include <io.h>

  63. int file_exists(char *filename);

  64. int main(void)
  65. {
  66. printf("Does NOTEXIST.FIL exist: %sn",
  67. file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
  68. return 0;
  69. }

  70. int file_exists(char *filename)
  71. {
  72. return (access(filename, 0) == 0);
  73. }


  74. 函数名: acos
  75. 功 能: 反余弦函数
  76. 用 法: double acos(double x);
  77. 程序例:
  78. #include <stdio.h>
  79. #include <math.h>

  80. int main(void)
  81. {
  82. double result;
  83. double x = 0.5;

  84. result = acos(x);
  85. printf("The arc cosine of %lf is %lfn", x, result);
  86. return 0;
  87. }



  88. 函数名: allocmem
  89. 功 能: 分配DOS存储段
  90. 用 法: int allocmem(unsigned size, unsigned *seg);
  91. 程序例:
  92. #include <dos.h>
  93. #include <alloc.h>
  94. #include <stdio.h>

  95. int main(void)
  96. {
  97. unsigned int size, segp;
  98. int stat;

  99. size = 64; /* (64 x 16) = 1024 bytes */
  100. stat = allocmem(size, &segp);
  101. if (stat == -1)
  102. printf("Allocated memory at segment: %xn", segp);
  103. else
  104. printf("Failed: maximum number of paragraphs available is %un",
  105. stat);

  106. return 0;
  107. }



  108. 函数名: arc
  109. 功 能: 画一弧线
  110. 用 法: void far arc(int x, int y, int stangle, int endangle, int radius);
  111. 程序例:
  112. #include <graphics.h>
  113. #include <stdlib.h>
  114. #include <stdio.h>
  115. #include <conio.h>

  116. int main(void)
  117. {
  118. /* request auto detection */
  119. int gdriver = DETECT, gmode, errorcode;
  120. int midx, midy;
  121. int stangle = 45, endangle = 135;
  122. int radius = 100;

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

  125. /* read result of initialization */
  126. errorcode = graphresult(); /* an error occurred */
  127. if (errorcode != grOk)
  128. {
  129. printf("Graphics error: %sn", grapherrormsg(errorcode));
  130. printf("Press any key to halt:");
  131. getch();

  132. exit(1); /* terminate with an error code */
  133. }

  134. midx = getmaxx() / 2;
  135. midy = getmaxy() / 2;
  136. setcolor(getmaxcolor());

  137. /* draw arc */
  138. arc(midx, midy, stangle, endangle, radius);

  139. /* clean up */
  140. getch();
  141. closegraph();
  142. return 0;
  143. }



  144. 函数名: asctime
  145. 功 能: 转换日期和时间为ASCII码
  146. 用 法: char *asctime(const struct tm *tblock);
  147. 程序例:
  148. #include <stdio.h>
  149. #include <string.h>
  150. #include <time.h>

  151. int main(void)
  152. {
  153. struct tm t;
  154. char str[80];

  155. /* sample loading of tm structure */

  156. t.tm_sec = 1; /* Seconds */
  157. t.tm_min = 30; /* Minutes */
  158. t.tm_hour = 9; /* Hour */
  159. t.tm_mday = 22; /* Day of the Month */
  160. t.tm_mon = 11; /* Month */
  161. t.tm_year = 56; /* Year - does not include century */
  162. t.tm_wday = 4; /* Day of the week */
  163. t.tm_yday = 0; /* Does not show in asctime */
  164. t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */

  165. /* converts structure to null terminated
  166. string */

  167. strcpy(str, asctime(&t));
  168. printf("%sn", str);

  169. return 0;
  170. }




  171. 函数名: asin
  172. 功 能: 反正弦函数
  173. 用 法: double asin(double x);
  174. 程序例:
  175. #include <stdio.h>
  176. #include <math.h>

  177. int main(void)
  178. {
  179. double result;
  180. double x = 0.5;

  181. result = asin(x);
  182. printf("The arc sin of %lf is %lfn", x, result);
  183. return(0);
  184. }




  185. 函数名: assert
  186. 功 能: 测试一个条件并可能使程序终止
  187. 用 法: void assert(int test);
  188. 程序例:
  189. #include <assert.h>
  190. #include <stdio.h>
  191. #include <stdlib.h>

  192. struct ITEM {
  193. int key;
  194. int value;
  195. };

  196. /* add item to list, make sure list is not null */
  197. void additem(struct ITEM *itemptr) {
  198. assert(itemptr != NULL);
  199. /* add item to list */
  200. }

  201. int main(void)
  202. {
  203. additem(NULL);
  204. return 0;
  205. }




  206. 函数名: atan
  207. 功 能: 反正切函数
  208. 用 法: double atan(double x);
  209. 程序例:
  210. #include <stdio.h>
  211. #include <math.h>

  212. int main(void)
  213. {
  214. double result;
  215. double x = 0.5;

  216. result = atan(x);
  217. printf("The arc tangent of %lf is %lfn", x, result);
  218. return(0);
  219. }



  220. 函数名: atan2
  221. 功 能: 计算Y/X的反正切值
  222. 用 法: double atan2(double y, double x);
  223. 程序例:
  224. #include <stdio.h>
  225. #include <math.h>

  226. int main(void)
  227. {
  228. double result;
  229. double x = 90.0, y = 45.0;

  230. result = atan2(y, x);
  231. printf("The arc tangent ratio of %lf is %lfn", (y / x), result);
  232. return 0;
  233. }



  234. 函数名: atexit
  235. 功 能: 注册终止函数
  236. 用 法: int atexit(atexit_t func);
  237. 程序例:
  238. #include <stdio.h>
  239. #include <stdlib.h>

  240. void exit_fn1(void)
  241. {
  242. printf("Exit function #1 calledn");
  243. }

  244. void exit_fn2(void)
  245. {
  246. printf("Exit function #2 calledn");
  247. }

  248. int main(void)
  249. {
  250. /* post exit function #1 */
  251. atexit(exit_fn1);
  252. /* post exit function #2 */
  253. atexit(exit_fn2);
  254. return 0;
  255. }




  256. 函数名: atof
  257. 功 能: 把字符串转换成浮点数
  258. 用 法: double atof(const char *nptr);
  259. 程序例:
  260. #include <stdlib.h>
  261. #include <stdio.h>

  262. int main(void)
  263. {
  264. float f;
  265. char *str = "12345.67";

  266. f = atof(str);
  267. printf("string = %s float = %fn", str, f);
  268. return 0;
  269. }



  270. 函数名: atoi
  271. 功 能: 把字符串转换成长整型数
  272. 用 法: int atoi(const char *nptr);
  273. 程序例:
  274. #include <stdlib.h>
  275. #include <stdio.h>

  276. int main(void)
  277. {
  278. int n;
  279. char *str = "12345.67";

  280. n = atoi(str);
  281. printf("string = %s integer = %dn", str, n);
  282. return 0;
  283. }



  284. 函数名: atol
  285. 功 能: 把字符串转换成长整型数
  286. 用 法: long atol(const char *nptr);
  287. 程序例:

  288. #include <stdlib.h>
  289. #include <stdio.h>

  290. int main(void)
  291. {
  292. long l;
  293. char *str = "98765432";

  294. l = atol(lstr);
  295. printf("string = %s integer = %ldn", str, l);
  296. return(0);
  297. }

  298. 函数名: bar
  299. 功 能: 画一个二维条形图
  300. 用 法: void far bar(int left, int top, int right, int bottom);
  301. 程序例:
  302. #include <graphics.h>
  303. #include <stdlib.h>
  304. #include <stdio.h>
  305. #include <conio.h>

  306. int main(void)
  307. {
  308. /* request auto detection */
  309. int gdriver = DETECT, gmode, errorcode;
  310. int midx, midy, i;

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

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

  322. midx = getmaxx() / 2;
  323. midy = getmaxy() / 2;

  324. /* loop through the fill patterns */
  325. for (i=SOLID_FILL; i<USER_FILL; i++)
  326. {
  327. /* set the fill style */
  328. setfillstyle(i, getmaxcolor());

  329. /* draw the bar */
  330. bar(midx-50, midy-50, midx+50,
  331. midy+50);

  332. getch();
  333. }

  334. /* clean up */
  335. closegraph();
  336. return 0;
  337. }


  338. 函数名: bar3d
  339. 功 能: 画一个三维条形图
  340. 用 法: void far bar3d(int left, int top, int right, int bottom,
  341. int depth, int topflag);
  342. 程序例:

  343. #include <graphics.h>
  344. #include <stdlib.h>
  345. #include <stdio.h>
  346. #include <conio.h>

  347. int main(void)
  348. {
  349. /* request auto detection */
  350. int gdriver = DETECT, gmode, errorcode;
  351. int midx, midy, i;

  352. /* initialize graphics, local variables */
  353. initgraph(&gdriver, &gmode, "");

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

  363. midx = getmaxx() / 2;
  364. midy = getmaxy() / 2;

  365. /* loop through the fill patterns */
  366. for (i=EMPTY_FILL; i<USER_FILL; i++)
  367. {
  368. /* set the fill style */
  369. setfillstyle(i, getmaxcolor());

  370. /* draw the 3-d bar */
  371. bar3d(midx-50, midy-50, midx+50, midy+50, 10, 1);

  372. getch();
  373. }

  374. /* clean up */
  375. closegraph();
  376. return 0;
  377. }


  378. 函数名: bdos
  379. 功 能: DOS系统调用
  380. 用 法: int bdos(int dosfun, unsigned dosdx, unsigned dosal);
  381. 程序例:

  382. #include <stdio.h>
  383. #include <dos.h>

  384. /* Get current drive as 'A', 'B', ... */
  385. char current_drive(void)
  386. {
  387. char curdrive;

  388. /* Get current disk as 0, 1, ... */
  389. curdrive = bdos(0x19, 0, 0);
  390. return('A' + curdrive);
  391. }

  392. int main(void)
  393. {
  394. printf("The current drive is %c:\n", current_drive());
  395. return 0;
  396. }


  397. 函数名: bdosptr
  398. 功 能: DOS系统调用
  399. 用 法: int bdosptr(int dosfun, void *argument, unsigned dosal);
  400. 程序例:

  401. #include <string.h>
  402. #include <stdio.h>
  403. #include <dir.h>
  404. #include <dos.h>
  405. #include <errno.h>
  406. #include <stdlib.h>

  407. #define BUFLEN 80

  408. int main(void)
  409. {
  410. char buffer[BUFLEN];
  411. int test;

  412. printf("Enter full pathname of a directory\n");
  413. gets(buffer);

  414. test = bdosptr(0x3B,buffer,0);
  415. if(test)
  416. {
  417. printf("DOS error message: %d\n", errno);
  418. /* See errno.h for error listings */
  419. exit (1);
  420. }

  421. getcwd(buffer, BUFLEN);
  422. printf("The current directory is: %s\n", buffer);

  423. return 0;
  424. }


  425. 函数名: bioscom
  426. 功 能: 串行I/O通信
  427. 用 法: int bioscom(int cmd, char abyte, int port);
  428. 程序例:

  429. #include <bios.h>
  430. #include <conio.h>

  431. #define COM1 0
  432. #define DATA_READY 0x100
  433. #define TRUE 1
  434. #define FALSE 0

  435. #define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)

  436. int main(void)
  437. {
  438. int in, out, status, DONE = FALSE;

  439. bioscom(0, SETTINGS, COM1);
  440. cprintf("... BIOSCOM [ESC] to exit ...\n");
  441. while (!DONE)
  442. {
  443. status = bioscom(3, 0, COM1);
  444. if (status & DATA_READY)
  445. if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
  446. putch(out);
  447. if (kbhit())
  448. {
  449. if ((in = getch()) == '\x1B')
  450. DONE = TRUE;
  451. bioscom(1, in, COM1);
  452. }
  453. }
  454. return 0;
  455. }


  456. 函数名: biosdisk
  457. 功 能: 软硬盘I/O
  458. 用 法: int biosdisk(int cmd, int drive, int head, int track, int sector
  459. int nsects, void *buffer);
  460. 程序例:

  461. #include <bios.h>
  462. #include <stdio.h>

  463. int main(void)
  464. {
  465. int result;
  466. char buffer[512];

  467. printf("Testing to see if drive a: is ready\n");
  468. result = biosdisk(4,0,0,0,0,1,buffer);
  469. result &= 0x02;
  470. (result) ? (printf("Drive A: Ready\n")) :
  471. (printf("Drive A: Not Ready\n"));

  472. return 0;
  473. }


  474. 函数名: biosequip
  475. 功 能: 检查设备
  476. 用 法: int biosequip(void);
  477. 程序例:

  478. #include <bios.h>
  479. #include <stdio.h>

  480. int main(void)
  481. {
  482. int result;
  483. char buffer[512];

  484. printf("Testing to see if drive a: is ready\n");
  485. result = biosdisk(4,0,0,0,0,1,buffer);
  486. result &= 0x02;
  487. (result) ? (printf("Drive A: Ready\n")) :
  488. (printf("Drive A: Not Ready\n"));

  489. return 0;
  490. }


  491. 函数名: bioskey
  492. 功 能: 直接使用BIOS服务的键盘接口
  493. 用 法: int bioskey(int cmd);
  494. 程序例:

  495. #include <stdio.h>
  496. #include <bios.h>
  497. #include <ctype.h>

  498. #define RIGHT 0x01
  499. #define LEFT 0x02
  500. #define CTRL 0x04
  501. #define ALT 0x08

  502. int main(void)
  503. {
  504. int key, modifiers;

  505. /* function 1 returns 0 until a key is pressed */
  506. while (bioskey(1) == 0);

  507. /* function 0 returns the key that is waiting */
  508. key = bioskey(0);

  509. /* use function 2 to determine if shift keys were used */
  510. modifiers = bioskey(2);
  511. if (modifiers)
  512. {
  513. printf("[");
  514. if (modifiers & RIGHT) printf("RIGHT");
  515. if (modifiers & LEFT) printf("LEFT");
  516. if (modifiers & CTRL) printf("CTRL");
  517. if (modifiers & ALT) printf("ALT");
  518. printf("]");
  519. }
  520. /* print out the character read */
  521. if (isalnum(key & 0xFF))
  522. printf("'%c'\n", key);
  523. else
  524. printf("%#02x\n", key);
  525. return 0;
  526. }

  527. 函数名: biosmemory
  528. 功 能: 返回存储块大小
  529. 用 法:int biosmemory(void);
  530. 程序例:

  531. #include <stdio.h>
  532. #include <bios.h>

  533. int main(void)
  534. {
  535. int memory_size;

  536. memory_size = biosmemory(); /* returns value up to 640K */
  537. printf("RAM size = %dK\n",memory_size);
  538. return 0;
  539. }


  540. 函数名: biosprint
  541. 功 能: 直接使用BIOS服务的打印机I/O
  542. 用 法: int biosprint(int cmd, int byte, int port);
  543. 程序例:

  544. #include <stdio.h>
  545. #include <conio.h>
  546. #include <bios.h>

  547. int main(void)
  548. {
  549. #define STATUS 2 /* printer status command */
  550. #define PORTNUM 0 /* port number for LPT1 */

  551. int status, abyte=0;

  552. printf("Please turn off your printer. Press any key to continue\n");
  553. getch();
  554. status = biosprint(STATUS, abyte, PORTNUM);
  555. if (status & 0x01)
  556. printf("Device time out.\n");
  557. if (status & 0x08)
  558. printf("I/O error.\n");

  559. if (status & 0x10)
  560. printf("Selected.\n");
  561. if (status & 0x20)
  562. printf("Out of paper.\n");

  563. if (status & 0x40)
  564. printf("Acknowledge.\n");
  565. if (status & 0x80)
  566. printf("Not busy.\n");

  567. return 0;
  568. }


  569. 函数名: biostime
  570. 功 能: 读取或设置BIOS时间
  571. 用 法: long biostime(int cmd, long newtime);
  572. 程序例:

  573. #include <stdio.h>
  574. #include <bios.h>
  575. #include <time.h>
  576. #include <conio.h>

  577. int main(void)
  578. {
  579. long bios_time;

  580. clrscr();
  581. cprintf("The number of clock ticks since midnight is:\r\n");
  582. cprintf("The number of seconds since midnight is:\r\n");
  583. cprintf("The number of minutes since midnight is:\r\n");
  584. cprintf("The number of hours since midnight is:\r\n");
  585. cprintf("\r\nPress any key to quit:");
  586. while(!kbhit())
  587. {
  588. bios_time = biostime(0, 0L);

  589. gotoxy(50, 1);
  590. cprintf("%lu", bios_time);

  591. gotoxy(50, 2);
  592. cprintf("%.4f", bios_time / CLK_TCK);

  593. gotoxy(50, 3);
  594. cprintf("%.4f", bios_time / CLK_TCK / 60);

  595. gotoxy(50, 4);
  596. cprintf("%.4f", bios_time / CLK_TCK / 3600);
  597. }
  598. return 0;
  599. }


  600. 函数名: brk
  601. 功 能: 改变数据段空间分配
  602. 用 法: int brk(void *endds);
  603. 程序例:

  604. #include <stdio.h>
  605. #include <alloc.h>

  606. int main(void)
  607. {
  608. char *ptr;

  609. printf("Changing allocation with brk()\n");
  610. ptr = malloc(1);
  611. printf("Before brk() call: %lu bytes free\n", coreleft());
  612. brk(ptr+1000);
  613. printf(" After brk() call: %lu bytes free\n", coreleft());
  614. return 0;
  615. }


  616. 函数名: bsearch
  617. 功 能: 二分法搜索
  618. 用 法: void *bsearch(const void *key, const void *base, size_t *nelem,
  619. size_t width, int(*fcmp)(const void *, const *));
  620. 程序例:

  621. #include <stdlib.h>
  622. #include <stdio.h>

  623. #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))

  624. int numarray[] = {123, 145, 512, 627, 800, 933};

  625. int numeric (const int *p1, const int *p2)
  626. {
  627. return(*p1 - *p2);
  628. }

  629. int lookup(int key)
  630. {
  631. int *itemptr;

  632. /* The cast of (int(*)(const void *,const void*))
  633. is needed to avoid a type mismatch error at
  634. compile time */
  635. itemptr = bsearch (&key, numarray, NELEMS(numarray),
  636. sizeof(int), (int(*)(const void *,const void *))numeric);
  637. return (itemptr != NULL);
  638. }

  639. int main(void)
  640. {
  641. if (lookup(512))
  642. printf("512 is in the table.\n");
  643. else
  644. printf("512 isn't in the table.\n");

  645. return 0;
  646. }
  647. 函数名: cabs
  648. 功 能: 计算复数的绝对值
  649. 用 法: double cabs(struct complex z);
  650. 程序例:
  651. #include <stdio.h>
  652. #include <math.h>

  653. int main(void)
  654. {
  655. struct complex z;
  656. double val;

  657. z.x = 2.0;
  658. z.y = 1.0;
  659. val = cabs(z);

  660. printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
  661. return 0;
  662. }

  663. 函数名: calloc
  664. 功 能: 分配主存储器
  665. 用 法: void *calloc(size_t nelem, size_t elsize);
  666. 程序例:

  667. #include <stdio.h>
  668. #include <alloc.h>

  669. int main(void)
  670. {
  671. char *str = NULL;

  672. /* allocate memory for string */
  673. str = calloc(10, sizeof(char));

  674. /* copy "Hello" into string */
  675. strcpy(str, "Hello");

  676. /* display string */
  677. printf("String is %sn", str);

  678. /* free memory */
  679. free(str);

  680. return 0;
  681. }

  682. 函数名: ceil
  683. 功 能: 向上舍入
  684. 用 法: double ceil(double x);
  685. 程序例:

  686. #include <math.h>
  687. #include <stdio.h>

  688. int main(void)
  689. {
  690. double number = 123.54;
  691. double down, up;

  692. down = floor(number);
  693. up = ceil(number);

  694. printf("original number %5.2lfn", number);
  695. printf("number rounded down %5.2lfn", down);
  696. printf("number rounded up %5.2lfn", up);

  697. return 0;
  698. }

  699. 函数名: cgets
  700. 功 能: 从控制台读字符串
  701. 用 法: char *cgets(char *str);
  702. 程序例:

  703. #include <stdio.h>
  704. #include <conio.h>

  705. int main(void)
  706. {
  707. char buffer[83];
  708. char *p;

  709. /* There''s space for 80 characters plus the NULL terminator */
  710. buffer[0] = 81;

  711. printf("Input some chars:");
  712. p = cgets(buffer);
  713. printf("ncgets read %d characters: "%s"n", buffer[1], p);
  714. printf("The returned pointer is %p, buffer[0] is at %pn", p, &buffer);

  715. /* Leave room for 5 characters plus the NULL terminator */
  716. buffer[0] = 6;

  717. printf("Input some chars:");
  718. p = cgets(buffer);
  719. printf("ncgets read %d characters: "%s"n", buffer[1], p);
  720. printf("The returned pointer is %p, buffer[0] is at %pn", p, &buffer);
  721. return 0;
  722. }

  723. 函数名: chdir
  724. 功 能: 改变工作目录
  725. 用 法: int chdir(const char *path);
  726. 程序例:

  727. #include <stdio.h>
  728. #include <stdlib.h>
  729. #include <dir.h>

  730. char old_dir[MAXDIR];
  731. char new_dir[MAXDIR];

  732. int main(void)
  733. {
  734. if (getcurdir(0, old_dir))
  735. {
  736. perror("getcurdir()");
  737. exit(1);
  738. }
  739. printf("Current directory is: %sn", old_dir);

  740. if (chdir(""))
  741. {
  742. perror("chdir()");
  743. exit(1);
  744. }

  745. if (getcurdir(0, new_dir))
  746. {
  747. perror("getcurdir()");
  748. exit(1);
  749. }
  750. printf("Current directory is now: %sn", new_dir);

  751. printf("nChanging back to orignal directory: %sn", old_dir);
  752. if (chdir(old_dir))
  753. {
  754. perror("chdir()");
  755. exit(1);
  756. }

  757. return 0;
  758. }

  759. 函数名: _chmod, chmod
  760. 功 能: 改变文件的访问方式
  761. 用 法: int chmod(const char *filename, int permiss);
  762. 程序例:

  763. #include <sysstat.h>
  764. #include <stdio.h>
  765. #include <io.h>

  766. void make_read_only(char *filename);

  767. int main(void)
  768. {
  769. make_read_only("NOTEXIST.FIL");
  770. make_read_only("MYFILE.FIL");
  771. return 0;
  772. }

  773. void make_read_only(char *filename)
  774. {
  775. int stat;

  776. stat = chmod(filename, S_IREAD);
  777. if (stat)
  778. printf("Couldn''t make %s read-onlyn", filename);
  779. else
  780. printf("Made %s read-onlyn", filename);
  781. }

  782. 函数名: chsize
  783. 功 能: 改变文件大小
  784. 用 法: int chsize(int handle, long size);
  785. 程序例:

  786. #include <string.h>
  787. #include <fcntl.h>
  788. #include <io.h>

  789. int main(void)
  790. {
  791. int handle;
  792. char buf[11] = "0123456789";

  793. /* create text file containing 10 bytes */
  794. handle = open("DUMMY.FIL", O_CREAT);
  795. write(handle, buf, strlen(buf));

  796. /* truncate the file to 5 bytes in size */
  797. chsize(handle, 5);

  798. /* close the file */
  799. close(handle);
  800. return 0;
  801. }

  802. 函数名: circle
  803. 功 能: 在给定半径以(x, y)为圆心画圆
  804. 用 法: void far circle(int x, int y, int radius);
  805. 程序例:

  806. #include <graphics.h>
  807. #include <stdlib.h>
  808. #include <stdio.h>
  809. #include <conio.h>

  810. int main(void)
  811. {
  812. /* request auto detection */
  813. int gdriver = DETECT, gmode, errorcode;
  814. int midx, midy;
  815. int radius = 100;

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

  818. /* read result of initialization */
  819. errorcode = graphresult();
  820. if (errorcode != grOk) /* an error occurred */
  821. {
  822. printf("Graphics error: %sn", grapherrormsg(errorcode));
  823. printf("Press any key to halt:");
  824. getch();
  825. exit(1); /* terminate with an error code */
  826. }

  827. midx = getmaxx() / 2;
  828. midy = getmaxy() / 2;
  829. setcolor(getmaxcolor());

  830. /* draw the circle */
  831. circle(midx, midy, radius);

  832. /* clean up */
  833. getch();
  834. closegraph();
  835. return 0;
  836. }

  837. 函数名: cleardevice
  838. 功 能: 清除图形屏幕
  839. 用 法: void far cleardevice(void);
  840. 程序例:

  841. #include <graphics.h>
  842. #include <stdlib.h>
  843. #include <stdio.h>
  844. #include <conio.h>

  845. int main(void)
  846. {
  847. /* request auto detection */
  848. int gdriver = DETECT, gmode, errorcode;
  849. int midx, midy;

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

  852. /* read result of initialization */
  853. errorcode = graphresult();
  854. if (errorcode != grOk) /* an error occurred */
  855. {
  856. printf("Graphics error: %sn", grapherrormsg(errorcode));
  857. printf("Press any key to halt:");
  858. getch();
  859. exit(1); /* terminate with an error code */
  860. }

  861. midx = getmaxx() / 2;
  862. midy = getmaxy() / 2;
  863. setcolor(getmaxcolor());

  864. /* for centering screen messages */
  865. settextjustify(CENTER_TEXT, CENTER_TEXT);

  866. /* output a message to the screen */
  867. outtextxy(midx, midy, "press any key to clear the screen:");

  868. /* wait for a key */
  869. getch();

  870. /* clear the screen */
  871. cleardevice();

  872. /* output another message */
  873. outtextxy(midx, midy, "press any key to quit:");

  874. /* clean up */
  875. getch();
  876. closegraph();
  877. return 0;
  878. }

  879. 函数名: clearerr
  880. 功 能: 复位错误标志
  881. 用 法:void clearerr(FILE *stream);
  882. 程序例:

  883. #include <stdio.h>

  884. int main(void)
  885. {
  886. FILE *fp;
  887. char ch;

  888. /* open a file for writing */
  889. fp = fopen("DUMMY.FIL", "w");

  890. /* force an error condition by attempting to read */
  891. ch = fgetc(fp);
  892. printf("%cn",ch);

  893. if (ferror(fp))
  894. {
  895. /* display an error message */
  896. printf("Error reading from DUMMY.FILn");

  897. /* reset the error and EOF indicators */
  898. clearerr(fp);
  899. }

  900. fclose(fp);
  901. return 0;
  902. }

  903. 函数名: clearviewport
  904. 功 能: 清除图形视区
  905. 用 法: void far clearviewport(void);
  906. 程序例:

  907. #include <graphics.h>
  908. #include <stdlib.h>
  909. #include <stdio.h>
  910. #include <conio.h>

  911. #define CLIP_ON 1 /* activates clipping in viewport */

  912. int main(void)
  913. {
  914. /* request auto detection */
  915. int gdriver = DETECT, gmode, errorcode;
  916. int ht;

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

  919. /* read result of initialization */
  920. errorcode = graphresult();
  921. if (errorcode != grOk) /* an error occurred */
  922. {
  923. printf("Graphics error: %sn", grapherrormsg(errorcode));
  924. printf("Press any key to halt:");
  925. getch();
  926. exit(1); /* terminate with an error code */
  927. }

  928. setcolor(getmaxcolor());
  929. ht = textheight("W");

  930. /* message in default full-screen viewport */
  931. outtextxy(0, 0, "* <-- (0, 0) in default viewport");

  932. /* create a smaller viewport */
  933. setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);

  934. /* display some messages */
  935. outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
  936. outtextxy(0, 2*ht, "Press any key to clear viewport:");

  937. /* wait for a key */
  938. getch();

  939. /* clear the viewport */
  940. clearviewport();

  941. /* output another message */
  942. outtextxy(0, 0, "Press any key to quit:");

  943. /* clean up */
  944. getch();
  945. closegraph();
  946. return 0;
  947. }

  948. 函数名: _close, close
  949. 功 能: 关闭文件句柄
  950. 用 法: int close(int handle);
  951. 程序例:

  952. #include <string.h>
  953. #include <stdio.h>
  954. #include <fcntl.h>
  955. #include <io.h>

  956. main()
  957. {
  958. int handle;
  959. char buf[11] = "0123456789";

  960. /* create a file containing 10 bytes */
  961. handle = open("NEW.FIL", O_CREAT);
  962. if (handle > -1)
  963. {
  964. write(handle, buf, strlen(buf));

  965. /* close the file */
  966. close(handle);
  967. }
  968. else
  969. {
  970. printf("Error opening filen");
  971. }
  972. return 0;
  973. }

  974. 函数名: clock
  975. 功 能: 确定处理器时间
  976. 用 法: clock_t clock(void);
  977. 程序例:

  978. #include <time.h>
  979. #include <stdio.h>
  980. #include <dos.h>

  981. int main(void)
  982. {
  983. clock_t start, end;
  984. start = clock();

  985. delay(2000);

  986. end = clock();
  987. printf("The time was: %fn", (end - start) / CLK_TCK);

  988. return 0;
  989. }




  990. 函数名: closegraph
  991. 功 能: 关闭图形系统
  992. 用 法: void far closegraph(void);
  993. 程序例:

  994. #include <graphics.h>
  995. #include <stdlib.h>
  996. #include <stdio.h>
  997. #include <conio.h>

  998. int main(void)
  999. {
  1000. /* request auto detection */
  1001. int gdriver = DETECT, gmode, errorcode;
  1002. int x, y;

  1003. /* initialize graphics mode */
  1004. initgraph(&gdriver, &gmode, "");

  1005. /* read result of initialization */
  1006. errorcode = graphresult();

  1007. if (errorcode != grOk) /* an error
  1008. occurred */
  1009. {
  1010. printf("Graphics error: %sn", grapherrormsg(errorcode));
  1011. printf("Press any key to halt:");
  1012. getch();
  1013. exit(1); /* terminate with an error code */
  1014. }

  1015. x = getmaxx() / 2;
  1016. y = getmaxy() / 2;

  1017. /* output a message */
  1018. settextjustify(CENTER_TEXT, CENTER_TEXT);
  1019. outtextxy(x, y, "Press a key to close the graphics system:");

  1020. /* wait for a key */
  1021. getch();

  1022. /* closes down the graphics system */
  1023. closegraph();

  1024. printf("We''re now back in text mode.n");
  1025. printf("Press any key to halt:");
  1026. getch();
  1027. return 0;
  1028. }

  1029. 函数名: clreol
  1030. 功 能: 在文本窗口中清除字符到行末
  1031. 用 法: void clreol(void);
  1032. 程序例:

  1033. #include <conio.h>

  1034. int main(void)

  1035. {
  1036. clrscr();
  1037. cprintf("The function CLREOL clears all characters from thern");
  1038. cprintf("cursor position to the end of the line within thern");
  1039. cprintf("current text window, without moving the cursor.rn");
  1040. cprintf("Press any key to continue . . .");
  1041. gotoxy(14, 4);
  1042. getch();

  1043. clreol();
  1044. getch();

  1045. return 0;
  1046. }

  1047. 函数名: clrscr
  1048. 功 能: 清除文本模式窗口
  1049. 用 法: void clrscr(void);
  1050. 程序例:

  1051. #include <conio.h>

  1052. int main(void)
  1053. {
  1054. int i;

  1055. clrscr();
  1056. for (i = 0; i < 20; i++)
  1057. cprintf("%drn", i);
  1058. cprintf("rnPress any key to clear screen");
  1059. getch();

  1060. clrscr();
  1061. cprintf("The screen has been cleared!");
  1062. getch();

  1063. return 0;
  1064. }

  1065. 函数名: coreleft
  1066. 功 能: 返回未使用内存的大小
  1067. 用 法: unsigned coreleft(void);
  1068. 程序例:

  1069. #include <stdio.h>
  1070. #include <alloc.h>

  1071. int main(void)
  1072. {
  1073. printf("The difference between the highest allocated block andn");
  1074. printf("the top of the heap is: %lu bytesn", (unsigned long) coreleft());

  1075. return 0;
  1076. }


  1077. 函数名: cos
  1078. 功 能: 余弦函数
  1079. 用 法: double cos(double x);
  1080. 程序例:

  1081. #include <stdio.h>
  1082. #include <math.h>

  1083. int main(void)
  1084. {
  1085. double result;
  1086. double x = 0.5;

  1087. result = cos(x);
  1088. printf("The cosine of %lf is %lfn", x, result);
  1089. return 0;
  1090. }

  1091. 函数名: cosh
  1092. 功 能: 双曲余弦函数
  1093. 用 法: dluble cosh(double x);
  1094. 程序例:

  1095. #include <stdio.h>
  1096. #include <math.h>

  1097. int main(void)
  1098. {
  1099. double result;
  1100. double x = 0.5;

  1101. result = cosh(x);
  1102. printf("The hyperboic cosine of %lf is %lfn", x, result);
  1103. return 0;
  1104. }
  1105. 函数名: country
  1106. 功 能: 返回与国家有关的信息
  1107. 用 法: struct COUNTRY *country(int countrycode, struct country *country);
  1108. 程序例:

  1109. #include <dos.h>
  1110. #include <stdio.h>

  1111. #define USA 0

  1112. int main(void)
  1113. {
  1114. struct COUNTRY country_info;

  1115. country(USA, &country_info);
  1116. printf("The currency symbol for the USA is: %sn",
  1117. country_info.co_curr);
  1118. return 0;
  1119. }

  1120. 函数名: cprintf
  1121. 功 能: 送格式化输出至屏幕
  1122. 用 法: int cprintf(const char *format[, argument, ...]);
  1123. 程序例:

  1124. #include <conio.h>

  1125. int main(void)
  1126. {
  1127. /* clear the screen */
  1128. clrscr();

  1129. /* create a text window */
  1130. window(10, 10, 80, 25);

  1131. /* output some text in the window */
  1132. cprintf("Hello worldrn");

  1133. /* wait for a key */
  1134. getch();
  1135. return 0;
  1136. }

  1137. 函数名: cputs
  1138. 功 能: 写字符到屏幕
  1139. 用 法: void cputs(const char *string);
  1140. 程序例:

  1141. #include <conio.h>

  1142. int main(void)
  1143. {
  1144. /* clear the screen */
  1145. clrscr();

  1146. /* create a text window */
  1147. window(10, 10, 80, 25);

  1148. /* output some text in the window */
  1149. cputs("This is within the windowrn");

  1150. /* wait for a key */
  1151. getch();
  1152. return 0;
  1153. }

  1154. 函数名: _creat creat
  1155. 功 能: 创建一个新文件或重写一个已存在的文件
  1156. 用 法: int creat (const char *filename, int permiss);
  1157. 程序例:

  1158. #include <sysstat.h>
  1159. #include <string.h>
  1160. #include <fcntl.h>
  1161. #include <io.h>

  1162. int main(void)
  1163. {
  1164. int handle;
  1165. char buf[11] = "0123456789";

  1166. /* change the default file mode from text to binary */
  1167. _fmode = O_BINARY;

  1168. /* create a binary file for reading and writing */
  1169. handle = creat("DUMMY.FIL", S_IREAD S_IWRITE);

  1170. /* write 10 bytes to the file */
  1171. write(handle, buf, strlen(buf));

  1172. /* close the file */
  1173. close(handle);
  1174. return 0;
  1175. }

  1176. 函数名: creatnew
  1177. 功 能: 创建一个新文件
  1178. 用 法: int creatnew(const char *filename, int attrib);
  1179. 程序例:

  1180. #include <string.h>
  1181. #include <stdio.h>
  1182. #include <errno.h>
  1183. #include <dos.h>
  1184. #include <io.h>

  1185. int main(void)
  1186. {
  1187. int handle;
  1188. char buf[11] = "0123456789";

  1189. /* attempt to create a file that doesn''t already exist */
  1190. handle = creatnew("DUMMY.FIL", 0);

  1191. if (handle == -1)
  1192. printf("DUMMY.FIL already exists.n");
  1193. else
  1194. {
  1195. printf("DUMMY.FIL successfully created.n");
  1196. write(handle, buf, strlen(buf));
  1197. close(handle);
  1198. }
  1199. return 0;
  1200. }

  1201. 函数名: creattemp
  1202. 功 能: 创建一个新文件或重写一个已存在的文件
  1203. 用 法: int creattemp(const char *filename, int attrib);
  1204. 程序例:

  1205. #include <string.h>
  1206. #include <stdio.h>
  1207. #include <io.h>

  1208. int main(void)
  1209. {
  1210. int handle;
  1211. char pathname[128];

  1212. strcpy(pathname, "");

  1213. /* create a unique file in the root directory */
  1214. handle = creattemp(pathname, 0);

  1215. printf("%s was the unique file created.n", pathname);
  1216. close(handle);
  1217. return 0;
  1218. }

  1219. 函数名: cscanf
  1220. 功 能: 从控制台执行格式化输入
  1221. 用 法: int cscanf(char *format[,argument, ...]);
  1222. 程序例:

  1223. #include <conio.h>

  1224. int main(void)
  1225. {
  1226. char string[80];

  1227. /* clear the screen */
  1228. clrscr();

  1229. /* Prompt the user for input */
  1230. cprintf("Enter a string with no spaces:");

  1231. /* read the input */
  1232. cscanf("%s", string);

  1233. /* display what was read */
  1234. cprintf("rnThe string entered is: %s", string);
  1235. return 0;
  1236. }

  1237. 函数名: ctime
  1238. 功 能: 把日期和时间转换为字符串
  1239. 用 法: char *ctime(const time_t *time);
  1240. 程序例:

  1241. #include <stdio.h>
  1242. #include <time.h>

  1243. int main(void)
  1244. {
  1245. time_t t;

  1246. time(&t);
  1247. printf("Today''s date and time: %sn", ctime(&t));
  1248. return 0;
  1249. }

  1250. 函数名: ctrlbrk
  1251. 功 能: 设置Ctrl-Break处理程序
  1252. 用 法: void ctrlbrk(*fptr)(void);
  1253. 程序例:

  1254. #include <stdio.h>
  1255. #include <dos.h>

  1256. #define ABORT 0

  1257. int c_break(void)
  1258. {
  1259. printf("Control-Break pressed. Program aborting ...n");
  1260. return (ABORT);
  1261. }

  1262. int main(void)
  1263. {
  1264. ctrlbrk(c_break);
  1265. for(;;)
  1266. {
  1267. printf("Looping... Press <Ctrl-Break> to quit:n");
  1268. }
  1269. return 0;
  1270. }

  1271. 函数名: delay
  1272. 功 能: 将程序的执行暂停一段时间(毫秒)
  1273. 用 法: void delay(unsigned milliseconds);
  1274. 程序例:
  1275. /* Emits a 440-Hz tone for 500 milliseconds */
  1276. #include <dos.h>
  1277. int main(void)
  1278. {
  1279. sound(440);
  1280. delay(500);
  1281. nosound();

  1282. return 0;
  1283. }

  1284. 函数名: delline
  1285. 功 能: 在文本窗口中删去一行
  1286. 用 法: void delline(void);
  1287. 程序例:

  1288. #include <conio.h>

  1289. int main(void)
  1290. {
  1291. clrscr();
  1292. cprintf("The function DELLINE deletes \
  1293. the line containing the\r\n");
  1294. cprintf("cursor and moves all lines \
  1295. below it one line up.\r\n");
  1296. cprintf("DELLINE operates within the \
  1297. currently active text\r\n");
  1298. cprintf("window. Press any key to \
  1299. continue . . .");
  1300. gotoxy(1,2); /* Move the cursor to the
  1301. second line and first column */
  1302. getch();

  1303. delline();
  1304. getch();

  1305. return 0;
  1306. }


  1307. 函数名: detectgraph
  1308. 功 能: 通过检测硬件确定图形驱动程序和模式
  1309. 用 法: void far detectgraph(int far *graphdriver, int far *graphmode);
  1310. 程序例:

  1311. #include <graphics.h>
  1312. #include <stdlib.h>
  1313. #include <stdio.h>
  1314. #include <conio.h>

  1315. /* names of the various cards supported */
  1316. char *dname[] = { "requests detection",
  1317. "a CGA",
  1318. "an MCGA",
  1319. "an EGA",
  1320. "a 64K EGA",
  1321. "a monochrome EGA",
  1322. "an IBM 8514",
  1323. "a Hercules monochrome",
  1324. "an AT&T 6300 PC",
  1325. "a VGA",
  1326. "an IBM 3270 PC"
  1327. };

  1328. int main(void)
  1329. {
  1330. /* returns detected hardware info. */
  1331. int gdriver, gmode, errorcode;

  1332. /* detect graphics hardware available */
  1333. detectgraph(&gdriver, &gmode);

  1334. /* read result of detectgraph call */
  1335. errorcode = graphresult();
  1336. if (errorcode != grOk) /* an error
  1337. occurred */
  1338. {
  1339. printf("Graphics error: %s\n", \
  1340. grapherrormsg(errorcode));
  1341. printf("Press any key to halt:");
  1342. getch();
  1343. exit(1); /* terminate with an error
  1344. code */
  1345. }

  1346. /* display the information detected */
  1347. clrscr();
  1348. printf("You have %s video display \
  1349. card.\n", dname[gdriver]);
  1350. printf("Press any key to halt:");
  1351. getch();
  1352. return 0;
  1353. }


  1354. 函数名: difftime
  1355. 功 能: 计算两个时刻之间的时间差
  1356. 用 法: double difftime(time_t time2, time_t time1);
  1357. 程序例:

  1358. #include <time.h>
  1359. #include <stdio.h>
  1360. #include <dos.h>
  1361. #include <conio.h>

  1362. int main(void)
  1363. {
  1364. time_t first, second;

  1365. clrscr();
  1366. first = time(NULL); /* Gets system
  1367. time */
  1368. delay(2000); /* Waits 2 secs */
  1369. second = time(NULL); /* Gets system time
  1370. again */

  1371. printf("The difference is: %f \
  1372. seconds\n",difftime(second,first));
  1373. getch();

  1374. return 0;
  1375. }

  1376. 函数名: disable
  1377. 功 能: 屏蔽中断
  1378. 用 法: void disable(void);
  1379. 程序例:

  1380. /***NOTE: This is an interrupt service
  1381. routine. You cannot compile this program
  1382. with Test Stack Overflow turned on and
  1383. get an executable file that operates
  1384. correctly. */

  1385. #include <stdio.h>
  1386. #include <dos.h>
  1387. #include <conio.h>

  1388. #define INTR 0X1C /* The clock tick
  1389. interrupt */

  1390. void interrupt ( *oldhandler)(void);

  1391. int count=0;

  1392. void interrupt handler(void)
  1393. {
  1394. /* disable interrupts during the handling of
  1395. the interrupt */
  1396. disable();
  1397. /* increase the global counter */
  1398. count++;
  1399. /* reenable interrupts at the end of the
  1400. handler */
  1401. enable();
  1402. /* call the old routine */
  1403. oldhandler();
  1404. }

  1405. int main(void)
  1406. {
  1407. /* save the old interrupt vector */
  1408. oldhandler = getvect(INTR);

  1409. /* install the new interrupt handler */
  1410. setvect(INTR, handler);

  1411. /* loop until the counter exceeds 20 */
  1412. while (count < 20)
  1413. printf("count is %d\n",count);

  1414. /* reset the old interrupt handler */
  1415. setvect(INTR, oldhandler);

  1416. return 0;
  1417. }

  1418. 函数名: div
  1419. 功 能: 将两个整数相除, 返回商和余数
  1420. 用 法: div_t (int number, int denom);
  1421. 程序例:

  1422. #include <stdlib.h>
  1423. #include <stdio.h>

  1424. div_t x;

  1425. int main(void)
  1426. {
  1427. x = div(10,3);
  1428. printf("10 div 3 = %d remainder %d\n", x.quot, x.rem);

  1429. return 0;
  1430. }

  1431. 函数名: dosexterr
  1432. 功 能: 获取扩展DOS错误信息
  1433. 用 法: int dosexterr(struct DOSERR *dblkp);
  1434. 程序例:

  1435. #include <stdio.h>
  1436. #include <dos.h>

  1437. int main(void)
  1438. {
  1439. FILE *fp;
  1440. struct DOSERROR info;

  1441. fp = fopen("perror.dat","r");
  1442. if (!fp) perror("Unable to open file for
  1443. reading");
  1444. dosexterr(&info);

  1445. printf("Extended DOS error \
  1446. information:\n");
  1447. printf(" Extended error: \
  1448. %d\n",info.exterror);
  1449. printf(" Class: \
  1450. %x\n",info.class);
  1451. printf(" Action: \
  1452. %x\n",info.action);
  1453. printf(" Error Locus: \
  1454. %x\n",info.locus);

  1455. return 0;
  1456. }

  1457. 函数名: dostounix
  1458. 功 能: 转换日期和时间为UNIX时间格式
  1459. 用 法: long dostounix(struct date *dateptr, struct time *timeptr);
  1460. 程序例:

  1461. #include <time.h>
  1462. #include <stddef.h>
  1463. #include <dos.h>
  1464. #include <stdio.h>

  1465. int main(void)
  1466. {
  1467. time_t t;
  1468. struct time d_time;
  1469. struct date d_date;
  1470. struct tm *local;

  1471. getdate(&d_date);
  1472. gettime(&d_time);

  1473. t = dostounix(&d_date, &d_time);
  1474. local = localtime(&t);
  1475. printf("Time and Date: %s\n", \
  1476. asctime(local));

  1477. return 0;
  1478. }

  1479. 函数名: drawpoly
  1480. 功 能: 画多边形
  1481. 用 法: void far drawpoly(int numpoints, int far *polypoints);
  1482. 程序例:

  1483. #include <graphics.h>
  1484. #include <stdlib.h>
  1485. #include <stdio.h>
  1486. #include <conio.h>

  1487. int main(void)
  1488. {
  1489. /* request auto detection */
  1490. int gdriver = DETECT, gmode, errorcode;
  1491. int maxx, maxy;

  1492. /* our polygon array */
  1493. int poly[10];

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

  1497. /* read result of initialization */
  1498. errorcode = graphresult();
  1499. if (errorcode != grOk)
  1500. /* an error occurred */
  1501. {
  1502. printf("Graphics error: %s\n", \
  1503. grapherrormsg(errorcode));
  1504. printf("Press any key to halt:");
  1505. getch();
  1506. /* terminate with an error code */
  1507. exit(1);
  1508. }

  1509. maxx = getmaxx();
  1510. maxy = getmaxy();

  1511. poly[0] = 20; /* 1st vertext */
  1512. poly[1] = maxy / 2;

  1513. poly[2] = maxx - 20; /* 2nd */
  1514. poly[3] = 20;

  1515. poly[4] = maxx - 50; /* 3rd */
  1516. poly[5] = maxy - 20;

  1517. poly[6] = maxx / 2; /* 4th */
  1518. poly[7] = maxy / 2;
  1519. /*
  1520. drawpoly doesn't automatically close
  1521. the polygon, so we close it.
  1522. */
  1523. poly[8] = poly[0];
  1524. poly[9] = poly[1];

  1525. /* draw the polygon */
  1526. drawpoly(5, poly);

  1527. /* clean up */
  1528. getch();
  1529. closegraph();
  1530. return 0;
  1531. }

  1532. 函数名: dup
  1533. 功 能: 复制一个文件句柄
  1534. 用 法: int dup(int handle);
  1535. 程序例:

  1536. #include <string.h>
  1537. #include <stdio.h>
  1538. #include <conio.h>
  1539. #include <io.h>

  1540. void flush(FILE *stream);

  1541. int main(void)
  1542. {
  1543. FILE *fp;
  1544. char msg[] = "This is a test";

  1545. /* create a file */
  1546. fp = fopen("DUMMY.FIL", "w");

  1547. /* write some data to the file */
  1548. fwrite(msg, strlen(msg), 1, fp);

  1549. clrscr();
  1550. printf("Press any key to flush \
  1551. DUMMY.FIL:");
  1552. getch();

  1553. /* flush the data to DUMMY.FIL without
  1554. closing it */
  1555. flush(fp);

  1556. printf("\nFile was flushed, Press any \
  1557. key to quit:");
  1558. getch();
  1559. return 0;
  1560. }

  1561. void flush(FILE *stream)
  1562. {
  1563. int duphandle;

  1564. /* flush TC's internal buffer */
  1565. fflush(stream);

  1566. /* make a duplicate file handle */
  1567. duphandle = dup(fileno(stream));

  1568. /* close the duplicate handle to flush the
  1569. DOS buffer */
  1570. close(duphandle);
  1571. }

  1572. 函数名: dup2
  1573. 功 能: 复制文件句柄
  1574. 用 法: int dup2(int oldhandle, int newhandle);
  1575. 程序例:

  1576. #include <sys\stat.h>
  1577. #include <string.h>
  1578. #include <fcntl.h>
  1579. #include <io.h>

  1580. int main(void)
  1581. {
  1582. #define STDOUT 1

  1583. int nul, oldstdout;
  1584. char msg[] = "This is a test";

  1585. /* create a file */
  1586. nul = open("DUMMY.FIL", O_CREAT | O_RDWR,
  1587. S_IREAD | S_IWRITE);

  1588. /* create a duplicate handle for standard
  1589. output */
  1590. oldstdout = dup(STDOUT);
  1591. /*
  1592. redirect standard output to DUMMY.FIL
  1593. by duplicating the file handle onto the
  1594. file handle for standard output.
  1595. */
  1596. dup2(nul, STDOUT);

  1597. /* close the handle for DUMMY.FIL */
  1598. close(nul);

  1599. /* will be redirected into DUMMY.FIL */
  1600. write(STDOUT, msg, strlen(msg));

  1601. /* restore original standard output
  1602. handle */
  1603. dup2(oldstdout, STDOUT);

  1604. /* close duplicate handle for STDOUT */
  1605. close(oldstdout);

  1606. return 0;
  1607. }
  1608. 函数名: ecvt
  1609. 功 能: 把一个浮点数转换为字符串
  1610. 用 法: char ecvt(double value, int ndigit, int *decpt, int *sign);
  1611. 程序例: #include <stdlib.h>
  1612. #include <stdio.h>
  1613. #include <conio.h> int main(void)
  1614. {
  1615. char *string;
  1616. double value;
  1617. int dec, sign;
  1618. int ndig = 10; clrscr();
  1619. value = 9.876;
  1620. string = ecvt(value, ndig, &dec, &sign);
  1621. printf("string = %s dec = %d \
  1622. sign = %d\n", string, dec, sign); value = -123.45;
  1623. ndig= 15;
  1624. string = ecvt(value,ndig,&dec,&sign);
  1625. printf("string = %s dec = %d sign = %d\n",
  1626. string, dec, sign);
  1627. value = 0.6789e5; /* scientific
  1628. notation */
  1629. ndig = 5;
  1630. string = ecvt(value,ndig,&dec,&sign);
  1631. printf("string = %s dec = %d\
  1632. sign = %d\n", string, dec, sign);
  1633. return 0;
  1634. } 函数名: ellipse
  1635. 功 能: 画一椭圆
  1636. 用 法: void far ellipse(int x, int y, int stangle, int endangle,
  1637. int xradius, int yradius);
  1638. 程序例: #include <graphics.h>
  1639. #include <stdlib.h>
  1640. #include <stdio.h>
  1641. #include <conio.h> int main(void)
  1642. {
  1643. /* request auto detection */
  1644. int gdriver = DETECT, gmode, errorcode;
  1645. int midx, midy;
  1646. int stangle = 0, endangle = 360;
  1647. int xradius = 100, yradius = 50; /* initialize graphics, local variables */
  1648. initgraph(&gdriver, &gmode, ""); /* read result of initialization */
  1649. errorcode = graphresult();
  1650. if (errorcode != grOk)
  1651. /* an error occurred */
  1652. {
  1653. printf("Graphics error: %s\n",
  1654. grapherrormsg(errorcode));
  1655. printf("Press any key to halt:");
  1656. getch();
  1657. exit(1);
  1658. /* terminate with an error code */
  1659. }
  1660. midx = getmaxx() / 2;
  1661. midy = getmaxy() / 2;
  1662. setcolor(getmaxcolor()); /* draw ellipse */
  1663. ellipse(midx, midy, stangle, endangle,
  1664. xradius, yradius); /* clean up */
  1665. getch();
  1666. closegraph();
  1667. return 0;
  1668. } 函数名: enable
  1669. 功 能: 开放硬件中断
  1670. 用 法: void enable(void);
  1671. 程序例: /* ** NOTE:
  1672. This is an interrupt service routine. You can NOT compile this program
  1673. with Test Stack Overflow turned on and get an executable file which will
  1674. operate correctly.
  1675. */ #include <stdio.h>
  1676. #include <dos.h>
  1677. #include <conio.h> /* The clock tick interrupt */
  1678. #define INTR 0X1C void interrupt ( *oldhandler)(void); int count=0; void interrupt handler(void)
  1679. {
  1680. /*
  1681. disable interrupts during the handling of the interrupt
  1682. */
  1683. disable();
  1684. /* increase the global counter */
  1685. count++;
  1686. /*
  1687. re enable interrupts at the end of the handler
  1688. */
  1689. enable();
  1690. /* call the old routine */
  1691. oldhandler();
  1692. }
  1693. int main(void)
  1694. {
  1695. /* save the old interrupt vector */
  1696. oldhandler = getvect(INTR); /* install the new interrupt handler */
  1697. setvect(INTR, handler); /* loop until the counter exceeds 20 */
  1698. while (count < 20)
  1699. printf("count is %d\n",count); /* reset the old interrupt handler */
  1700. setvect(INTR, oldhandler); return 0;
  1701. } 函数名: eof
  1702. 功 能: 检测文件结束
  1703. 用 法: int eof(int *handle);
  1704. 程序例: #include <sys\stat.h>
  1705. #include <string.h>
  1706. #include <stdio.h>
  1707. #include <fcntl.h>
  1708. #include <io.h> int main(void)
  1709. {
  1710. int handle;
  1711. char msg[] = "This is a test";
  1712. char ch;
  1713. /* create a file */
  1714. handle = open("DUMMY.FIL",
  1715. O_CREAT O_RDWR,
  1716. S_IREAD S_IWRITE); /* write some data to the file */
  1717. write(handle, msg, strlen(msg)); /* seek to the beginning of the file */
  1718. lseek(handle, 0L, SEEK_SET); /*
  1719. reads chars from the file until hit EOF
  1720. */
  1721. do
  1722. {
  1723. read(handle, &ch, 1);
  1724. printf("%c", ch);
  1725. } while (!eof(handle)); close(handle);
  1726. return 0;
  1727. } 函数名: exec...
  1728. 功 能: 装入并运行其它程序的函数
  1729. 用 法: int execl(char *pathname, char *arg0, arg1, ..., argn, NULL);
  1730. int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,
  1731. char *envp[]);
  1732. int execlp(char *pathname, char *arg0, arg1, .., NULL);
  1733. int execple(char *pathname, char *arg0, arg1, ..., NULL,
  1734. char *envp[]);
  1735. int execv(char *pathname, char *argv[]);
  1736. int execve(char *pathname, char *argv[], char *envp[]);
  1737. int execvp(char *pathname, char *argv[]);
  1738. int execvpe(char *pathname, char *argv[], char *envp[]);
  1739. 程序例:
  1740. /* execv example */
  1741. #include <process.h>
  1742. #include <stdio.h>
  1743. #include <errno.h> void main(int argc, char *argv[])
  1744. {
  1745. int i; printf("Command line arguments:\n");
  1746. for (i=0; i<argc; i++)
  1747. printf("[%2d] : %s\n", i, argv[i]); printf("About to exec child with arg1 arg2 ...\n");
  1748. execv("CHILD.EXE", argv); perror("exec error"); exit(1);
  1749. } 函数名: exit
  1750. 功 能: 终止程序
  1751. 用 法: void exit(int status);
  1752. 程序例: #include <stdlib.h>
  1753. #include <conio.h>
  1754. #include <stdio.h> int main(void)
  1755. {
  1756. int status; printf("Enter either 1 or 2\n");
  1757. status = getch();
  1758. /* Sets DOS errorlevel */
  1759. exit(status - '0');
  1760. /* Note: this line is never reached */
  1761. return 0;
  1762. } 函数名: eXP
  1763. 功 能: 指数函数
  1764. 用 法: double exp(double x);
  1765. 程序例: #include <stdio.h>
  1766. #include <math.h> int main(void)
  1767. {
  1768. double result;
  1769. double x = 4.0; result = exp(x);
  1770. printf("'e' raised to the power \
  1771. of %lf (e ^ %lf) = %lf\n",
  1772. x, x, result); return 0;
  1773. }
  1774. 函数名: fabs
  1775. 功 能: 返回浮点数的绝对值
  1776. 用 法: double fabs(double x);
  1777. 程序例:
  1778. #include <stdio.h>
  1779. #include <math.h>

  1780. int main(void)
  1781. {
  1782. float number = -1234.0;

  1783. printf("number: %f absolute value: %f\n",
  1784. number, fabs(number));
  1785. return 0;
  1786. }


  1787. 函数名: farcalloc
  1788. 功 能: 从远堆栈中申请空间
  1789. 用 法: void far *farcalloc(unsigned long units, unsigned ling unitsz);
  1790. 程序例:
  1791. #include <stdio.h>
  1792. #include <alloc.h>
  1793. #include <string.h>
  1794. #include <dos.h>

  1795. int main(void)
  1796. {
  1797. char far *fptr;
  1798. char *str = "Hello";

  1799. /* allocate memory for the far pointer */
  1800. fptr = farcalloc(10, sizeof(char));

  1801. /* copy "Hello" into allocated memory */
  1802. /*
  1803. Note: movedata is used because you
  1804. might be in a small data model, in
  1805. which case a normal string copy routine
  1806. can not be used since it assumes the
  1807. pointer size is near.
  1808. */
  1809. movedata(FP_SEG(str), FP_OFF(str),
  1810. FP_SEG(fptr), FP_OFF(fptr),
  1811. strlen(str));

  1812. /* display string (note the F modifier) */
  1813. printf("Far string is: %Fs\n", fptr);

  1814. /* free the memory */
  1815. farfree(fptr);

  1816. return 0;
  1817. }


  1818. 函数名: farcoreleft
  1819. 功 能: 返回远堆中未作用存储区大小
  1820. 用 法: long farcoreleft(void);
  1821. 程序例:

  1822. #include <stdio.h>
  1823. #include <alloc.h>

  1824. int main(void)
  1825. {
  1826. printf("The difference between the\
  1827. highest allocated block in the\
  1828. far\n");
  1829. printf("heap and the top of the far heap\
  1830. is: %lu bytes\n", farcoreleft());

  1831. return 0;
  1832. }


  1833. 函数名: farfree
  1834. 功 能: 从远堆中释放一块
  1835. 用 法: void farfree(void);
  1836. 程序例:

  1837. #include <stdio.h>
  1838. #include <alloc.h>
  1839. #include <string.h>
  1840. #include <dos.h>

  1841. int main(void)
  1842. {
  1843. char far *fptr;
  1844. char *str = "Hello";

  1845. /* allocate memory for the far pointer */
  1846. fptr = farcalloc(10, sizeof(char));

  1847. /* copy "Hello" into allocated memory */
  1848. /*
  1849. Note: movedata is used because you might be in a small data model,
  1850. in which case a normal string copy routine can't be used since it
  1851. assumes the pointer size is near.
  1852. */
  1853. movedata(FP_SEG(str), FP_OFF(str),
  1854. FP_SEG(fptr), FP_OFF(fptr),
  1855. strlen(str));

  1856. /* display string (note the F modifier) */
  1857. printf("Far string is: %Fs\n", fptr);

  1858. /* free the memory */
  1859. farfree(fptr);

  1860. return 0;
  1861. }


  1862. 函数名: farmalloc
  1863. 功 能: 从远堆中分配存储块
  1864. 用 法: void far *farmalloc(unsigned long size);
  1865. 程序例:

  1866. #include <stdio.h>
  1867. #include <alloc.h>
  1868. #include <string.h>
  1869. #include <dos.h>

  1870. int main(void)
  1871. {
  1872. char far *fptr;
  1873. char *str = "Hello";

  1874. /* allocate memory for the far pointer */
  1875. fptr = farmalloc(10);

  1876. /* copy "Hello" into allocated memory */
  1877. /*
  1878. Note: movedata is used because we might
  1879. be in a small data model, in which case
  1880. a normal string copy routine can not be
  1881. used since it assumes the pointer size
  1882. is near.
  1883. */
  1884. movedata(FP_SEG(str), FP_OFF(str),
  1885. FP_SEG(fptr), FP_OFF(fptr),
  1886. strlen(str));

  1887. /* display string (note the F modifier) */
  1888. printf("Far string is: %Fs\n", fptr);

  1889. /* free the memory */
  1890. farfree(fptr);

  1891. return 0;
  1892. }


  1893. 函数名: farrealloc
  1894. 功 能: 调整远堆中的分配块
  1895. 用 法: void far *farrealloc(void far *block, unsigned long newsize);
  1896. 程序例:

  1897. #include <stdio.h>
  1898. #include <alloc.h>

  1899. int main(void)
  1900. {
  1901. char far *fptr;

  1902. fptr = farmalloc(10);
  1903. printf("First address: %Fp\n", fptr);
  1904. fptr = farrealloc(fptr,20);
  1905. printf("New address : %Fp\n", fptr);
  1906. farfree(fptr);
  1907. return 0;
  1908. }

  1909. 函数名: fclose
  1910. 功 能: 关闭一个流
  1911. 用 法: int fclose(FILE *stream);
  1912. 程序例:

  1913. #include <string.h>
  1914. #include <stdio.h>

  1915. int main(void)
  1916. {
  1917. FILE *fp;
  1918. char buf[11] = "0123456789";

  1919. /* create a file containing 10 bytes */
  1920. fp = fopen("DUMMY.FIL", "w");
  1921. fwrite(&buf, strlen(buf), 1, fp);

  1922. /* close the file */
  1923. fclose(fp);
  1924. return 0;
  1925. }


  1926. 函数名: fcloseall
  1927. 功 能: 关闭打开流
  1928. 用 法: int fcloseall(void);
  1929. 程序例:

  1930. #include <stdio.h>

  1931. int main(void)
  1932. {
  1933. int streams_closed;

  1934. /* open two streams */
  1935. fopen("DUMMY.ONE", "w");
  1936. fopen("DUMMY.TWO", "w");

  1937. /* close the open streams */
  1938. streams_closed = fcloseall();

  1939. if (streams_closed == EOF)
  1940. /* issue an error message */
  1941. perror("Error");
  1942. else
  1943. /* print result of fcloseall() function */
  1944. printf("%d streams were closed.\n", streams_closed);

  1945. return 0;
  1946. }

  1947. 函数名: fcvt
  1948. 功 能: 把一个浮点数转换为字符串
  1949. 用 法: char *fcvt(double value, int ndigit, int *decpt, int *sign);
  1950. 程序例:

  1951. #include <stdlib.h>
  1952. #include <stdio.h>
  1953. #include <conio.h>

  1954. int main(void)
  1955. {
  1956. char *string;
  1957. double value;
  1958. int dec, sign;
  1959. int ndig = 10;

  1960. clrscr();
  1961. value = 9.876;
  1962. string = ecvt(value, ndig, &dec, &sign);
  1963. printf("string = %s dec = %d \
  1964. sign = %d\n", string, dec, sign);

  1965. value = -123.45;
  1966. ndig= 15;
  1967. string = ecvt(value,ndig,&dec,&sign);
  1968. printf("string = %s dec = %d sign = %d\n",
  1969. string, dec, sign);


  1970. value = 0.6789e5; /* scientific
  1971. notation */
  1972. ndig = 5;
  1973. string = ecvt(value,ndig,&dec,&sign);
  1974. printf("string = %s dec = %d\
  1975. sign = %d\n", string, dec, sign);

  1976. return 0;
  1977. }


  1978. 函数名: fdopen
  1979. 功 能: 把流与一个文件句柄相接
  1980. 用 法: FILE *fdopen(int handle, char *type);
  1981. 程序例:

  1982. #include <sys\stat.h>
  1983. #include <stdio.h>
  1984. #include <fcntl.h>
  1985. #include <io.h>

  1986. int main(void)
  1987. {
  1988. int handle;
  1989. FILE *stream;

  1990. /* open a file */
  1991. handle = open("DUMMY.FIL", O_CREAT,
  1992. S_IREAD | S_IWRITE);

  1993. /* now turn the handle into a stream */
  1994. stream = fdopen(handle, "w");

  1995. if (stream == NULL)
  1996. printf("fdopen failed\n");
  1997. else
  1998. {
  1999. fprintf(stream, "Hello world\n");
  2000. fclose(stream);
  2001. }
  2002. return 0;
  2003. }

  2004. 函数名: feof
  2005. 功 能: 检测流上的文件结束符
  2006. 用 法: int feof(FILE *stream);
  2007. 程序例:

  2008. #include <stdio.h>

  2009. int main(void)
  2010. {
  2011. FILE *stream;

  2012. /* open a file for reading */
  2013. stream = fopen("DUMMY.FIL", "r");

  2014. /* read a character from the file */
  2015. fgetc(stream);

  2016. /* check for EOF */
  2017. if (feof(stream))
  2018. printf("We have reached end-of-file\n");

  2019. /* close the file */
  2020. fclose(stream);
  2021. return 0;
  2022. }

  2023. 函数名: ferror
  2024. 功 能: 检测流上的错误
  2025. 用 法: int ferror(FILE *stream);
  2026. 程序例:

  2027. #include <stdio.h>

  2028. int main(void)
  2029. {
  2030. FILE *stream;

  2031. /* open a file for writing */
  2032. stream = fopen("DUMMY.FIL", "w");

  2033. /* force an error condition by attempting to read */
  2034. (void) getc(stream);

  2035. if (ferror(stream)) /* test for an error on the stream */
  2036. {
  2037. /* display an error message */
  2038. printf("Error reading from DUMMY.FIL\n");

  2039. /* reset the error and EOF indicators */
  2040. clearerr(stream);
  2041. }

  2042. fclose(stream);
  2043. return 0;
  2044. }


  2045. 函数名: fflush
  2046. 功 能: 清除一个流
  2047. 用 法: int fflush(FILE *stream);
  2048. 程序例:

  2049. #include <string.h>
  2050. #include <stdio.h>
  2051. #include <conio.h>
  2052. #include <io.h>

  2053. void flush(FILE *stream);

  2054. int main(void)
  2055. {
  2056. FILE *stream;
  2057. char msg[] = "This is a test";

  2058. /* create a file */
  2059. stream = fopen("DUMMY.FIL", "w");

  2060. /* write some data to the file */
  2061. fwrite(msg, strlen(msg), 1, stream);

  2062. clrscr();
  2063. printf("Press any key to flush\
  2064. DUMMY.FIL:");
  2065. getch();

  2066. /* flush the data to DUMMY.FIL without\
  2067. closing it */
  2068. flush(stream);

  2069. printf("\nFile was flushed, Press any key\
  2070. to quit:");
  2071. getch();
  2072. return 0;
  2073. }

  2074. void flush(FILE *stream)
  2075. {
  2076. int duphandle;

  2077. /* flush the stream's internal buffer */
  2078. fflush(stream);

  2079. /* make a duplicate file handle */
  2080. duphandle = dup(fileno(stream));

  2081. /* close the duplicate handle to flush\
  2082. the DOS buffer */
  2083. close(duphandle);
  2084. }


  2085. 函数名: fgetc
  2086. 功 能: 从流中读取字符
  2087. 用 法: int fgetc(FILE *stream);
  2088. 程序例:

  2089. #include <string.h>
  2090. #include <stdio.h>
  2091. #include <conio.h>

  2092. int main(void)
  2093. {
  2094. FILE *stream;
  2095. char string[] = "This is a test";
  2096. char ch;

  2097. /* open a file for update */
  2098. stream = fopen("DUMMY.FIL", "w+");

  2099. /* write a string into the file */
  2100. fwrite(string, strlen(string), 1, stream);

  2101. /* seek to the beginning of the file */
  2102. fseek(stream, 0, SEEK_SET);

  2103. do
  2104. {
  2105. /* read a char from the file */
  2106. ch = fgetc(stream);

  2107. /* display the character */
  2108. putch(ch);
  2109. } while (ch != EOF);

  2110. fclose(stream);
  2111. return 0;
  2112. }


  2113. 函数名: fgetchar
  2114. 功 能: 从流中读取字符
  2115. 用 法: int fgetchar(void);
  2116. 程序例:

  2117. #include <stdio.h>

  2118. int main(void)
  2119. {
  2120. char ch;

  2121. /* prompt the user for input */
  2122. printf("Enter a character followed by \
  2123. <Enter>: ");

  2124. /* read the character from stdin */
  2125. ch = fgetchar();

  2126. /* display what was read */
  2127. printf("The character read is: '%c'\n",
  2128. ch);
  2129. return 0;
  2130. }


  2131. 函数名: fgetpos
  2132. 功 能: 取得当前文件的句柄
  2133. 用 法: int fgetpos(FILE *stream);
  2134. 程序例:

  2135. #include <string.h>
  2136. #include <stdio.h>

  2137. int main(void)
  2138. {
  2139. FILE *stream;
  2140. char string[] = "This is a test";
  2141. fpos_t filepos;

  2142. /* open a file for update */
  2143. stream = fopen("DUMMY.FIL", "w+");

  2144. /* write a string into the file */
  2145. fwrite(string, strlen(string), 1, stream);

  2146. /* report the file pointer position */
  2147. fgetpos(stream, &filepos);
  2148. printf("The file pointer is at byte\
  2149. %ld\n", filepos);

  2150. fclose(stream);
  2151. return 0;
  2152. }


  2153. 函数名: fgets
  2154. 功 能: 从流中读取一字符串
  2155. 用 法: char *fgets(char *string, int n, FILE *stream);
  2156. 程序例:

  2157. #include <string.h>
  2158. #include <stdio.h>

  2159. int main(void)
  2160. {
  2161. FILE *stream;
  2162. char string[] = "This is a test";
  2163. char msg[20];

  2164. /* open a file for update */
  2165. stream = fopen("DUMMY.FIL", "w+");

  2166. /* write a string into the file */
  2167. fwrite(string, strlen(string), 1, stream);

  2168. /* seek to the start of the file */
  2169. fseek(stream, 0, SEEK_SET);

  2170. /* read a string from the file */
  2171. fgets(msg, strlen(string)+1, stream);

  2172. /* display the string */
  2173. printf("%s", msg);

  2174. fclose(stream);
  2175. return 0;
  2176. }


  2177. 函数名: filelength
  2178. 功 能: 取文件长度字节数
  2179. 用 法: long filelength(int handle);
  2180. 程序例:

  2181. #include <string.h>
  2182. #include <stdio.h>
  2183. #include <fcntl.h>
  2184. #include <io.h>

  2185. int main(void)
  2186. {
  2187. int handle;
  2188. char buf[11] = "0123456789";

  2189. /* create a file containing 10 bytes */
  2190. handle = open("DUMMY.FIL", O_CREAT);
  2191. write(handle, buf, strlen(buf));

  2192. /* display the size of the file */
  2193. printf("file length in bytes: %ld\n",
  2194. filelength(handle));

  2195. /* close the file */
  2196. close(handle);
  2197. return 0;
  2198. }

  2199. 函数名: fillellipse
  2200. 功 能: 画出并填充一椭圆
  2201. 用 法: void far fillellipse(int x, int y, int xradius, int yradius);
  2202. 程序例:

  2203. #include <graphics.h>
  2204. #include <conio.h>

  2205. int main(void)
  2206. {
  2207. int gdriver = DETECT, gmode;
  2208. int xcenter, ycenter, i;

  2209. initgraph(&gdriver,&gmode,"");
  2210. xcenter = getmaxx() / 2;
  2211. ycenter = getmaxy() / 2;

  2212. for (i=0; i<13; i++)
  2213. {
  2214. setfillstyle(i,WHITE);
  2215. fillellipse(xcenter,ycenter,100,50);
  2216. getch();
  2217. }

  2218. closegraph();
  2219. return 0;
  2220. }
  2221. 函数名: fillpoly
  2222. 功 能: 画并填充一个多边形
  2223. 用 法: void far fillpoly(int numpoints, int far *polypoints);
  2224. 程序例:

  2225. #include <graphics.h>
  2226. #include <stdlib.h>
  2227. #include <stdio.h>
  2228. #include <conio.h>

  2229. int main(void)
  2230. {
  2231. /* request auto detection */
  2232. int gdriver = DETECT, gmode, errorcode;
  2233. int i, maxx, maxy;

  2234. /* our polygon array */
  2235. int poly[8];

  2236. /* initialize graphics, local variables */
  2237. initgraph(&gdriver, &gmode, "");

  2238. /* read result of initialization */
  2239. errorcode = graphresult();
  2240. if (errorcode != grOk)
  2241. /* an error occurred */
  2242. {
  2243. printf("Graphics error: %s\n",
  2244. grapherrormsg(errorcode));
  2245. printf("Press any key to halt:");
  2246. getch();
  2247. exit(1);
  2248. /* terminate with an error code */
  2249. }

  2250. maxx = getmaxx();
  2251. maxy = getmaxy();

  2252. poly[0] = 20; /* 1st vertext */
  2253. poly[1] = maxy / 2;

  2254. poly[2] = maxx - 20; /* 2nd */
  2255. poly[3] = 20;

  2256. poly[4] = maxx - 50; /* 3rd */
  2257. poly[5] = maxy - 20;

  2258. /*
  2259. 4th vertex. fillpoly automatically
  2260. closes the polygon.
  2261. */
  2262. poly[6] = maxx / 2;
  2263. poly[7] = maxy / 2;

  2264. /* loop through the fill patterns */
  2265. for (i=EMPTY_FILL; i<USER_FILL; i++)
  2266. {
  2267. /* set fill pattern */
  2268. setfillstyle(i, getmaxcolor());

  2269. /* draw a filled polygon */
  2270. fillpoly(4, poly);

  2271. getch();
  2272. }

  2273. /* clean up */
  2274. closegraph();
  2275. return 0;
  2276. }


  2277. 函数名: findfirst, findnext
  2278. 功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件
  2279. 用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);
  2280. int findnext(struct ffblk *ffblk);
  2281. 程序例:

  2282. /* findnext example */

  2283. #include <stdio.h>
  2284. #include <dir.h>

  2285. int main(void)
  2286. {
  2287. struct ffblk ffblk;
  2288. int done;
  2289. printf("Directory listing of *.*\n");
  2290. done = findfirst("*.*",&ffblk,0);
  2291. while (!done)
  2292. {
  2293. printf(" %s\n", ffblk.ff_name);
  2294. done = findnext(&ffblk);
  2295. }

  2296. return 0;
  2297. }


  2298. 函数名: floodfill
  2299. 功 能: 填充一个有界区域
  2300. 用 法: void far floodfill(int x, int y, int border);
  2301. 程序例:

  2302. #include <graphics.h>
  2303. #include <stdlib.h>
  2304. #include <stdio.h>
  2305. #include <conio.h>

  2306. int main(void)
  2307. {
  2308. /* request auto detection */
  2309. int gdriver = DETECT, gmode, errorcode;
  2310. int maxx, maxy;

  2311. /* initialize graphics, local variables */
  2312. initgraph(&gdriver, &gmode, "");

  2313. /* read result of initialization */
  2314. errorcode = graphresult();
  2315. if (errorcode != grOk)
  2316. /* an error occurred */
  2317. {
  2318. printf("Graphics error: %s\n",
  2319. grapherrormsg(errorcode));
  2320. printf("Press any key to halt:");
  2321. getch();
  2322. exit(1);
  2323. /* terminate with an error code */
  2324. }

  2325. maxx = getmaxx();
  2326. maxy = getmaxy();

  2327. /* select drawing color */
  2328. setcolor(getmaxcolor());

  2329. /* select fill color */
  2330. setfillstyle(SOLID_FILL, getmaxcolor());

  2331. /* draw a border around the screen */
  2332. rectangle(0, 0, maxx, maxy);

  2333. /* draw some circles */
  2334. circle(maxx / 3, maxy /2, 50);
  2335. circle(maxx / 2, 20, 100);
  2336. circle(maxx-20, maxy-50, 75);
  2337. circle(20, maxy-20, 25);

  2338. /* wait for a key */
  2339. getch();

  2340. /* fill in bounded region */
  2341. floodfill(2, 2, getmaxcolor());

  2342. /* clean up */
  2343. getch();
  2344. closegraph();
  2345. return 0;
  2346. }


  2347. 函数名: floor
  2348. 功 能: 向下舍入
  2349. 用 法: double floor(double x);
  2350. 程序例:

  2351. #include <stdio.h>
  2352. #include <math.h>

  2353. int main(void)
  2354. {
  2355. double number = 123.54;
  2356. double down, up;

  2357. down = floor(number);
  2358. up = ceil(number);

  2359. printf("original number %10.2lf\n",
  2360. number);
  2361. printf("number rounded down %10.2lf\n",
  2362. down);
  2363. printf("number rounded up %10.2lf\n",
  2364. up);

  2365. return 0;
  2366. }


  2367. 函数名: flushall
  2368. 功 能: 清除所有缓冲区
  2369. 用 法: int flushall(void);
  2370. 程序例:

  2371. #include <stdio.h>

  2372. int main(void)
  2373. {
  2374. FILE *stream;

  2375. /* create a file */
  2376. stream = fopen("DUMMY.FIL", "w");

  2377. /* flush all open streams */
  2378. printf("%d streams were flushed.\n",
  2379. flushall());

  2380. /* close the file */
  2381. fclose(stream);
  2382. return 0;
  2383. }


  2384. 函数名: fmod
  2385. 功 能: 计算x对y的模, 即x/y的余数
  2386. 用 法: double fmod(double x, double y);
  2387. 程序例:

  2388. #include <stdio.h>
  2389. #include <math.h>

  2390. int main(void)
  2391. {
  2392. double x = 5.0, y = 2.0;
  2393. double result;

  2394. result = fmod(x,y);
  2395. printf("The remainder of (%lf / %lf) is \
  2396. %lf\n", x, y, result);
  2397. return 0;
  2398. }


  2399. 函数名: fnmerge
  2400. 功 能: 建立新文件名
  2401. 用 法: void fnerge(char *path, char *drive, char *dir);
  2402. 程序例:

  2403. #include <string.h>
  2404. #include <stdio.h>
  2405. #include <dir.h>


  2406. int main(void)
  2407. {
  2408. char s[MAXPATH];
  2409. char drive[MAXDRIVE];
  2410. char dir[MAXDIR];
  2411. char file[MAXFILE];
  2412. char ext[MAXEXT];

  2413. getcwd(s,MAXPATH); /* get the current working directory */
  2414. strcat(s,"\"); /* append on a trailing \ character */
  2415. fnsplit(s,drive,dir,file,ext); /* split the string to separate elems */
  2416. strcpy(file,"DATA");
  2417. strcpy(ext,".TXT");
  2418. fnmerge(s,drive,dir,file,ext); /* merge everything into one string */
  2419. puts(s); /* display resulting string */

  2420. return 0;
  2421. }


  2422. 函数名: fopen
  2423. 功 能: 打开一个流
  2424. 用 法: FILE *fopen(char *filename, char *type);
  2425. 程序例:

  2426. #include <stdlib.h>
  2427. #include <stdio.h>
  2428. #include <dir.h>

  2429. int main(void)
  2430. {
  2431. char *s;
  2432. char drive[MAXDRIVE];
  2433. char dir[MAXDIR];
  2434. char file[MAXFILE];
  2435. char ext[MAXEXT];
  2436. int flags;

  2437. s=getenv("COMSPEC"); /* get the comspec environment parameter */
  2438. flags=fnsplit(s,drive,dir,file,ext);

  2439. printf("Command processor info:\n");
  2440. if(flags & DRIVE)
  2441. printf("\tdrive: %s\n",drive);
  2442. if(flags & DIRECTORY)
  2443. printf("\tdirectory: %s\n",dir);
  2444. if(flags & FILENAME)
  2445. printf("\tfile: %s\n",file);
  2446. if(flags & EXTENSION)
  2447. printf("\textension: %s\n",ext);

  2448. return 0;
  2449. }


  2450. 函数名: fprintf
  2451. 功 能: 传送格式化输出到一个流中
  2452. 用 法: int fprintf(FILE *stream, char *format[, argument,...]);
  2453. 程序例:

  2454. /* Program to create backup of the
  2455. AUTOEXEC.BAT file */

  2456. #include <stdio.h>

  2457. int main(void)
  2458. {
  2459. FILE *in, *out;

  2460. if ((in = fopen("\\AUTOEXEC.BAT", "rt"))
  2461. == NULL)
  2462. {
  2463. fprintf(stderr, "Cannot open input \
  2464. file.\n");
  2465. return 1;
  2466. }

  2467. if ((out = fopen("\\AUTOEXEC.BAK", "wt"))
  2468. == NULL)
  2469. {
  2470. fprintf(stderr, "Cannot open output \
  2471. file.\n");
  2472. return 1;
  2473. }

  2474. while (!feof(in))
  2475. fputc(fgetc(in), out);

  2476. fclose(in);
  2477. fclose(out);
  2478. return 0;
  2479. }


  2480. 函数名: FP_OFF
  2481. 功 能: 获取远地址偏移量
  2482. 用 法: unsigned FP_OFF(void far *farptr);
  2483. 程序例:

  2484. /* FP_OFF */

  2485. #include <dos.h>
  2486. #include <stdio.h>

  2487. int main(void)
  2488. {
  2489. char *str = "fpoff.c";

  2490. printf("The offset of this file in memory\
  2491. is: %Fp\n", FP_OFF(str));

  2492. return 0;
  2493. }

  2494. 函数名: FP_SEG
  2495. 功 能: 获取远地址段值
  2496. 用 法: unsigned FP_SEG(void far *farptr);
  2497. 程序例:

  2498. /* FP_SEG */

  2499. #include <dos.h>
  2500. #include <stdio.h>

  2501. int main(void)
  2502. {
  2503. char *filename = "fpseg.c";

  2504. printf("The offset of this file in memory\
  2505. is: %Fp\n", FP_SEG(filename));

  2506. return(0);
  2507. }


  2508. 函数名: fputc
  2509. 功 能: 送一个字符到一个流中
  2510. 用 法: int fputc(int ch, FILE *stream);
  2511. 程序例:

  2512. #include <stdio.h>

  2513. int main(void)
  2514. {
  2515. char msg[] = "Hello world";
  2516. int i = 0;

  2517. while (msg[i])
  2518. {
  2519. fputc(msg[i], stdout);
  2520. i++;
  2521. }
  2522. return 0;
  2523. }


  2524. 函数名: fputchar
  2525. 功 能: 送一个字符到标准输出流(stdout)中
  2526. 用 法: int fputchar(char ch);
  2527. 程序例:

  2528. #include <stdio.h>

  2529. int main(void)
  2530. {
  2531. char msg[] = "This is a test";
  2532. int i = 0;

  2533. while (msg[i])
  2534. {
  2535. fputchar(msg[i]);
  2536. i++;
  2537. }
  2538. return 0;
  2539. }


  2540. 函数名: fputs
  2541. 功 能: 送一个字符到一个流中
  2542. 用 法: int fputs(char *string, FILE *stream);
  2543. 程序例:

  2544. #include <stdio.h>

  2545. int main(void)
  2546. {
  2547. /* write a string to standard output */
  2548. fputs("Hello world\n", stdout);

  2549. return 0;
  2550. }


  2551. 函数名: fread
  2552. 功 能: 从一个流中读数据
  2553. 用 法: int fread(void *ptr, int size, int nitems, FILE *stream);
  2554. 程序例:

  2555. #include <string.h>
  2556. #include <stdio.h>

  2557. int main(void)
  2558. {
  2559. FILE *stream;
  2560. char msg[] = "this is a test";
  2561. char buf[20];

  2562. if ((stream = fopen("DUMMY.FIL", "w+"))
  2563. == NULL)
  2564. {
  2565. fprintf(stderr,
  2566. "Cannot open output file.\n");
  2567. return 1;
  2568. }

  2569. /* write some data to the file */
  2570. fwrite(msg, strlen(msg)+1, 1, stream);

  2571. /* seek to the beginning of the file */
  2572. fseek(stream, SEEK_SET, 0);

  2573. /* read the data and display it */
  2574. fread(buf, strlen(msg)+1, 1, stream);
  2575. printf("%s\n", buf);

  2576. fclose(stream);
  2577. return 0;
  2578. }

  2579. 函数名: free
  2580. 功 能: 释放已分配的块
  2581. 用 法: void free(void *ptr);
  2582. 程序例:

  2583. #include <string.h>
  2584. #include <stdio.h>
  2585. #include <alloc.h>

  2586. int main(void)
  2587. {
  2588. char *str;

  2589. /* allocate memory for string */
  2590. str = malloc(10);

  2591. /* copy "Hello" to string */
  2592. strcpy(str, "Hello");

  2593. /* display string */
  2594. printf("String is %s\n", str);

  2595. /* free memory */
  2596. free(str);

  2597. return 0;
  2598. }


  2599. 函数名: freemem
  2600. 功 能: 释放先前分配的DOS内存块
  2601. 用 法: int freemem(unsigned seg);
  2602. 程序例:

  2603. #include <dos.h>
  2604. #include <alloc.h>
  2605. #include <stdio.h>

  2606. int main(void)
  2607. {
  2608. unsigned int size, segp;
  2609. int stat;

  2610. size = 64; /* (64 x 16) = 1024 bytes */
  2611. stat = allocmem(size, &segp);
  2612. if (stat < 0)
  2613. printf("Allocated memory at segment:\
  2614. %x\n", segp);
  2615. else
  2616. printf("Failed: maximum number of\
  2617. paragraphs available is %u\n",
  2618. stat);
  2619. freemem(segp);

  2620. return 0;
  2621. }

  2622. 函数名: freopen
  2623. 功 能: 替换一个流
  2624. 用 法: FILE *freopen(char *filename, char *type, FILE *stream);
  2625. 程序例:

  2626. #include <stdio.h>

  2627. int main(void)
  2628. {
  2629. /* redirect standard output to a file */
  2630. if (freopen("OUTPUT.FIL", "w", stdout)
  2631. == NULL)
  2632. fprintf(stderr, "error redirecting\
  2633. stdout\n");

  2634. /* this output will go to a file */
  2635. printf("This will go into a file.");

  2636. /* close the standard output stream */
  2637. fclose(stdout);

  2638. return 0;
  2639. }


  2640. 函数名: frexp
  2641. 功 能: 把一个双精度数分解为尾数的指数
  2642. 用 法: double frexp(double value, int *eptr);
  2643. 程序例:

  2644. #include <math.h>
  2645. #include <stdio.h>

  2646. int main(void)
  2647. {
  2648. double mantissa, number;
  2649. int exponent;

  2650. number = 8.0;
  2651. mantissa = frexp(number, &exponent);

  2652. printf("The number %lf is ", number);
  2653. printf("%lf times two to the ", mantissa);
  2654. printf("power of %d\n", exponent);

  2655. return 0;
  2656. }

  2657. 函数名: fscanf
  2658. 功 能: 从一个流中执行格式化输入
  2659. 用 法: int fscanf(FILE *stream, char *format[,argument...]);
  2660. 程序例:

  2661. #include <stdlib.h>
  2662. #include <stdio.h>

  2663. int main(void)
  2664. {
  2665. int i;

  2666. printf("Input an integer: ");

  2667. /* read an integer from the
  2668. standard input stream */
  2669. if (fscanf(stdin, "%d", &i))
  2670. printf("The integer read was: %i\n",
  2671. i);
  2672. else
  2673. {
  2674. fprintf(stderr, "Error reading an \
  2675. integer from stdin.\n");
  2676. exit(1);
  2677. }
  2678. return 0;
  2679. }


  2680. 函数名: fseek
  2681. 功 能: 重定位流上的文件指针
  2682. 用 法: int fseek(FILE *stream, long offset, int fromwhere);
  2683. 程序例:

  2684. #include <stdio.h>

  2685. long filesize(FILE *stream);

  2686. int main(void)
  2687. {
  2688. FILE *stream;

  2689. stream = fopen("MYFILE.TXT", "w+");
  2690. fprintf(stream, "This is a test");
  2691. printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
  2692. fclose(stream);
  2693. return 0;
  2694. }

  2695. long filesize(FILE *stream)
  2696. {
  2697. long curpos, length;

  2698. curpos = ftell(stream);
  2699. fseek(stream, 0L, SEEK_END);
  2700. length = ftell(stream);
  2701. fseek(stream, curpos, SEEK_SET);
  2702. return length;
  2703. }


  2704. 函数名: fsetpos
  2705. 功 能: 定位流上的文件指针
  2706. 用 法: int fsetpos(FILE *stream, const fpos_t *pos);
  2707. 程序例:

  2708. #include <stdlib.h>
  2709. #include <stdio.h>

  2710. void showpos(FILE *stream);

  2711. int main(void)
  2712. {
  2713. FILE *stream;
  2714. fpos_t filepos;

  2715. /* open a file for update */
  2716. stream = fopen("DUMMY.FIL", "w+");

  2717. /* save the file pointer position */
  2718. fgetpos(stream, &filepos);

  2719. /* write some data to the file */
  2720. fprintf(stream, "This is a test");

  2721. /* show the current file position */
  2722. showpos(stream);

  2723. /* set a new file position, display it */
  2724. if (fsetpos(stream, &filepos) == 0)
  2725. showpos(stream);
  2726. else
  2727. {
  2728. fprintf(stderr, "Error setting file \
  2729. pointer.\n");
  2730. exit(1);
  2731. }

  2732. /* close the file */
  2733. fclose(stream);
  2734. return 0;
  2735. }

  2736. void showpos(FILE *stream)
  2737. {
  2738. fpos_t pos;

  2739. /* display the current file pointer
  2740. position of a stream */
  2741. fgetpos(stream, &pos);
  2742. printf("File position: %ld\n", pos);
  2743. }


  2744. 函数名: fstat
  2745. 功 能: 获取打开文件信息
  2746. 用 法: int fstat(char *handle, struct stat *buff);
  2747. 程序例:

  2748. #include <sys\stat.h>
  2749. #include <stdio.h>
  2750. #include <time.h>

  2751. int main(void)
  2752. {
  2753. struct stat statbuf;
  2754. FILE *stream;

  2755. /* open a file for update */
  2756. if ((stream = fopen("DUMMY.FIL", "w+"))
  2757. == NULL)
  2758. {
  2759. fprintf(stderr, "Cannot open output \
  2760. file.\n");
  2761. return(1);
  2762. }
  2763. fprintf(stream, "This is a test");
  2764. fflush(stream);

  2765. /* get information about the file */
  2766. fstat(fileno(stream), &statbuf);
  2767. fclose(stream);

  2768. /* display the information returned */
  2769. if (statbuf.st_mode & S_IFCHR)
  2770. printf("Handle refers to a device.\n");
  2771. if (statbuf.st_mode & S_IFREG)
  2772. printf("Handle refers to an ordinary \
  2773. file.\n");
  2774. if (statbuf.st_mode & S_IREAD)
  2775. printf("User has read permission on \
  2776. file.\n");
  2777. if (statbuf.st_mode & S_IWRITE)
  2778. printf("User has write permission on \
  2779. file.\n");

  2780. printf("Drive letter of file: %c\n",
  2781. 'A'+statbuf.st_dev);
  2782. printf("Size of file in bytes: %ld\n",
  2783. statbuf.st_size);
  2784. printf("Time file last opened: %s\n",
  2785. ctime(&statbuf.st_ctime));
  2786. return 0;
  2787. }


  2788. 函数名: ftell
  2789. 功 能: 返回当前文件指针
  2790. 用 法: long ftell(FILE *stream);
  2791. 程序例:

  2792. #include <stdio.h>

  2793. int main(void)
  2794. {
  2795. FILE *stream;

  2796. stream = fopen("MYFILE.TXT", "w+");
  2797. fprintf(stream, "This is a test");
  2798. printf("The file pointer is at byte \
  2799. %ld\n", ftell(stream));
  2800. fclose(stream);
  2801. return 0;
  2802. }


  2803. 函数名: fwrite
  2804. 功 能: 写内容到流中
  2805. 用 法: int fwrite(void *ptr, int size, int nitems, FILE *stream);
  2806. 程序例:

  2807. #include <stdio.h>

  2808. struct mystruct
  2809. {
  2810. int i;
  2811. char ch;
  2812. };

  2813. int main(void)
  2814. {
  2815. FILE *stream;
  2816. struct mystruct s;

  2817. if ((stream = fopen("TEST.
  2818. $","wb"))==NULL)/∗openfileTEST.
  2819. $
  2820. "
  2821. ,
  2822. "


  2823. "
  2824. )
  2825. )
  2826. ==




  2827. )
  2828. /













  2829. .
  2830. $ */
  2831. {
  2832. fprintf(stderr, "Cannot open output file.\n");
  2833. return 1;
  2834. }
  2835. s.i = 0;
  2836. s.ch = 'A';
  2837. fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
  2838. fclose(stream); /* close file */
  2839. return 0;
  2840. }函数名: gcvt
  2841. 功 能: 把浮点数转换成字符串
  2842. 用 法: char *gcvt(double value, int ndigit, char *buf);
  2843. 程序例:
  2844. #include <stdlib.h>
  2845. #include <stdio.h>

  2846. int main(void)
  2847. {
  2848. char str[25];
  2849. double num;
  2850. int sig = 5; /* significant digits */

  2851. /* a regular number */
  2852. num = 9.876;
  2853. gcvt(num, sig, str);
  2854. printf("string = %s\n", str);

  2855. /* a negative number */
  2856. num = -123.4567;
  2857. gcvt(num, sig, str);
  2858. printf("string = %s\n", str);

  2859. /* scientific notation */
  2860. num = 0.678e5;
  2861. gcvt(num, sig, str);
  2862. printf("string = %s\n", str);

  2863. return(0);
  2864. }


  2865. 函数名: geninterrupt
  2866. 功 能: 产生一个软中断
  2867. 用 法: void geninterrupt(int intr_num);
  2868. 程序例:

  2869. #include <conio.h>
  2870. #include <dos.h>

  2871. /* function prototype */
  2872. void writechar(char ch);

  2873. int main(void)
  2874. {
  2875. clrscr();
  2876. gotoxy(80,25);
  2877. writechar('*');
  2878. getch();
  2879. return 0;
  2880. }

  2881. /*
  2882. outputs a character at the current cursor
  2883. position using the video BIOS to avoid the
  2884. scrolling of the screen when writing to
  2885. location (80,25).
  2886. */

  2887. void writechar(char ch)
  2888. {
  2889. struct text_info ti;
  2890. /* grab current text settings */
  2891. gettextinfo(&ti);
  2892. /* interrupt 0x10 sub-function 9 */
  2893. _AH = 9;
  2894. /* character to be output */
  2895. _AL = ch;
  2896. _BH = 0; /* video page */
  2897. _BL = ti.attribute; /* video attribute */
  2898. _CX = 1; /* repetition factor */
  2899. geninterrupt(0x10); /* output the char */
  2900. }

  2901. 函数名: getarccoords
  2902. 功 能: 取得最后一次调用arc的坐标
  2903. 用 法: void far getarccoords(struct arccoordstype far *arccoords);
  2904. 程序例:

  2905. #include <graphics.h>
  2906. #include <stdlib.h>
  2907. #include <stdio.h>
  2908. #include <conio.h>

  2909. int main(void)
  2910. {
  2911. /* request auto detection */
  2912. int gdriver = DETECT, gmode, errorcode;
  2913. struct arccoordstype arcinfo;
  2914. int midx, midy;
  2915. int stangle = 45, endangle = 270;
  2916. char sstr[80], estr[80];

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

  2919. /* read result of initialization */
  2920. errorcode = graphresult();
  2921. /* an error occurred */
  2922. if (errorcode != grOk)
  2923. {
  2924. printf("Graphics error: %s\n",
  2925. grapherrormsg(errorcode));
  2926. printf("Press any key to halt:");
  2927. getch();
  2928. /* terminate with an error code */
  2929. exit(1);
  2930. }

  2931. midx = getmaxx() / 2;
  2932. midy = getmaxy() / 2;

  2933. /* draw arc and get coordinates */
  2934. setcolor(getmaxcolor());
  2935. arc(midx, midy, stangle, endangle, 100);
  2936. getarccoords(&arcinfo);

  2937. /* convert arc information into strings */
  2938. sprintf(sstr, "*- (%d, %d)",
  2939. arcinfo.xstart, arcinfo.ystart);
  2940. sprintf(estr, "*- (%d, %d)",
  2941. arcinfo.xend, arcinfo.yend);

  2942. /* output the arc information */
  2943. outtextxy(arcinfo.xstart,
  2944. arcinfo.ystart, sstr);
  2945. outtextxy(arcinfo.xend,
  2946. arcinfo.yend, estr);

  2947. /* clean up */
  2948. getch();
  2949. closegraph();
  2950. return 0;
  2951. }


  2952. 函数名: getaspectratio
  2953. 功 能: 返回当前图形模式的纵横比
  2954. 用 法: void far getaspectratio(int far *xasp, int far *yasp);
  2955. 程序例:

  2956. #include <graphics.h>
  2957. #include <stdlib.h>
  2958. #include <stdio.h>
  2959. #include <conio.h>

  2960. int main(void)
  2961. {
  2962. /* request auto detection */
  2963. int gdriver = DETECT, gmode, errorcode;
  2964. int xasp, yasp, midx, midy;

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

  2967. /* read result of initialization */
  2968. errorcode = graphresult();
  2969. /* an error occurred */
  2970. if (errorcode != grOk)
  2971. {
  2972. printf("Graphics error: %s\n",
  2973. grapherrormsg(errorcode));
  2974. printf("Press any key to halt:");
  2975. getch();
  2976. /* terminate with an error code */
  2977. exit(1);
  2978. }

  2979. midx = getmaxx() / 2;
  2980. midy = getmaxy() / 2;
  2981. setcolor(getmaxcolor());

  2982. /* get current aspect ratio settings */
  2983. getaspectratio(&xasp, &yasp);

  2984. /* draw normal circle */
  2985. circle(midx, midy, 100);
  2986. getch();

  2987. /* draw wide circle */
  2988. cleardevice();
  2989. setaspectratio(xasp/2, yasp);
  2990. circle(midx, midy, 100);
  2991. getch();

  2992. /* draw narrow circle */
  2993. cleardevice();
  2994. setaspectratio(xasp, yasp/2);
  2995. circle(midx, midy, 100);

  2996. /* clean up */
  2997. getch();
  2998. closegraph();
  2999. return 0;
  3000. }


  3001. 函数名: getbkcolor
  3002. 功 能: 返回当前背景颜色
  3003. 用 法: int far getbkcolor(void);
  3004. 程序例:

  3005. #include <graphics.h>
  3006. #include <stdlib.h>
  3007. #include <string.h>
  3008. #include <stdio.h>
  3009. #include <conio.h>

  3010. int main(void)
  3011. {
  3012. /* request auto detection */
  3013. int gdriver = DETECT, gmode, errorcode;
  3014. int bkcolor, midx, midy;
  3015. char bkname[35];

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

  3018. /* read result of initialization */
  3019. errorcode = graphresult();
  3020. /* an error occurred */
  3021. if (errorcode != grOk)
  3022. {
  3023. printf("Graphics error: %s\n",
  3024. grapherrormsg(errorcode));
  3025. printf("Press any key to halt:");
  3026. getch();
  3027. /* terminate with an error code */
  3028. exit(1);
  3029. }

  3030. midx = getmaxx() / 2;
  3031. midy = getmaxy() / 2;
  3032. setcolor(getmaxcolor());

  3033. /* for centering text on the display */
  3034. settextjustify(CENTER_TEXT, CENTER_TEXT);

  3035. /* get the current background color */
  3036. bkcolor = getbkcolor();

  3037. /* convert color value into a string */
  3038. itoa(bkcolor, bkname, 10);
  3039. strcat(bkname,
  3040. " is the current background color.");

  3041. /* display a message */
  3042. outtextxy(midx, midy, bkname);

  3043. /* clean up */
  3044. getch();
  3045. closegraph();
  3046. return 0;
  3047. }


  3048. 函数名: getc
  3049. 功 能: 从流中取字符
  3050. 用 法: int getc(FILE *stream);
  3051. 程序例:

  3052. #include <stdio.h>

  3053. int main(void)
  3054. {
  3055. char ch;

  3056. printf("Input a character:");
  3057. /* read a character from the
  3058. standard input stream */
  3059. ch = getc(stdin);
  3060. printf("The character input was: '%c'\n",
  3061. ch);
  3062. return 0;
  3063. }


  3064. 函数名: getcbrk
  3065. 功 能: 获取Control_break设置
  3066. 用 法: int getcbrk(void);
  3067. 程序例: #include <stdio.h>
  3068. #include <dos.h>

  3069. int main(void)
  3070. {
  3071. if (getcbrk())
  3072. printf("Cntrl-brk flag is on\n");
  3073. else
  3074. printf("Cntrl-brk flag is off\n");

  3075. return 0;
  3076. }

  3077. 函数名: getch
  3078. 功 能: 从控制台无回显地取一个字符
  3079. 用 法: int getch(void);
  3080. 程序例:

  3081. #include <stdio.h>
  3082. #include <conio.h>

  3083. int main(void)
  3084. {
  3085. char ch;

  3086. printf("Input a character:");
  3087. ch = getche();
  3088. printf("\nYou input a '%c'\n", ch);
  3089. return 0;
  3090. }

  3091. 函数名: getchar
  3092. 功 能: 从stdin流中读字符
  3093. 用 法: int getchar(void);
  3094. 程序例:

  3095. #include <stdio.h>

  3096. int main(void)
  3097. {
  3098. int c;

  3099. /* Note that getchar reads from stdin and
  3100. is line buffered; this means it will
  3101. not return until you press ENTER. */

  3102. while ((c = getchar()) != '\n')
  3103. printf("%c", c);

  3104. return 0;
  3105. }

  3106. 函数名: getche
  3107. 功 能: 从控制台取字符(带回显)
  3108. 用 法: int getche(void);
  3109. 程序例:

  3110. #include <stdio.h>
  3111. #include <conio.h>

  3112. int main(void)
  3113. {
  3114. char ch;

  3115. printf("Input a character:");
  3116. ch = getche();
  3117. printf("\nYou input a '%c'\n", ch);
  3118. return 0;
  3119. }

  3120. 函数名: getcolor
  3121. 功 能: 返回当前画线颜色
  3122. 用 法: int far getcolor(void);
  3123. 程序例:

  3124. #include <graphics.h>
  3125. #include <stdlib.h>
  3126. #include <string.h>
  3127. #include <stdio.h>
  3128. #include <conio.h>

  3129. int main(void)
  3130. {
  3131. /* request auto detection */
  3132. int gdriver = DETECT, gmode, errorcode;
  3133. int color, midx, midy;
  3134. char colname[35];

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

  3137. /* read result of initialization */
  3138. errorcode = graphresult();
  3139. /* an error occurred */
  3140. if (errorcode != grOk)
  3141. {
  3142. printf("Graphics error: %s\n",
  3143. grapherrormsg(errorcode));
  3144. printf("Press any key to halt:");
  3145. getch();
  3146. /* terminate with an error code */
  3147. exit(1);
  3148. }

  3149. midx = getmaxx() / 2;
  3150. midy = getmaxy() / 2;
  3151. setcolor(getmaxcolor());

  3152. /* for centering text on the display */
  3153. settextjustify(CENTER_TEXT, CENTER_TEXT);

  3154. /* get the current drawing color */
  3155. color = getcolor();

  3156. /* convert color value into a string */
  3157. itoa(color, colname, 10);
  3158. strcat(colname,
  3159. " is the current drawing color.");

  3160. /* display a message */
  3161. outtextxy(midx, midy, colname);

  3162. /* clean up */
  3163. getch();
  3164. closegraph();
  3165. return 0;
  3166. }

  3167. 函数名: getcurdir
  3168. 功 能: 取指定驱动器的当前目录
  3169. 用 法: int getcurdir(int drive, char *direc);
  3170. 程序例:

  3171. #include <dir.h>
  3172. #include <stdio.h>
  3173. #include <string.h>

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

  3181. int main(void)
  3182. {
  3183. char curdir[MAXPATH];

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

  3186. return 0;
  3187. }

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

  3192. #include <stdio.h>
  3193. #include <dir.h>

  3194. int main(void)
  3195. {
  3196. char buffer[MAXPATH];

  3197. getcwd(buffer, MAXPATH);
  3198. printf("The current directory is: %s\n", buffer);
  3199. return 0;
  3200. }
  3201. 函数名: getdate
  3202. 功 能: 取DOS日期
  3203. 用 法: void getdate(struct *dateblk);
  3204. 程序例:

  3205. #include <dos.h>
  3206. #include <stdio.h>

  3207. int main(void)
  3208. {
  3209. struct date d;

  3210. getdate(&d);
  3211. printf("The current year is: %d\n",
  3212. d.da_year);
  3213. printf("The current day is: %d\n",
  3214. d.da_day);
  3215. printf("The current month is: %d\n",
  3216. d.da_mon);
  3217. return 0;
  3218. }

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

  3223. #include <graphics.h>
  3224. #include <stdlib.h>
  3225. #include <stdio.h>
  3226. #include <conio.h>

  3227. int main(void)
  3228. {
  3229. /* request auto detection */
  3230. int gdriver = DETECT, gmode, errorcode;
  3231. int i;

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

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

  3236. /* read result of initialization */
  3237. errorcode = graphresult();
  3238. /* an error occurred */
  3239. if (errorcode != grOk)
  3240. {
  3241. printf("Graphics error: %s\n",
  3242. grapherrormsg(errorcode));
  3243. printf("Press any key to halt:");
  3244. getch();
  3245. /* terminate with an error code */
  3246. exit(1);
  3247. }

  3248. setcolor(getmaxcolor());

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

  3251. for (i=0; i<16; i++)
  3252. {
  3253. printf("colors[%d] = %d\n", i,
  3254. pal->colors[i]);
  3255. getch();
  3256. }

  3257. /* clean up */
  3258. getch();
  3259. closegraph();
  3260. return 0;
  3261. }

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

  3266. #include <stdio.h>
  3267. #include <dir.h>

  3268. int main(void)
  3269. {
  3270. int disk;

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


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

  3280. #include <graphics.h>
  3281. #include <stdlib.h>
  3282. #include <stdio.h>
  3283. #include <conio.h>

  3284. int main(void)
  3285. {
  3286. /* request auto detection */
  3287. int gdriver = DETECT, gmode, errorcode;

  3288. /* stores the device driver name */
  3289. char *drivername;

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

  3292. /* read result of initialization */
  3293. errorcode = graphresult();
  3294. /* an error occurred */
  3295. if (errorcode != grOk)
  3296. {
  3297. printf("Graphics error: %s\n",
  3298. grapherrormsg(errorcode));
  3299. printf("Press any key to halt:");
  3300. getch();
  3301. /* terminate with an error code */
  3302. exit(1);
  3303. }

  3304. setcolor(getmaxcolor());

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

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

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

  3312. /* clean up */
  3313. getch();
  3314. closegraph();
  3315. return 0;
  3316. }

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

  3321. #include <dos.h>
  3322. #include <stdio.h>

  3323. int main(void)
  3324. {
  3325. char far *dta;

  3326. dta = getdta();
  3327. printf("The current disk transfer \
  3328. address is: %Fp\n", dta);
  3329. return 0;
  3330. }

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

  3335. #include <stdlib.h>
  3336. #include <stdio.h>


  3337. int main(void)
  3338. {
  3339. char *s;

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

  3342. return 0;
  3343. }


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

  3348. #include <stdio.h>
  3349. #include <dos.h>

  3350. int main(void)
  3351. {
  3352. struct fatinfo diskinfo;
  3353. int flag = 0;

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

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

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

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

  3367. default:
  3368. printf("unformatted\n");
  3369. flag = 1;
  3370. }

  3371. if (!flag)
  3372. {
  3373. printf(" sectors per cluster %5d\n",
  3374. diskinfo.fi_sclus);
  3375. printf(" number of clusters %5d\n",
  3376. diskinfo.fi_nclus);
  3377. printf(" bytes per sector %5d\n",
  3378. diskinfo.fi_bysec);
  3379. }

  3380. return 0;
  3381. }

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

  3386. #include <graphics.h>
  3387. #include <stdlib.h>
  3388. #include <stdio.h>
  3389. #include <conio.h>

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

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

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

  3407. maxx = getmaxx();
  3408. maxy = getmaxy();
  3409. setcolor(getmaxcolor());

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

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

  3414. getch();

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

  3417. /* alter the pattern we grabbed */
  3418. pattern[4] -= 1;
  3419. pattern[5] -= 3;
  3420. pattern[6] += 3;
  3421. pattern[7] -= 4;

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

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

  3426. /* clean up */
  3427. getch();
  3428. closegraph();
  3429. return 0;
  3430. }

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

  3435. #include <graphics.h>
  3436. #include <stdlib.h>
  3437. #include <stdio.h>
  3438. #include <conio.h>

  3439. / the names of the fill styles supported */
  3440. char *fname[] = { "EMPTY_FILL",
  3441. "SOLID_FILL",
  3442. "LINE_FILL",
  3443. "LTSLASH_FILL",
  3444. "SLASH_FILL",
  3445. "BKSLASH_FILL",
  3446. "LTBKSLASH_FILL",
  3447. "HATCH_FILL",
  3448. "XHATCH_FILL",
  3449. "INTERLEAVE_FILL",
  3450. "WIDE_DOT_FILL",
  3451. "CLOSE_DOT_FILL",
  3452. "USER_FILL"
  3453. };

  3454. int main(void)
  3455. {
  3456. /* request auto detection */
  3457. int gdriver = DETECT, gmode, errorcode;
  3458. struct fillsettingstype fillinfo;
  3459. int midx, midy;
  3460. char patstr[40], colstr[40];

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

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

  3472. midx = getmaxx() / 2;
  3473. midy = getmaxy() / 2;

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

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

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

  3483. /* clean up */
  3484. getch();
  3485. closegraph();
  3486. return 0;
  3487. }


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

  3492. #include <stdio.h>
  3493. #include <io.h>

  3494. int main(void)
  3495. {
  3496. FILE *stream;
  3497. struct ftime ft;

  3498. if ((stream = fopen("TEST.$",
  3499. "wt")) == NULL)
  3500. {
  3501. fprintf(stderr,
  3502. "Cannot open output file.\n");
  3503. return 1;
  3504. }
  3505. getftime(fileno(stream), &ft);
  3506. printf("File time: %u:%u:%u\n",
  3507. ft.ft_hour, ft.ft_min,
  3508. ft.ft_tsec * 2);
  3509. printf("File date: %u/%u/%u\n",
  3510. ft.ft_month, ft.ft_day,
  3511. ft.ft_year+1980);
  3512. fclose(stream);
  3513. return 0;
  3514. }


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

  3519. #include <graphics.h>
  3520. #include <stdlib.h>
  3521. #include <stdio.h>
  3522. #include <conio.h>

  3523. int main(void)
  3524. {
  3525. /* request auto detection */
  3526. int gdriver = DETECT, gmode, errorcode;
  3527. int midx, midy, mode;
  3528. char numname[80], modename[80];

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

  3531. /* read result of initialization */
  3532. errorcode = graphresult();
  3533. /* an error occurred */
  3534. if (errorcode != grOk)
  3535. {
  3536. printf("Graphics error: %s\n",
  3537. grapherrormsg(errorcode));
  3538. printf("Press any key to halt:");
  3539. getch();
  3540. /* terminate with an error code */
  3541. exit(1);
  3542. }

  3543. midx = getmaxx() / 2;
  3544. midy = getmaxy() / 2;

  3545. /* get mode number and name strings */
  3546. mode = getgraphmode();
  3547. sprintf(numname,
  3548. "%d is the current mode number.",
  3549. mode);
  3550. sprintf(modename,
  3551. "%s is the current graphics mode",
  3552. getmodename(mode));

  3553. /* display the information */
  3554. settextjustify(CENTER_TEXT, CENTER_TEXT);
  3555. outtextxy(midx, midy, numname);
  3556. outtextxy(midx, midy+2*textheight("W"),
  3557. modename);

  3558. /* clean up */
  3559. getch();
  3560. closegraph();
  3561. return 0;
  3562. }

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

  3567. #include <stdio.h>
  3568. #include <io.h>

  3569. int main(void)
  3570. {
  3571. FILE *stream;
  3572. struct ftime ft;

  3573. if ((stream = fopen("TEST.$",
  3574. "wt")) == NULL)
  3575. {
  3576. fprintf(stderr,
  3577. "Cannot open output file.\n");
  3578. return 1;
  3579. }
  3580. getftime(fileno(stream), &ft);
  3581. printf("File time: %u:%u:%u\n",
  3582. ft.ft_hour, ft.ft_min,
  3583. ft.ft_tsec * 2);
  3584. printf("File date: %u/%u/%u\n",
  3585. ft.ft_month, ft.ft_day,
  3586. ft.ft_year+1980);
  3587. fclose(stream);
  3588. return 0;
  3589. }
  3590. 函数名: harderr  
  3591. 功  能: 建立一个硬件错误处理程序  
  3592. 用  法: void harderr(int (*fptr)());  
  3593. 程序例:  
  3594. /*This program will trap disk errors and prompt  
  3595. the user for action. Try running it with no  
  3596. disk in drive A: to invoke its functions.*/  

  3597. #include <stdio.h>  
  3598. #include <conio.h>  
  3599. #include <dos.h>  
  3600. #define IGNORE  0  
  3601. #define RETRY   1  
  3602. #define ABORT   2  
  3603. int buf[500];  
  3604. /*define the error messages for trapping disk problems*/  
  3605. static char *err_msg[] = {  
  3606.     "write protect",  
  3607.     "unknown unit",  
  3608.     "drive not ready",  
  3609.     "unknown command",  
  3610.     "data error (CRC)",  
  3611.     "bad request",  
  3612.     "seek error",  
  3613.     "unknown media type",  
  3614.     "sector not found",  
  3615.     "printer out of paper",  
  3616.     "write fault",  
  3617.     "read fault",  
  3618.     "general failure",  
  3619.     "reserved",  
  3620.     "reserved",  
  3621.     "invalid disk change"  
  3622. };  

  3623. error_win(char *msg)  
  3624. {  
  3625.    int retval;  

  3626.    cputs(msg);  

  3627. /*prompt for user to press a key to abort, retry, ignore*/  
  3628.    while(1)  
  3629.    {  
  3630.        retval= getch();  
  3631.        if (retval == 'a' || retval == 'A')  
  3632.        {  
  3633.     retval = ABORT;  
  3634.     break;  
  3635.        }  
  3636.        if (retval == 'r' || retval == 'R')  
  3637.        {  
  3638.     retval = RETRY;  
  3639.     break;  
  3640.        }  
  3641.        if (retval == 'i' || retval == 'I')  
  3642.        {  
  3643.            retval = IGNORE;  
  3644.            break;  
  3645.        }  
  3646.    }  

  3647.    return(retval);  
  3648. }  

  3649. /*pragma warn -par reduces warnings which occur  
  3650. due to the non use of the parameters errval,  
  3651. bp and si to the handler.*/  
  3652. #pragma warn -par  

  3653. int handler(int errval,int ax,int bp,int si)  
  3654. {  
  3655.    static char msg[80];  
  3656.    unsigned di;  
  3657.    int drive;  
  3658.    int errorno;  
  3659.    di= _DI;  
  3660. /*if this is not a disk error then it was  
  3661. another device having trouble*/  

  3662.    if (ax < 0)  
  3663.    {  
  3664.       /* report the error */  
  3665.       error_win("Device error");  
  3666.       /* and return to the program directly requesting abort */  
  3667.       hardretn(ABORT);  
  3668.    }  
  3669. /* otherwise it was a disk error */  
  3670.    drive = ax & 0x00FF;  
  3671.    errorno = di & 0x00FF;  
  3672. /* report which error it was */  
  3673.    sprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ",  
  3674.     err_msg[errorno], 'A' + drive);  
  3675. /*  
  3676. return to the program via dos interrupt 0x23 with abort, retry,  
  3677. or ignore as input by the user.  
  3678. */  
  3679.    hardresume(error_win(msg));  
  3680.    return ABORT;  
  3681. }  
  3682. #pragma warn +par  

  3683. int main(void)  
  3684. {  
  3685. /*  
  3686. install our handler on the hardware problem interrupt  
  3687. */  
  3688.    harderr(handler);  
  3689.    clrscr();  
  3690.    printf("Make sure there is no disk in drive A:\n");  
  3691.    printf("Press any key ....\n");  
  3692.    getch();  
  3693.    printf("Trying to access drive A:\n");  
  3694.    printf("fopen returned %p\n",fopen("A:temp.dat", "w"));  
  3695.    return 0;  
  3696. }  
  3697.    
  3698.    

  3699. 函数名: hardresume  
  3700. 功  能: 硬件错误处理函数  
  3701. 用  法: void hardresume(int rescode);  
  3702. 程序例:  
  3703.    

  3704. /* This program will trap disk errors and prompt the user for action. */  
  3705. /* Try running it with no disk in drive A: to invoke its functions    */  

  3706. #include <stdio.h>  
  3707. #include <conio.h>  
  3708. #include <dos.h>  

  3709. #define IGNORE  0  
  3710. #define RETRY   1  
  3711. #define ABORT   2  

  3712. int buf[500];  

  3713. /* define the error messages for trapping disk problems */  
  3714. static char *err_msg[] = {  
  3715.     "write protect",  
  3716.     "unknown unit",  
  3717.     "drive not ready",  
  3718.     "unknown command",  
  3719.     "data error (CRC)",  
  3720.     "bad request",  
  3721.     "seek error",  
  3722.     "unknown media type",  
  3723.     "sector not found",  
  3724.     "printer out of paper",  
  3725.     "write fault",  
  3726.     "read fault",  
  3727.     "general failure",  
  3728.     "reserved",  
  3729.     "reserved",  
  3730.     "invalid disk change"  
  3731. };  

  3732. error_win(char *msg)  
  3733. {  
  3734.    int retval;  

  3735.    cputs(msg);  

  3736. /* prompt for user to press a key to abort, retry, ignore */  
  3737.    while(1)  
  3738.    {  
  3739.        retval= getch();  
  3740.        if (retval == 'a' || retval == 'A')  
  3741.        {  
  3742.            retval = ABORT;  
  3743.            break;  
  3744.        }  
  3745.        if (retval == 'r' || retval == 'R')  
  3746.        {  
  3747.            retval = RETRY;  
  3748.            break;  
  3749.        }  
  3750.        if (retval == 'i' || retval == 'I')  
  3751.        {  
  3752.            retval = IGNORE;  
  3753.            break;  
  3754.        }  
  3755.    }  

  3756.    return(retval);  
  3757. }  

  3758. /* pragma warn -par reduces warnings which occur due to the non use */  
  3759. /* of the parameters errval, bp and si to the handler.              */  
  3760. #pragma warn -par  

  3761. int handler(int errval,int ax,int bp,int si)  
  3762. {  
  3763.    static char msg[80];  
  3764.    unsigned di;  
  3765.    int drive;  
  3766.    int errorno;  

  3767.    di= _DI;  
  3768. /* if this is not a disk error then it was another device having trouble */  

  3769.    if (ax < 0)  
  3770.    {  
  3771.       /* report the error */  
  3772.       error_win("Device error");  
  3773.       /* and return to the program directly  
  3774.       requesting abort */  
  3775.       hardretn(ABORT);  
  3776.    }  
  3777. /* otherwise it was a disk error */  
  3778.    drive = ax & 0x00FF;  
  3779.    errorno = di & 0x00FF;  
  3780. /* report which error it was */  
  3781.    sprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ",  
  3782.            err_msg[errorno], 'A' + drive);  
  3783. /* return to the program via dos interrupt 0x23 with abort, retry */  
  3784. /* or ignore as input by the user.  */  
  3785.    hardresume(error_win(msg));  
  3786.    return ABORT;  
  3787. }  
  3788. #pragma warn +par  

  3789. int main(void)  
  3790. {  
  3791. /* install our handler on the hardware problem interrupt */  
  3792.    harderr(handler);  
  3793.    clrscr();  
  3794.    printf("Make sure there is no disk in drive A:\n");  
  3795.    printf("Press any key ....\n");  
  3796.    getch();  
  3797.    printf("Trying to access drive A:\n");  
  3798.    printf("fopen returned %p\n",fopen("A:temp.dat", "w"));  
  3799.    return 0;  
  3800. }  
  3801.    
  3802.    

  3803. 函数名: highvideo  
  3804. 功  能: 选择高亮度文本字符  
  3805. 用  法: void highvideo(void);  
  3806. 程序例:  

  3807. #include <conio.h>  

  3808. int main(void)  
  3809. {  
  3810.    clrscr();  

  3811.    lowvideo();  
  3812.    cprintf("Low Intensity text\r\n");  
  3813.    highvideo();  
  3814.    gotoxy(1,2);  
  3815.    cprintf("High Intensity Text\r\n");  

  3816.    return 0;  
  3817. }  
  3818.    
  3819.    

  3820. 函数名: hypot  
  3821. 功  能: 计算直角三角形的斜边长  
  3822. 用  法: double hypot(double x, double y);  
  3823. 程序例:  

  3824. #include <stdio.h>  
  3825. #include <math.h>  

  3826. int main(void)  
  3827. {  
  3828.    double result;  
  3829.    double x = 3.0;  
  3830.    double y = 4.0;  

  3831.    result = hypot(x, y);  
  3832.    printf("The hypotenuse is: %lf\n", result);  

  3833.    return 0;  
  3834. }  
  3835. 函数名: kbhit
  3836. 功  能: 检查当前按下的键
  3837. 用  法: int kbhit(void);
  3838. 程序例:  
  3839. #include <conio.h>  

  3840. int main(void)
  3841. {
  3842.    cprintf("Press any key to continue:");
  3843.    while (!kbhit()) /* do nothing */ ;
  3844.    cprintf("\r\nA key was pressed...\r\n");
  3845.    return 0;
  3846. }
  3847.   
  3848.   
  3849.    

  3850. 函数名: keep
  3851. 功  能: 退出并继续驻留
  3852. 用  法: void keep(int status, int size);
  3853. 程序例:  

  3854. /***NOTE:
  3855.    This is an interrupt service routine.  You
  3856.    can NOT compile this program with Test
  3857.    Stack Overflow turned on and get an
  3858.    executable file which will operate
  3859.    correctly.  Due to the nature of this
  3860.    function the formula used to compute
  3861.    the number of paragraphs may not
  3862.    necessarily work in all cases.  Use with
  3863.    care!  Terminate Stay Resident (TSR)
  3864.    programs are complex and no other support
  3865.    for them is provided.  Refer to the
  3866.    MS-DOS technical documentation
  3867.    for more information.  */
  3868. #include <dos.h>
  3869. /* The clock tick interrupt */
  3870. #define INTR 0x1C
  3871. /* Screen attribute (blue on grey) */
  3872. #define ATTR 0x7900  

  3873. /* reduce heaplength and stacklength
  3874. to make a smaller program in memory */
  3875. extern unsigned _heaplen = 1024;
  3876. extern unsigned _stklen  = 512;  

  3877. void interrupt ( *oldhandler)(void);  

  3878. void interrupt handler(void)
  3879. {
  3880.    unsigned int (far *screen)[80];
  3881.    static int count;  

  3882. /* For a color screen the video memory
  3883.    is at B800:0000.  For a monochrome
  3884.    system use B000:000 */
  3885.    screen = MK_FP(0xB800,0);  

  3886. /* increase the counter and keep it
  3887.    within 0 to 9 */
  3888.    count++;
  3889.    count %= 10;  

  3890. /* put the number on the screen */
  3891.    screen[0][79] = count + '0' + ATTR;  

  3892. /* call the old interrupt handler */
  3893.    oldhandler();
  3894. }  

  3895. int main(void)
  3896. {  

  3897. /* get the address of the current clock
  3898.    tick interrupt */
  3899. oldhandler = getvect(INTR);  

  3900. /* install the new interrupt handler */
  3901. setvect(INTR, handler);  

  3902. /* _psp is the starting address of the
  3903.    program in memory.  The top of the stack
  3904.    is the end of the program.  Using _SS and
  3905.    _SP together we can get the end of the
  3906.    stack.  You may want to allow a bit of
  3907.    saftey space to insure that enough room
  3908.    is being allocated ie:
  3909.    (_SS + ((_SP + safety space)/16) - _psp)
  3910. */
  3911. keep(0, (_SS + (_SP/16) - _psp));
  3912. return 0;
  3913. } main()主函数  
  3914.     每一C 程序都必须有一main()函数, 可以根据自己的爱好把它放在程序的某  
  3915. 个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放  
  3916. 在哪个地方, 以下几点说明都是适合的。  
  3917.     1. main() 参数  
  3918.     在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。  
  3919.      * argc:  整数, 为传给main()的命令行参数个数。  
  3920.      * argv:  字符串数组。  
  3921.               在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0  
  3922.               以下的版本, argv[0]为空串("") 。  
  3923.               argv[1] 为在DOS命令行中执行程序名后的第一个字符串;  
  3924.               argv[2] 为执行程序名后的第二个字符串;  
  3925.               ...  
  3926.               argv[argc]为NULL。  
  3927.      *env:  安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符  
  3928. 串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:\DOS, C:  
  3929. \TURBOC(对于PATH) 或YES(对于87)。  
  3930.     Turbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中  
  3931. 说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序  
  3932. 的局部变量。  
  3933.     请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下  
  3934. 的例子:  
  3935.      main()  
  3936.      main(int argc)  
  3937.      main(int argc, char *argv[])  
  3938.      main(int argc, char *argv[], char *env[])  
  3939.     其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不  
  3940. 用argv[]的情况。  
  3941.     以下提供一样例程序EXAMPLE.EXE,  演示如何在main()函数中使用三个参数:  
  3942.      /*program name EXAMPLE.EXE*/  
  3943.      #include <stdio.h>  
  3944.      #include <stdlib.h>  
  3945.      main(int argc, char *argv[], char *env[])  
  3946.      {  
  3947.           int i;  
  3948.           printf("These are the %d  command- line  arguments passed  to  
  3949.                   main:\n\n", argc);  
  3950.           for(i=0; i<=argc; i++)  
  3951.             printf("argv[%d]:%s\n", i, argv[i]);  
  3952.           printf("\nThe environment string(s)on this system are:\n\n");  
  3953.           for(i=0; env[i]!=NULL; i++)  
  3954.                printf(" env[%d]:%s\n", i, env[i]);  
  3955.      }  
  3956.     如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE:  
  3957.     C:\example first_argument "argument with blanks"  3  4  "last  but  
  3958. one" stop!  
  3959.     注意: 可以用双引号括起内含空格的参数, 如本例中的:   "  argument  
  3960. with blanks"和"Last but one")。  
  3961.     结果是这样的:  
  3962.      The value of argc is 7  
  3963.      These are the 7 command-linearguments passed to main:  
  3964.      argv[0]:C:\TURBO\EXAMPLE.EXE  
  3965.      argv[1]:first_argument  
  3966.      argv[2]:argument with blanks  
  3967.      argv[3]:3  
  3968.      argv[4]:4  
  3969.      argv[5]:last but one  
  3970.      argv[6]:stop!  
  3971.      argv[7]:(NULL)  
  3972.      The environment string(s) on this system are:  
  3973.      env[0]: COMSPEC=C:\COMMAND.COM  
  3974.      env[1]: PROMPT=$P$G            /*视具体设置而定*/  
  3975.      env[2]: PATH=C:\DOS;C:\TC      /*视具体设置而定*/  
  3976.    
  3977.      应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包  
  3978. 括参数间的空格),  这是由DOS 限制的。  
  3979.    

  3980. 函数名: matherr  
  3981. 功  能: 用户可修改的数学错误处理程序  
  3982. 用  法: int matherr(struct exception *e);  
  3983. 程序例:  

  3984. /* This is a user-defined matherr function that prevents  
  3985.    any error messages from being printed. */  

  3986. #include<math.h>  

  3987. int matherr(struct exception *a)  
  3988. {  
  3989.    return 1;  
  3990. }  
  3991.    
  3992.    
  3993.    

  3994. 函数名: memccpy  
  3995. 功  能: 从源source中拷贝n个字节到目标destin中  
  3996. 用  法: void *memccpy(void *destin, void *source, unsigned char ch,  
  3997.        unsigned n);  
  3998. 程序例:  

  3999. #include <string.h>  
  4000. #include <stdio.h>  

  4001. int main(void)  
  4002. {  
  4003.    char *src = "This is the source string";  
  4004.    char dest[50];  
  4005.    char *ptr;  

  4006.    ptr = memccpy(dest, src, 'c', strlen(src));  

  4007.    if (ptr)  
  4008.    {  
  4009.       *ptr = '\0';  
  4010.       printf("The character was found:  %s\n", dest);  
  4011.    }  
  4012.    else  
  4013.       printf("The character wasn't found\n");  
  4014.    return 0;  
  4015. }  
  4016.    
  4017.    

  4018. 函数名: malloc  
  4019. 功  能: 内存分配函数  
  4020. 用  法: void *malloc(unsigned size);  
  4021. 程序例:  

  4022. #include <stdio.h>  
  4023. #include <string.h>  
  4024. #include <alloc.h>  
  4025. #include <process.h>  

  4026. int main(void)  
  4027. {  
  4028.    char *str;  

  4029.    /* allocate memory for string */  
  4030.    /* This will generate an error when compiling */  
  4031.    /* with C++, use the new operator instead. */  
  4032.    if ((str = malloc(10)) == NULL)  
  4033.    {  
  4034.       printf("Not enough memory to allocate buffer\n");  
  4035.       exit(1);  /* terminate program if out of memory */  
  4036.    }  

  4037.    /* copy "Hello" into string */  
  4038.    strcpy(str, "Hello");  

  4039.    /* display string */  
  4040.    printf("String is %s\n", str);  

  4041.    /* free memory */  
  4042.    free(str);  

  4043.    return 0;  
  4044. }  
  4045.    
  4046.    
  4047.    

  4048. 函数名: memchr  
  4049. 功  能: 在数组的前n个字节中搜索字符  
  4050. 用  法: void *memchr(void *s, char ch, unsigned n);  
  4051. 程序例:  

  4052. #include <string.h>  
  4053. #include <stdio.h>  

  4054. int main(void)  
  4055. {  
  4056.    char str[17];  
  4057.    char *ptr;  

  4058.    strcpy(str, "This is a string");  
  4059.    ptr = memchr(str, 'r', strlen(str));  
  4060.    if (ptr)  
  4061.       printf("The character 'r' is at position: %d\n", ptr - str);  
  4062.    else  
  4063.       printf("The character was not found\n");  
  4064.    return 0;  
  4065. }  
  4066.    

  4067. 函数名: memcpy  
  4068. 功  能: 从源source中拷贝n个字节到目标destin中  
  4069. 用  法: void *memcpy(void *destin, void *source, unsigned n);  
  4070. 程序例:  

  4071. #include <stdio.h>  
  4072. #include <string.h>  
  4073. int main(void)  
  4074. {  
  4075.    char src[] = "******************************";  
  4076.    char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";  
  4077.    char *ptr;  
  4078.    printf("destination before memcpy: %s\n", dest);  
  4079.    ptr = memcpy(dest, src, strlen(src));  
  4080.    if (ptr)  
  4081.       printf("destination after memcpy:  %s\n", dest);  
  4082.    else  
  4083.       printf("memcpy failed\n");  
  4084.    return 0;  
  4085. }  
  4086.    
  4087.    

  4088. 函数名: memicmp  
  4089. 功  能: 比较两个串s1和s2的前n个字节, 忽略大小写  
  4090. 用  法: int memicmp(void *s1, void *s2, unsigned n);  
  4091. 程序例:  

  4092. #include <stdio.h>  
  4093. #include <string.h>  

  4094. int main(void)  
  4095. {  
  4096.    char *buf1 = "ABCDE123";  
  4097.    char *buf2 = "abcde456";  
  4098.    int stat;  
  4099.    stat = memicmp(buf1, buf2, 5);  
  4100.    printf("The strings to position 5 are ");  
  4101.    if (stat)  
  4102.       printf("not ");  
  4103.    printf("the same\n");  
  4104.    return 0;  
  4105. }  
  4106.    
  4107.    

  4108. 函数名: memmove  
  4109. 功  能: 移动一块字节  
  4110. 用  法: void *memmove(void *destin, void *source, unsigned n);  
  4111. 程序例:  

  4112. #include <string.h>  
  4113. #include <stdio.h>  

  4114. int main(void)  
  4115. {  
  4116.   char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";  
  4117.   char *src = "******************************";  
  4118.   printf("destination prior to memmove: %s\n", dest);  
  4119.   memmove(dest, src, 26);  
  4120.   printf("destination after memmove:    %s\n", dest);  
  4121.   return 0;  
  4122. }  
  4123.    
  4124.    
  4125.    

  4126. 函数名: memset  
  4127. 功  能: 设置s中的所有字节为ch, s数组的大小由n给定  
  4128. 用  法: void *memset(void *s, char ch, unsigned n);  
  4129. 程序例:  

  4130. #include <string.h>  
  4131. #include <stdio.h>  
  4132. #include <mem.h>  

  4133. int main(void)  
  4134. {  
  4135.    char buffer[] = "Hello world\n";  

  4136.    printf("Buffer before memset: %s\n", buffer);  
  4137.    memset(buffer, '*', strlen(buffer) - 1);  
  4138.    printf("Buffer after memset:  %s\n", buffer);  
  4139.    return 0;  
  4140. }  
  4141.    
  4142.    

  4143. 函数名: mkdir  
  4144. 功  能: 建立一个目录  
  4145. 用  法: int mkdir(char *pathname);  
  4146. 程序例:  

  4147. #include <stdio.h>  
  4148. #include <conio.h>  
  4149. #include <process.h>  
  4150. #include <dir.h>  

  4151. int main(void)  
  4152. {  
  4153.   int status;  

  4154.    clrscr();  
  4155.    status = mkdir("asdfjklm");  
  4156.    (!status) ? (printf("Directory created\n")) :  
  4157.                (printf("Unable to create directory\n"));  

  4158.    getch();  
  4159.    system("dir");  
  4160.    getch();  

  4161.    status = rmdir("asdfjklm");  
  4162.    (!status) ? (printf("Directory deleted\n")) :  
  4163.   (perror("Unable to delete directory"));  

  4164.    return 0;  
  4165. }  
  4166.    
  4167.    
  4168.    

  4169. 函数名: mktemp  
  4170. 功  能: 建立唯一的文件名  
  4171. 用  法: char *mktemp(char *template);  
  4172. 程序例:  

  4173. #include <dir.h>  
  4174. #include <stdio.h>  

  4175. int main(void)  
  4176. {  
  4177.    /* fname defines the template for the  
  4178.      temporary file.  */  

  4179.    char *fname = "TXXXXXX", *ptr;  

  4180.    ptr = mktemp(fname);  
  4181.    printf("%s\n",ptr);  
  4182.    return 0;  
  4183. }  
  4184.    
  4185.    

  4186. 函数名: MK_FP  
  4187. 功  能: 设置一个远指针  
  4188. 用  法: void far *MK_FP(unsigned seg, unsigned off);  
  4189. 程序例:  

  4190. #include <dos.h>  
  4191. #include <graphics.h>  

  4192. int main(void)  
  4193. {  
  4194.    int gd, gm, i;  
  4195.    unsigned int far *screen;  

  4196.    detectgraph(&gd, &gm);  
  4197.    if (gd == HERCMONO)  
  4198.        screen = MK_FP(0xB000, 0);  
  4199.    else  
  4200.        screen = MK_FP(0xB800, 0);  
  4201.    for (i=0; i<26; i++)  
  4202.       screen[i] = 0x0700 + ('a' + i);  
  4203.    return 0;  
  4204. }  
  4205.    
  4206.    

  4207. 函数名: modf  
  4208. 功  能: 把数分为指数和尾数  
  4209. 用  法: double modf(double value, double *iptr);  
  4210. 程序例:  

  4211. #include <math.h>  
  4212. #include <stdio.h>  

  4213. int main(void)  
  4214. {  
  4215.    double fraction, integer;  
  4216.    double number = 100000.567;  

  4217.    fraction = modf(number, &integer);  
  4218.    printf("The whole and fractional parts of %lf are %lf and %lf\n",  
  4219.           number, integer, fraction);  
  4220.    return 0;  
  4221. }  
  4222.    
  4223.    

  4224. 函数名: movedata  
  4225. 功  能: 拷贝字节  
  4226. 用  法: void movedata(int segsrc, int offsrc, int segdest,  
  4227.   int offdest, unsigned numbytes);  
  4228. 程序例:  

  4229. #include <mem.h>  

  4230. #define MONO_BASE 0xB000  

  4231. /* saves the contents of the monochrome screen in buffer */  
  4232. void save_mono_screen(char near *buffer)  
  4233. {  
  4234.    movedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2);  
  4235. }  

  4236. int main(void)  
  4237. {  
  4238.    char buf[80*25*2];  
  4239.    save_mono_screen(buf);  
  4240. }  
  4241.    
  4242.    

  4243. 函数名: moverel  
  4244. 功  能: 将当前位置(CP)移动一相对距离  
  4245. 用  法: void far moverel(int dx, int dy);  
  4246. 程序例:  

  4247. #include <graphics.h>  
  4248. #include <stdlib.h>  
  4249. #include <stdio.h>  
  4250. #include <conio.h>  

  4251. int main(void)  
  4252. {  
  4253.    /* request auto detection */  
  4254.    int gdriver = DETECT, gmode, errorcode;  
  4255.    char msg[80];  

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

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

  4267.    /* move the C.P. to location (20, 30) */  
  4268.    moveto(20, 30);  

  4269.    /* plot a pixel at the C.P. */  
  4270.    putpixel(getx(), gety(), getmaxcolor());  

  4271.    /* create and output a message at (20, 30) */  
  4272.    sprintf(msg, " (%d, %d)", getx(), gety());  
  4273.    outtextxy(20, 30, msg);  

  4274.    /* move to a point a relative distance */  
  4275.    /* away from the current value of C.P. */  
  4276.    moverel(100, 100);  

  4277.    /* plot a pixel at the C.P. */  
  4278.    putpixel(getx(), gety(), getmaxcolor());  

  4279.    /* create and output a message at C.P. */  
  4280.    sprintf(msg, " (%d, %d)", getx(), gety());  
  4281.    outtext(msg);  

  4282.    /* clean up */  
  4283.    getch();  
  4284.    closegraph();  
  4285.    return 0;  
  4286. }  
  4287.    
  4288.    

  4289. 函数名: movetext  
  4290. 功  能: 将屏幕文本从一个矩形区域拷贝到另一个矩形区域  
  4291. 用  法: int movetext(int left, int top, int right, int bottom,  
  4292.   int newleft, int newtop);  
  4293. 程序例:  
  4294. #include <conio.h>  
  4295. #include <string.h>  

  4296. int main(void)  
  4297. {  
  4298.    char *str = "This is a test string";  

  4299.    clrscr();  
  4300.    cputs(str);  
  4301.    getch();  

  4302.    movetext(1, 1, strlen(str), 2, 10, 10);  
  4303.    getch();  

  4304.    return 0;  
  4305. }  
  4306.    
  4307.    

  4308. 函数名: moveto  
  4309. 功  能: 将CP移到(x, y)  
  4310. 用  法: void far moveto(int x, int y);  
  4311. 程序例:  

  4312. #include <graphics.h>  
  4313. #include <stdlib.h>  
  4314. #include <stdio.h>  
  4315. #include <conio.h>  

  4316. int main(void)  
  4317. {  
  4318.    /* request auto detection */  
  4319.    int gdriver = DETECT, gmode, errorcode;  
  4320.    char msg[80];  

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

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

  4332.    /* move the C.P. to location (20, 30) */  
  4333.    moveto(20, 30);  

  4334.    /* plot a pixel at the C.P. */  
  4335.    putpixel(getx(), gety(), getmaxcolor());  

  4336.    /* create and output a message at (20, 30) */  
  4337.    sprintf(msg, " (%d, %d)", getx(), gety());  
  4338.    outtextxy(20, 30, msg);  

  4339.    /* move to (100, 100) */  
  4340.    moveto(100, 100);  

  4341.    /* plot a pixel at the C.P. */  
  4342.    putpixel(getx(), gety(), getmaxcolor());  

  4343.    /* create and output a message at C.P. */  
  4344.    sprintf(msg, " (%d, %d)", getx(), gety());  
  4345.    outtext(msg);  

  4346.    /* clean up */  
  4347.    getch();  
  4348.    closegraph();  
  4349.    return 0;  
  4350. }  
  4351.    
  4352.    

  4353. 函数名: movemem  
  4354. 功  能: 移动一块字节  
  4355. 用  法: void movemem(void *source, void *destin, unsigned len);  
  4356. 程序例:  

  4357. #include <mem.h>  
  4358. #include <alloc.h>  
  4359. #include <stdio.h>  
  4360. #include <string.h>  

  4361. int main(void)  
  4362. {  
  4363.    char *source = "Borland International";  
  4364.    char *destination;  
  4365.    int length;  

  4366.    length = strlen(source);  
  4367.    destination = malloc(length + 1);  
  4368.    movmem(source,destination,length);  
  4369.    printf("%s\n",destination);  

  4370.    return 0;  
  4371. }  
  4372.    
  4373.    

  4374. 函数名: normvideo  
  4375. 功  能: 选择正常亮度字符  
  4376. 用  法: void normvideo(void);  
  4377. 程序例:  

  4378. #include <conio.h>  

  4379. int main(void)  
  4380. {  
  4381.    normvideo();  
  4382.    cprintf("NORMAL Intensity Text\r\n");  
  4383.    return 0;  
  4384. }  
  4385.    
  4386.    

  4387. 函数名: nosound  
  4388. 功  能: 关闭PC扬声器  
  4389. 用  法: void nosound(void);  
  4390. 程序例:  

  4391. /* Emits a 7-Hz tone for 10 seconds.  

  4392.      True story: 7 Hz is the resonant frequency of a chicken's skull cavity.  
  4393.      This was determined empirically in Australia, where a new factory  
  4394.      generating 7-Hz tones was located too close to a chicken ranch:  
  4395.      When the factory started up, all the chickens died.  

  4396.      Your PC may not be able to emit a 7-Hz tone.  
  4397. */  

  4398. int main(void)  
  4399. {  
  4400.    sound(7);  
  4401.    delay(10000);  
  4402.    nosound();  
  4403. }
  4404. 函数名: open  
  4405. 功  能: 打开一个文件用于读或写  
  4406. 用  法: int open(char *pathname, int access[, int permiss]);  
  4407. 程序例:  
  4408. #include <string.h>  
  4409. #include <stdio.h>  
  4410. #include <fcntl.h>  
  4411. #include <io.h>  

  4412. int main(void)  
  4413. {  
  4414.    int handle;  
  4415.    char msg[] = "Hello world";  

  4416.    if ((handle = open("TEST.$", O_CREAT | O_TEXT)) == -1)  
  4417.    {  
  4418.       perror("Error:");  
  4419.       return 1;  
  4420.    }  
  4421.    write(handle, msg, strlen(msg));  
  4422.    close(handle);  
  4423.    return 0;  
  4424. }  
  4425.    
  4426.    

  4427. 函数名: outport  
  4428. 功  能: 输出整数到硬件端口中  
  4429. 用  法: void outport(int port, int value);  
  4430. 程序例:  

  4431. #include <stdio.h>  
  4432. #include <dos.h>  

  4433. int main(void)  
  4434. {  
  4435.    int value = 64;  
  4436.    int port = 0;  

  4437.    outportb(port, value);  
  4438.    printf("Value %d sent to port number %d\n", value, port);  
  4439.    return 0;  
  4440. }  
  4441.    
  4442.    

  4443. 函数名: outportb  
  4444. 功  能: 输出字节到硬件端口中  
  4445. 用  法: void outportb(int port, char byte);  
  4446. 程序例:  

  4447. #include <stdio.h>  
  4448. #include <dos.h>  

  4449. int main(void)  
  4450. {  
  4451.    int value = 64;  
  4452.    int port = 0;  

  4453.    outportb(port, value);  
  4454.    printf("Value %d sent to port number %d\n", value, port);  
  4455.    return 0;  
  4456. }  
  4457.    
  4458.    

  4459. 函数名: outtext  
  4460. 功  能: 在视区显示一个字符串  
  4461. 用  法: void far outtext(char far *textstring);  
  4462. 程序例:  

  4463. #include <graphics.h>  
  4464. #include <stdlib.h>  
  4465. #include <stdio.h>  
  4466. #include <conio.h>  

  4467. int main(void)  
  4468. {  
  4469.    /* request auto detection */  
  4470.    int gdriver = DETECT, gmode, errorcode;  
  4471.    int midx, midy;  

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

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

  4483.    midx = getmaxx() / 2;  
  4484.    midy = getmaxy() / 2;  

  4485.    /* move the C.P. to the center of the screen */  
  4486.    moveto(midx, midy);  

  4487.    /* output text starting at the C.P. */  
  4488.    outtext("This ");  
  4489.    outtext("is ");  
  4490.    outtext("a ");  
  4491.    outtext("test.");  

  4492.    /* clean up */  
  4493.    getch();  
  4494.    closegraph();  
  4495.    return 0;  
  4496. }  
  4497.    
  4498.    

  4499. 函数名: outtextxy  
  4500. 功  能: 在指定位置显示一字符串  
  4501. 用  法: void far outtextxy(int x, int y, char *textstring);  
  4502. 程序例:  

  4503. #include <graphics.h>  
  4504. #include <stdlib.h>  
  4505. #include <stdio.h>  
  4506. #include <conio.h>  

  4507. int main(void)  
  4508. {  
  4509.    /* request auto detection */  
  4510.    int gdriver = DETECT, gmode, errorcode;  
  4511.    int midx, midy;  

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

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

  4523.    midx = getmaxx() / 2;  
  4524.    midy = getmaxy() / 2;  

  4525.    /* output text at the center of the screen*/  
  4526.    /* Note: the C.P. doesn't get changed.*/  
  4527.    outtextxy(midx, midy, "This is a test.");  

  4528.    /* clean up */  
  4529.    getch();  
  4530.    closegraph();  
  4531.    return 0;  
  4532. }  
  4533. 函数名: parsfnm  
  4534. 功  能: 分析文件名  
  4535. 用  法: char *parsfnm (char *cmdline, struct fcb *fcbptr, int option);  
  4536. 程序例:  

  4537. #include <process.h>  
  4538. #include <string.h>  
  4539. #include <stdio.h>  
  4540. #include <dos.h>  

  4541. int main(void)  
  4542. {  
  4543.    char line[80];  
  4544.    struct fcb blk;  

  4545.    /* get file name */  
  4546.    printf("Enter drive and file name (no path - ie. a:file.dat)\n");  
  4547.    gets(line);  

  4548.    /* put file name in fcb */  
  4549.    if (parsfnm(line, &blk, 1) == NULL)  
  4550.       printf("Error in parsfm call\n");  
  4551.    else  
  4552.       printf("Drive #%d  Name: %11s\n", blk.fcb_drive, blk.fcb_name);  

  4553.    return 0;  
  4554. }  
  4555.    
  4556.    

  4557. 函数名: peek  
  4558. 功  能: 检查存储单元  
  4559. 用  法: int peek(int segment, unsigned offset);  
  4560. 程序例:  

  4561. #include <stdio.h>  
  4562. #include <conio.h>  
  4563. #include <dos.h>  

  4564. int main(void)  
  4565. {  
  4566.    int value = 0;  

  4567.    printf("The current status of your keyboard is:\n");  
  4568.    value = peek(0x0040, 0x0017);  
  4569.    if (value & 1)  
  4570.       printf("Right shift on\n");  
  4571.    else  
  4572.       printf("Right shift off\n");  

  4573.    if (value & 2)  
  4574.       printf("Left shift on\n");  
  4575.    else  
  4576.       printf("Left shift off\n");  

  4577.    if (value & 4)  
  4578.       printf("Control key on\n");  
  4579.    else  
  4580.       printf("Control key off\n");  

  4581.    if (value & 8)  
  4582.       printf("Alt key on\n");  
  4583.    else  
  4584.       printf("Alt key off\n");  

  4585.    if (value & 16)  
  4586.       printf("Scroll lock on\n");  
  4587.    else  
  4588.       printf("Scroll lock off\n");  

  4589.    if (value & 32)  
  4590.       printf("Num lock on\n");  
  4591.    else  
  4592.       printf("Num lock off\n");  

  4593.    if (value & 64)  
  4594.       printf("Caps lock on\n");  
  4595.    else  
  4596.       printf("Caps lock off\n");  

  4597.    return 0;  
  4598. }  
  4599.    
  4600.    

  4601. 函数名: peekb  
  4602. 功  能: 检查存储单元  
  4603. 用  法: char peekb (int segment, unsigned offset);  
  4604. 程序例:  

  4605. #include <stdio.h>  
  4606. #include <conio.h>  
  4607. #include <dos.h>  

  4608. int main(void)  
  4609. {  
  4610.    int value = 0;  

  4611.    printf("The current status of your keyboard is:\n");  
  4612.    value = peekb(0x0040, 0x0017);  
  4613.    if (value & 1)  
  4614.       printf("Right shift on\n");  
  4615.    else  
  4616.       printf("Right shift off\n");  

  4617.    if (value & 2)  
  4618.       printf("Left shift on\n");  
  4619.    else  
  4620.       printf("Left shift off\n");  

  4621.    if (value & 4)  
  4622.       printf("Control key on\n");  
  4623.    else  
  4624.       printf("Control key off\n");  

  4625.    if (value & 8)  
  4626.       printf("Alt key on\n");  
  4627.    else  
  4628.       printf("Alt key off\n");  

  4629.    if (value & 16)  
  4630.       printf("Scroll lock on\n");  
  4631.    else  
  4632.       printf("Scroll lock off\n");  

  4633.    if (value & 32)  
  4634.       printf("Num lock on\n");  
  4635.    else  
  4636.       printf("Num lock off\n");  

  4637.    if (value & 64)  
  4638.       printf("Caps lock on\n");  
  4639.    else  
  4640.       printf("Caps lock off\n");  

  4641.    return 0;  
  4642. }  
  4643.    
  4644.    

  4645. 函数名: perror  
  4646. 功  能: 系统错误信息  
  4647. 用  法: void perror(char *string);  
  4648. 程序例:  

  4649. #include <stdio.h>  

  4650. int main(void)  
  4651. {  
  4652.    FILE *fp;  

  4653.    fp = fopen("perror.dat", "r");  
  4654.    if (!fp)  
  4655.       perror("Unable to open file for reading");  
  4656.    return 0;  
  4657. }  
  4658.    
  4659.    

  4660. 函数名: pieslice  
  4661. 功  能: 绘制并填充一个扇形  
  4662. 用  法: void far pieslice(int x, int stanle, int endangle, int radius);  
  4663. 程序例:  

  4664. #include <graphics.h>  
  4665. #include <stdlib.h>  
  4666. #include <stdio.h>  
  4667. #include <conio.h>  

  4668. int main(void)  
  4669. {  
  4670.    /* request auto detection */  
  4671.    int gdriver = DETECT, gmode, errorcode;  
  4672.    int midx, midy;  
  4673.    int stangle = 45, endangle = 135, radius = 100;  

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

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

  4685.    midx = getmaxx() / 2;  
  4686.    midy = getmaxy() / 2;  

  4687.    /* set fill style and draw a pie slice */  
  4688.    setfillstyle(EMPTY_FILL, getmaxcolor());  
  4689.    pieslice(midx, midy, stangle, endangle, radius);  

  4690.    /* clean up */  
  4691.    getch();  
  4692.    closegraph();  
  4693.    return 0;  
  4694. }  
  4695.    
  4696.    

  4697. 函数名: poke  
  4698. 功  能: 存值到一个给定存储单元  
  4699. 用  法: void poke(int segment, int offset, int value);  
  4700. 程序例:  

  4701. #include <dos.h>  
  4702. #include <conio.h>  

  4703. int main(void)  
  4704. {  
  4705.    clrscr();  
  4706.    cprintf("Make sure the scroll lock key is off and press any key\r\n");  
  4707.    getch();  
  4708.    poke(0x0000,0x0417,16);  
  4709.    cprintf("The scroll lock is now on\r\n");  
  4710.    return 0;  
  4711. }  
  4712.    
  4713.    

  4714. 函数名: pokeb  
  4715. 功  能: 存值到一个给定存储单元  
  4716. 用  法: void pokeb(int segment, int offset, char value);  
  4717. 程序例:  

  4718. #include <dos.h>  
  4719. #include <conio.h>  

  4720. int main(void)  
  4721. {  
  4722.    clrscr();  
  4723.    cprintf("Make sure the scroll lock key is off and press any key\r\n");  
  4724.    getch();  
  4725.    pokeb(0x0000,0x0417,16);  
  4726.    cprintf("The scroll lock is now on\r\n");  
  4727.    return 0;  
  4728. }  
  4729.    
  4730.    

  4731. 函数名: poly  
  4732. 功  能: 根据参数产生一个多项式  
  4733. 用  法: double poly(double x, int n, double c[]);  
  4734. 程序例:  

  4735. #include <stdio.h>  
  4736. #include <math.h>  

  4737. /* polynomial:  x**3 - 2x**2 + 5x - 1 */  

  4738. int main(void)  
  4739. {  
  4740.    double array[] = { -1.0, 5.0, -2.0, 1.0 };  
  4741.    double result;  

  4742.    result = poly(2.0, 3, array);  
  4743.    printf("The polynomial: x**3 - 2.0x**2 + 5x - 1 at 2.0 is %lf\n",  
  4744.            result);  
  4745.    return 0;  
  4746. }  
  4747.    
  4748.    

  4749. 函数名: pow  
  4750. 功  能: 指数函数(x的y次方)  
  4751. 用  法: double pow(double x, double y);  
  4752. 程序例:  

  4753. #include <math.h>  
  4754. #include <stdio.h>  

  4755. int main(void)  
  4756. {  
  4757.    double x = 2.0, y = 3.0;  

  4758.    printf("%lf raised to %lf is %lf\n", x, y, pow(x, y));  
  4759.    return 0;  
  4760. }  
  4761.    

  4762. 函数名: pow10  
  4763. 功  能: 指数函数(10的p次方)  
  4764. 用  法: double pow10(int p);  
  4765. 程序例:  

  4766. #include <math.h>  
  4767. #include <stdio.h>  

  4768. int main(void)  
  4769. {  
  4770.    double p = 3.0;  

  4771.    printf("Ten raised to %lf is %lf\n", p, pow10(p));  
  4772.    return 0;  
  4773. }  
  4774.    
  4775.    

  4776. 函数名: printf  
  4777. 功  能: 产生格式化输出的函数  
  4778. 用  法: int printf(char *format...);  
  4779. 程序例:  

  4780. #include <stdio.h>  
  4781. #include <string.h>  

  4782. #define I 555  
  4783. #define R 5.5  

  4784. int main(void)  
  4785. {  
  4786.    int i,j,k,l;  
  4787.    char buf[7];  
  4788.    char *prefix = buf;  
  4789.    char tp[20];  
  4790.    printf("prefix  6d      6o      8x        10.2e        "  
  4791.           "10.2f\n");  
  4792.    strcpy(prefix,"%");  
  4793.    for (i = 0; i < 2; i++)  
  4794.    {  
  4795.       for (j = 0; j < 2; j++)  
  4796.          for (k = 0; k < 2; k++)  
  4797.      for (l = 0; l < 2; l++)  
  4798.             {  
  4799.                if (i==0)  strcat(prefix,"-");  
  4800.                if (j==0)  strcat(prefix,"+");  
  4801.                if (k==0)  strcat(prefix,"#");  
  4802.                if (l==0)  strcat(prefix,"0");  
  4803.                printf("%5s |",prefix);  
  4804.                strcpy(tp,prefix);  
  4805.                strcat(tp,"6d |");  
  4806.                printf(tp,I);  
  4807.                strcpy(tp,"");  
  4808.                strcpy(tp,prefix);  
  4809.                strcat(tp,"6o |");  
  4810.                printf(tp,I);  
  4811.                strcpy(tp,"");  
  4812.                strcpy(tp,prefix);  
  4813.                strcat(tp,"8x |");  
  4814.                printf(tp,I);  
  4815.                strcpy(tp,"");  
  4816.                strcpy(tp,prefix);  
  4817.         strcat(tp,"10.2e |");  
  4818.         printf(tp,R);  
  4819.         strcpy(tp,prefix);  
  4820.         strcat(tp,"10.2f |");  
  4821.         printf(tp,R);  
  4822.         printf("  \n");  
  4823.         strcpy(prefix,"%");  
  4824.      }  
  4825.        }  
  4826.    return 0;  
  4827. }  
  4828.    
  4829.    

  4830. 函数名: putc  
  4831. 功  能: 输出一字符到指定流中  
  4832. 用  法: int putc(int ch, FILE *stream);  
  4833. 程序例:  

  4834. #include <stdio.h>  

  4835. int main(void)  
  4836. {  
  4837.    char msg[] = "Hello world\n";  
  4838.    int i = 0;  

  4839.    while (msg[i])  
  4840.       putc(msg[i++], stdout);  
  4841.    return 0;  
  4842. }  
  4843.    
  4844.    

  4845. 函数名: putch  
  4846. 功  能: 输出字符到控制台  
  4847. 用  法: int putch(int ch);  
  4848. 程序例:  

  4849. #include <stdio.h>  
  4850. #include <conio.h>  

  4851. int main(void)  
  4852. {  
  4853.    char ch = 0;  

  4854.    printf("Input a string:");  
  4855.    while ((ch != '\r'))  
  4856.    {  
  4857.       ch = getch();  
  4858.       putch(ch);  
  4859.    }  
  4860.    return 0;  
  4861. }  
  4862.    
  4863.    

  4864. 函数名: putchar  
  4865. 功  能: 在stdout上输出字符  
  4866. 用  法: int putchar(int ch);  
  4867. 程序例:  

  4868. #include <stdio.h>  

  4869. /* define some box-drawing characters */  
  4870. #define LEFT_TOP  0xDA  
  4871. #define RIGHT_TOP 0xBF  
  4872. #define HORIZ     0xC4  
  4873. #define VERT      0xB3  
  4874. #define LEFT_BOT  0xC0  
  4875. #define RIGHT_BOT 0xD9  

  4876. int main(void)  
  4877. {  
  4878.    char i, j;  

  4879.    /* draw the top of the box */  
  4880.    putchar(LEFT_TOP);  
  4881.    for (i=0; i<10; i++)  
  4882.       putchar(HORIZ);  
  4883.    putchar(RIGHT_TOP);  
  4884.    putchar('\n');  

  4885.    /* draw the middle */  
  4886.    for (i=0; i<4; i++)  
  4887.    {  
  4888.       putchar(VERT);  
  4889.       for (j=0; j<10; j++)  
  4890.          putchar(' ');  
  4891.       putchar(VERT);  
  4892.       putchar('\n');  
  4893.    }  

  4894.    /* draw the bottom */  
  4895.    putchar(LEFT_BOT);  
  4896.    for (i=0; i<10; i++)  
  4897.       putchar(HORIZ);  
  4898.    putchar(RIGHT_BOT);  
  4899.    putchar('\n');  

  4900.    return 0;  
  4901. }  
  4902.    
  4903.    

  4904. 函数名: putenv  
  4905. 功  能: 把字符串加到当前环境中  
  4906. 用  法: int putenv(char *envvar);  
  4907. 程序例:  

  4908. #include <stdio.h>  
  4909. #include <stdlib.h>  
  4910. #include <alloc.h>  
  4911. #include <string.h>  
  4912. #include <dos.h>  

  4913. int main(void)  
  4914. {  
  4915.    char *path, *ptr;  
  4916.    int i = 0;  

  4917.    /* get the current path environment */  
  4918.    ptr = getenv("PATH");  

  4919.    /* set up new path */  
  4920.    path = malloc(strlen(ptr)+15);  
  4921.    strcpy(path,"PATH=");  
  4922.    strcat(path,ptr);  
  4923.    strcat(path,";c:\\temp");  

  4924.    /* replace the current path and display current environment */  
  4925.    putenv(path);  
  4926.    while (environ[i])  
  4927.        printf("%s\n",environ[i++]);  

  4928.    return 0;  
  4929. }  
  4930.    
  4931.    

  4932. 函数名: putimage  
  4933. 功  能: 在屏幕上输出一个位图  
  4934. 用  法: void far putimage(int x, int y, void far *bitmap, int op);  
  4935. 程序例:  

  4936. #include <graphics.h>  
  4937. #include <stdlib.h>  
  4938. #include <stdio.h>  
  4939. #include <conio.h>  

  4940. #define ARROW_SIZE 10  

  4941. void draw_arrow(int x, int y);  

  4942. int main(void)  
  4943. {  
  4944.    /* request autodetection */  
  4945.    int gdriver = DETECT, gmode, errorcode;  
  4946.    void *arrow;  
  4947.    int x, y, maxx;  
  4948.    unsigned int size;  

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

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

  4960.    maxx = getmaxx();  
  4961.    x = 0;  
  4962.    y = getmaxy() / 2;  

  4963.    /* draw the image to be grabbed */  
  4964.    draw_arrow(x, y);  

  4965.    /* calculate the size of the image */  
  4966.    size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE);  

  4967.    /* allocate memory to hold the image */  
  4968.    arrow = malloc(size);  

  4969.    /* grab the image */  
  4970.    getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow);  

  4971.    /* repeat until a key is pressed */  
  4972.    while (!kbhit())  
  4973.    {  
  4974.       /* erase old image */  
  4975.       putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);  

  4976.       x += ARROW_SIZE;  
  4977.       if (x >= maxx)  
  4978.           x = 0;  

  4979.       /* plot new image */  
  4980.       putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);  
  4981.    }  

  4982.    /* clean up */  
  4983.    free(arrow);  
  4984.    closegraph();  
  4985.    return 0;  
  4986. }  

  4987. void draw_arrow(int x, int y)  
  4988. {  
  4989.    /* draw an arrow on the screen */  
  4990.    moveto(x, y);  
  4991.    linerel(4*ARROW_SIZE, 0);  
  4992.    linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);  
  4993.    linerel(0, 2*ARROW_SIZE);  
  4994.    linerel(2*ARROW_SIZE, -1*ARROW_SIZE);  
  4995. }  
  4996.    
  4997.    

  4998. 函数名: putpixel  
  4999. 功  能: 在指定位置画一像素  
  5000. 用  法: void far putpixel (int x, int y, int pixelcolor);  
  5001. 程序例:  

  5002. #include <graphics.h>  
  5003. #include <stdlib.h>  
  5004. #include <stdio.h>  
  5005. #include <conio.h>  
  5006. #include <dos.h>  

  5007. #define PIXEL_COUNT 1000  
  5008. #define DELAY_TIME  100  /* in milliseconds */  

  5009. int main(void)  
  5010. {  
  5011.    /* request autodetection */  
  5012.    int gdriver = DETECT, gmode, errorcode;  
  5013.    int i, x, y, color, maxx, maxy, maxcolor, seed;  

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

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

  5025.    maxx = getmaxx() + 1;  
  5026.    maxy = getmaxy() + 1;  
  5027.    maxcolor = getmaxcolor() + 1;  

  5028.    while (!kbhit())  
  5029.    {  
  5030.       /* seed the random number generator */  
  5031.       seed = random(32767);  
  5032.       srand(seed);  
  5033.       for (i=0; i<PIXEL_COUNT; i++)  
  5034.       {  
  5035.   x = random(maxx);  
  5036.          y = random(maxy);  
  5037.          color = random(maxcolor);  
  5038.          putpixel(x, y, color);  
  5039.       }  

  5040.       delay(DELAY_TIME);  
  5041.       srand(seed);  
  5042.       for (i=0; i<PIXEL_COUNT; i++)  
  5043.       {  
  5044.   x = random(maxx);  
  5045.   y = random(maxy);  
  5046.   color = random(maxcolor);  
  5047.   if (color == getpixel(x, y))  
  5048.      putpixel(x, y, 0);  
  5049.       }  
  5050.    }  

  5051.    /* clean up */  
  5052.    getch();  
  5053.    closegraph();  
  5054.    return 0;  
  5055. }  
  5056.    
  5057.    

  5058. 函数名: puts  
  5059. 功  能: 送一字符串到流中  
  5060. 用  法: int puts(char *string);  
  5061. 程序例:  

  5062. #include <stdio.h>  
  5063. int main(void)  
  5064. {  
  5065.    char string[] = "This is an example output string\n";  

  5066.    puts(string);  
  5067.    return 0;  
  5068. }  
  5069.    
  5070.    

  5071. 函数名: puttext  
  5072. 功  能: 将文本从存储区拷贝到屏幕  
  5073. 用  法: int puttext(int left, int top, int right, int bottom, void *source);  
  5074. 程序例:  

  5075. #include <conio.h>  
  5076. int main(void)  
  5077. {  
  5078.    char buffer[512];  

  5079.    /* put some text to the console */  
  5080.    clrscr();  
  5081.    gotoxy(20, 12);  
  5082.    cprintf("This is a test.  Press any key to continue ...");  
  5083.    getch();  

  5084.    /* grab screen contents */  
  5085.    gettext(20, 12, 36, 21,buffer);  
  5086.    clrscr();  

  5087.    /* put selected characters back to the screen */  
  5088.    gotoxy(20, 12);  
  5089.    puttext(20, 12, 36, 21, buffer);  
  5090.    getch();  

  5091.    return 0;  
  5092. }  
  5093.    
  5094.    

  5095. 函数名: putw  
  5096. 功  能: 把一字符或字送到流中  
  5097. 用  法: int putw(int w, FILE *stream);  
  5098. 程序例:  

  5099. #include <stdio.h>  
  5100. #include <stdlib.h>  

  5101. #define FNAME "test.$"  

  5102. int main(void)  
  5103. {  
  5104.    FILE *fp;  
  5105.    int word;  

  5106.    /* place the word in a file */  
  5107.    fp = fopen(FNAME, "wb");  
  5108.    if (fp == NULL)  
  5109.    {  
  5110.       printf("Error opening file %s\n", FNAME);  
  5111.       exit(1);  
  5112.    }  

  5113.    word = 94;  
  5114.    putw(word,fp);  
  5115.    if (ferror(fp))  
  5116.        printf("Error writing to file\n");  
  5117.    else  
  5118.        printf("Successful write\n");  
  5119.    fclose(fp);  

  5120.    /* reopen the file */  
  5121.    fp = fopen(FNAME, "rb");  
  5122.    if (fp == NULL)  
  5123.    {  
  5124.       printf("Error opening file %s\n", FNAME);  
  5125.       exit(1);  
  5126.    }  

  5127.    /* extract the word */  
  5128.    word = getw(fp);  
  5129.    if (ferror(fp))  
  5130.        printf("Error reading file\n");  
  5131.    else  
  5132.        printf("Successful read: word = %d\n", word);  

  5133.    /* clean up */  
  5134.    fclose(fp);  
  5135.    unlink(FNAME);  

  5136.    return 0;  
  5137. }函数名: qsort
  5138. 功  能: 使用快速排序例程进行排序
  5139. 用  法: void qsort(void *base, int nelem, int width, int (*fcmp)());
  5140. 程序例:  
  5141. #include <stdio.h>
  5142. #include <stdlib.h>
  5143. #include <string.h>  

  5144. int sort_function( const void *a, const void *b);  

  5145. char list[5][4] = { "cat", "car", "cab", "cap", "can" };
  5146.    

  5147. int main(void)
  5148. {
  5149.    int  x;  

  5150.    qsort((void *)list, 5, sizeof(list[0]), sort_function);
  5151.    for (x = 0; x < 5; x++)
  5152.       printf("%s\n", list[x]);
  5153.    return 0;
  5154. }  

  5155. int sort_function( const void *a, const void *b)
  5156. {
  5157.    return( strcmp(a,b) );
  5158. } 函数名: raise  
  5159. 功  能: 向正在执行的程序发送一个信号  
  5160. 用  法: int raise(int sig);  
  5161. 程序例:  

  5162. #include <signal.h>  

  5163. int main(void)  
  5164. {  
  5165.    int a, b;  

  5166.    a = 10;  
  5167.    b = 0;  
  5168.    if (b == 0)  
  5169.    /* preempt divide by zero error */  
  5170.       raise(SIGFPE);  
  5171.    a = a / b;  
  5172.    return 0;  
  5173. }  
  5174.    
  5175.    

  5176. 函数名: rand  
  5177. 功  能: 随机数发生器  
  5178. 用  法: void rand(void);  
  5179. 程序例:  

  5180. #include <stdlib.h>  
  5181. #include <stdio.h>  

  5182. int main(void)  
  5183. {  
  5184.    int i;  

  5185.    printf("Ten random numbers from 0 to 99\n\n");  
  5186.    for(i=0; i<10; i++)  
  5187.       printf("%d\n", rand() % 100);  
  5188.    return 0;  
  5189. }  
  5190.    
  5191.    

  5192. 函数名: randbrd  
  5193. 功  能: 随机块读  
  5194. 用  法: int randbrd(struct fcb *fcbptr, int reccnt);  
  5195. 程序例:  

  5196. #include <process.h>  
  5197. #include <string.h>  
  5198. #include <stdio.h>  
  5199. #include <dos.h>  

  5200. int main(void)  
  5201. {  
  5202.    char far *save_dta;  
  5203.    char line[80], buffer[256];  
  5204.    struct fcb blk;  
  5205.    int i, result;  

  5206.    /* get user input file name for dta */  
  5207.    printf("Enter drive and file name (no path - i.e. a:file.dat)\n");  
  5208.    gets(line);  

  5209.    /* put file name in fcb */  
  5210.    if (!parsfnm(line, &blk, 1))  
  5211.    {  
  5212.       printf("Error in call to parsfnm\n");  
  5213.       exit(1);  
  5214.    }  
  5215.    printf("Drive #%d  File: %s\n\n", blk.fcb_drive, blk.fcb_name);  

  5216.    /* open file with DOS FCB open file */  
  5217.    bdosptr(0x0F, &blk, 0);  

  5218.    /* save old dta, and set new one */  
  5219.    save_dta = getdta();  
  5220.    setdta(buffer);  

  5221.    /* set up info for the new dta */  
  5222.    blk.fcb_recsize = 128;  
  5223.    blk.fcb_random = 0L;  
  5224.    result = randbrd(&blk, 1);  

  5225.    /* check results from randbrd */  
  5226.    if (!result)  
  5227.       printf("Read OK\n\n");  
  5228.    else  
  5229.    {  
  5230.       perror("Error during read");  
  5231.       exit(1);  
  5232.    }  

  5233.    /* read in data from the new dta */  
  5234.    printf("The first 128 characters are:\n");  
  5235.    for (i=0; i<128; i++)  
  5236.       putchar(buffer[i]);  

  5237.    /* restore previous dta */  
  5238.    setdta(save_dta);  

  5239.    return 0;  
  5240. }  
  5241.    

  5242. 函数名: randbwr  
  5243. 功  能: 随机块写  
  5244. 用  法: int randbwr(struct fcp *fcbptr, int reccnt);  
  5245. 程序例:  

  5246. #include <process.h>  
  5247. #include <string.h>  
  5248. #include <stdio.h>  
  5249. #include <dos.h>  

  5250. int main(void)  
  5251. {  
  5252.    char far *save_dta;  
  5253.    char line[80];  
  5254.    char buffer[256] = "RANDBWR test!";  
  5255.    struct fcb blk;  
  5256.    int result;  

  5257.    /* get new file name from user */  
  5258.    printf("Enter a file name to create (no path - ie. a:file.dat\n");  
  5259.    gets(line);  

  5260.    /* parse the new file name to the dta */  
  5261.    parsfnm(line,&blk,1);  
  5262.    printf("Drive #%d  File: %s\n", blk.fcb_drive, blk.fcb_name);  

  5263.    /* request DOS services to create file */  
  5264.    if (bdosptr(0x16, &blk, 0) == -1)  
  5265.    {  
  5266.       perror("Error creating file");  
  5267.       exit(1);  
  5268.    }  

  5269.    /* save old dta and set new dta */  
  5270.    save_dta = getdta();  
  5271.    setdta(buffer);  

  5272.    /* write new records */  
  5273.    blk.fcb_recsize = 256;  
  5274.    blk.fcb_random = 0L;  
  5275.    result = randbwr(&blk, 1);  

  5276.    if (!result)  
  5277.       printf("Write OK\n");  
  5278.    else  
  5279.    {  
  5280.       perror("Disk error");  
  5281.       exit(1);  
  5282.    }  

  5283.    /* request DOS services to close the file */  
  5284.    if (bdosptr(0x10, &blk, 0) == -1)  
  5285.    {  
  5286.       perror("Error closing file");  
  5287.       exit(1);  
  5288.    }  

  5289.    /* reset the old dta */  
  5290.    setdta(save_dta);  

  5291.    return 0;  
  5292. }  
  5293.    
  5294.    

  5295. 函数名: random  
  5296. 功  能: 随机数发生器  
  5297. 用  法: int random(int num);  
  5298. 程序例:  

  5299. #include <stdlib.h>  
  5300. #include <stdio.h>  
  5301. #include <time.h>  

  5302. /* prints a random number in the range 0 to 99 */  
  5303. int main(void)  
  5304. {  
  5305.    randomize();  
  5306.    printf("Random number in the 0-99 range: %d\n", random (100));  
  5307.    return 0;  
  5308. }  
  5309.    
  5310.    

  5311. 函数名: randomize  
  5312. 功  能: 初始化随机数发生器  
  5313. 用  法: void randomize(void);  
  5314. 程序例:  

  5315. #include <stdlib.h>  
  5316. #include <stdio.h>  
  5317. #include <time.h>  

  5318. int main(void)  
  5319. {  
  5320.    int i;  

  5321.    randomize();  
  5322.    printf("Ten random numbers from 0 to 99\n\n");  
  5323.    for(i=0; i<10; i++)  
  5324.        printf("%d\n", rand() % 100);  
  5325.    return 0;  
  5326. }  
  5327.    
  5328.    

  5329. 函数名: read  
  5330. 功  能: 从文件中读  
  5331. 用  法: int read(int handle, void *buf, int nbyte);  
  5332. 程序例:  

  5333. #include <stdio.h>  
  5334. #include <io.h>  
  5335. #include <alloc.h>  
  5336. #include <fcntl.h>  
  5337. #include <process.h>  
  5338. #include <sys\stat.h>  

  5339. int main(void)  
  5340. {  
  5341.    void *buf;  
  5342.    int handle, bytes;  

  5343.    buf = malloc(10);  

  5344. /*  
  5345.    Looks for a file in the current directory named TEST.
  5346. $andattemptstoread10bytesfromit.TousethisexampleyoushouldcreatethefileTEST.
  5347. $

















  5348. 10











  5349. .










































  5350. .
  5351. $  
  5352. */  
  5353.    if ((handle =  
  5354.       open("TEST.$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)  
  5355.    {  
  5356.       printf("Error Opening File\n");  
  5357.       exit(1);  
  5358.    }  

  5359.    if ((bytes = read(handle, buf, 10)) == -1) {  
  5360.       printf("Read Failed.\n");  
  5361.       exit(1);  
  5362.    }  
  5363.    else {  
  5364.       printf("Read: %d bytes read.\n", bytes);  
  5365.    }  
  5366.    return 0;  
  5367. }  
  5368.    
  5369.    

  5370. 函数名: realloc  
  5371. 功  能: 重新分配主存  
  5372. 用  法: void *realloc(void *ptr, unsigned newsize);  
  5373. 程序例:  

  5374. #include <stdio.h>  
  5375. #include <alloc.h>  
  5376. #include <string.h>  

  5377. int main(void)  
  5378. {  
  5379.    char *str;  

  5380.    /* allocate memory for string */  
  5381.    str = malloc(10);  

  5382.    /* copy "Hello" into string */  
  5383.    strcpy(str, "Hello");  

  5384.    printf("String is %s\n  Address is %p\n", str, str);  
  5385.    str = realloc(str, 20);  
  5386.    printf("String is %s\n  New address is %p\n", str, str);  

  5387.    /* free memory */  
  5388.    free(str);  

  5389.    return 0;  
  5390. }  
  5391.    
  5392.    

  5393. 函数名: rectangle  
  5394. 功  能: 画一个矩形  
  5395. 用  法: void far rectangle(int left, int top, int right, int bottom);  
  5396. 程序例:  

  5397. #include <graphics.h>  
  5398. #include <stdlib.h>  
  5399. #include <stdio.h>  
  5400. #include <conio.h>  

  5401. int main(void)  
  5402. {  
  5403.    /* request auto detection */  
  5404.    int gdriver = DETECT, gmode, errorcode;  
  5405.    int left, top, right, bottom;  

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

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

  5417.    left = getmaxx() / 2 - 50;  
  5418.    top = getmaxy() / 2 - 50;  
  5419.    right = getmaxx() / 2 + 50;  
  5420.    bottom = getmaxy() / 2 + 50;  

  5421.    /* draw a rectangle */  
  5422.    rectangle(left,top,right,bottom);  

  5423.    /* clean up */  
  5424.    getch();  
  5425.    closegraph();  
  5426.    return 0;  
  5427. }  
  5428.    
  5429.    

  5430. 函数名: registerbgidriver  
  5431. 功  能: 登录已连接进来的图形驱动程序代码  
  5432. 用  法: int registerbgidriver(void(*driver)(void));  
  5433. 程序例:  

  5434. #include <graphics.h>  
  5435. #include <stdlib.h>  
  5436. #include <stdio.h>  
  5437. #include <conio.h>  

  5438. int main(void)  
  5439. {  
  5440.    /* request auto detection */  
  5441.    int gdriver = DETECT, gmode, errorcode;  

  5442.    /* register a driver that was added into graphics.lib */  
  5443.    errorcode = registerbgidriver(EGAVGA_driver);  

  5444.    /* report any registration errors */  
  5445.    if (errorcode < 0)  
  5446.    {  
  5447.       printf("Graphics error: %s\n", grapherrormsg(errorcode));  
  5448.       printf("Press any key to halt:");  
  5449.       getch();  
  5450.       exit(1); /* terminate with an error code */  
  5451.    }  

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

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

  5463.    /* draw a line */  
  5464.    line(0, 0, getmaxx(), getmaxy());  

  5465.    /* clean up */  
  5466.    getch();  
  5467.    closegraph();  
  5468.    return 0;  
  5469. }  
  5470.    
  5471.    

  5472. 函数名: remove  
  5473. 功  能: 删除一个文件  
  5474. 用  法: int remove(char *filename);  
  5475. 程序例:  

  5476. #include <stdio.h>  

  5477. int main(void)  
  5478. {  
  5479.    char file[80];  

  5480.    /* prompt for file name to delete */  
  5481.    printf("File to delete: ");  
  5482.    gets(file);  

  5483.    /* delete the file */  
  5484.    if (remove(file) == 0)  
  5485.       printf("Removed %s.\n",file);  
  5486.    else  
  5487.       perror("remove");  

  5488.    return 0;  
  5489. }  
  5490.    
  5491.    

  5492. 函数名: rename  
  5493. 功  能: 重命名文件  
  5494. 用  法: int rename(char *oldname, char *newname);  
  5495. 程序例:  

  5496. #include <stdio.h>  

  5497. int main(void)  
  5498. {  
  5499.    char oldname[80], newname[80];  

  5500.    /* prompt for file to rename and new name */  
  5501.    printf("File to rename: ");  
  5502.    gets(oldname);  
  5503.    printf("New name: ");  
  5504.    gets(newname);  

  5505.    /* Rename the file */  
  5506.    if (rename(oldname, newname) == 0)  
  5507.       printf("Renamed %s to %s.\n", oldname, newname);  
  5508.    else  
  5509.       perror("rename");  

  5510.    return 0;  
  5511. }  
  5512.    
  5513.    

  5514. 函数名: restorecrtmode  
  5515. 功  能: 将屏幕模式恢复为先前的imitgraph设置  
  5516. 用  法: void far restorecrtmode(void);  
  5517. 程序例:  

  5518. #include <graphics.h>  
  5519. #include <stdlib.h>  
  5520. #include <stdio.h>  
  5521. #include <conio.h>  

  5522. int main(void)  
  5523. {  
  5524.    /* request auto detection */  
  5525.    int gdriver = DETECT, gmode, errorcode;  
  5526.    int x, y;  

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

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

  5538.    x = getmaxx() / 2;  
  5539.    y = getmaxy() / 2;  

  5540.    /* output a message */  
  5541.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  5542.    outtextxy(x, y, "Press any key to exit graphics:");  
  5543.    getch();  

  5544.    /* restore system to text mode */  
  5545.    restorecrtmode();  
  5546.    printf("We're now in text mode.\n");  
  5547.    printf("Press any key to return to graphics mode:");  
  5548.    getch();  

  5549.    /* return to graphics mode */  
  5550.    setgraphmode(getgraphmode());  

  5551.    /* output a message */  
  5552.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  5553.    outtextxy(x, y, "We're back in graphics mode.");  
  5554.    outtextxy(x, y+textheight("W"), "Press any key to halt:");  

  5555.    /* clean up */  
  5556.    getch();  
  5557.    closegraph();  
  5558.    return 0;  
  5559. }  
  5560.    
  5561.    

  5562. 函数名: rewind  
  5563. 功  能: 将文件指针重新指向一个流的开头  
  5564. 用  法: int rewind(FILE *stream);  
  5565. 程序例:  

  5566. #include <stdio.h>  
  5567. #include <dir.h>  

  5568. int main(void)  
  5569. {  
  5570.     FILE *fp;  
  5571.     char *fname = "TXXXXXX", *newname, first;  

  5572.     newname = mktemp(fname);  
  5573.     fp = fopen(newname,"w+");  
  5574.     fprintf(fp,"abcdefghijklmnopqrstuvwxyz");  
  5575.     rewind(fp);  
  5576.     fscanf(fp,"%c",&first);  
  5577.     printf("The first character is: %c\n",first);  
  5578.     fclose(fp);  
  5579.     remove(newname);  

  5580.     return 0;  
  5581. }  
  5582.    
  5583.    

  5584. 函数名: rmdir  
  5585. 功  能: 删除DOS文件目录  
  5586. 用  法: int rmdir(char *stream);  
  5587. 程序例:  

  5588. #include <stdio.h>  
  5589. #include <conio.h>  
  5590. #include <process.h>  
  5591. #include <dir.h>  

  5592. #define DIRNAME "testdir.$"  

  5593. int main(void)  
  5594. {  
  5595.    int stat;  

  5596.    stat = mkdir(DIRNAME);  
  5597.    if (!stat)  
  5598.           printf("Directory created\n");  
  5599.    else  
  5600.    {  
  5601.       printf("Unable to create directory\n");  
  5602.       exit(1);  
  5603.    }  

  5604.    getch();  
  5605.    system("dir/p");  
  5606.    getch();  

  5607.    stat = rmdir(DIRNAME);  
  5608.    if (!stat)  
  5609.           printf("\nDirectory deleted\n");  
  5610.    else  
  5611.    {  
  5612.    perror("\nUnable to delete directory\n");  
  5613.       exit(1);  
  5614.    }  

  5615.    return 0;  
  5616. }  
  5617. 函数名: sbrk  
  5618. 功  能: 改变数据段空间位置  
  5619. 用  法: char *sbrk(int incr);  
  5620. 程序例:  

  5621. #include <stdio.h>  
  5622. #include <alloc.h>  

  5623. int main(void)  
  5624. {  
  5625.    printf("Changing allocation with sbrk()\n");  
  5626.    printf("Before sbrk() call: %lu bytes free\n",  
  5627.    (unsigned long) coreleft());  
  5628.    sbrk(1000);  
  5629.    printf(" After sbrk() call: %lu bytes free\n",  
  5630.    (unsigned long) coreleft());  
  5631.    return 0;  
  5632. }  
  5633.    
  5634.    

  5635. 函数名: scanf  
  5636. 功  能: 执行格式化输入  
  5637. 用  法: int scanf(char *format[,argument,...]);  
  5638. 程序例:  

  5639. #include <stdio.h>  
  5640. #include <conio.h>  

  5641. int main(void)  
  5642. {  
  5643.    char label[20];  
  5644.    char name[20];  
  5645.    int entries = 0;  
  5646.    int loop, age;  
  5647.    double salary;  

  5648.    struct Entry_struct  
  5649.    {  
  5650.       char  name[20];  
  5651.       int   age;  
  5652.       float salary;  
  5653.    } entry[20];  

  5654. /* Input a label as a string of characters restricting to 20 characters */  
  5655.    printf("\n\nPlease enter a label for the chart: ");  
  5656.    scanf("%20s", label);  
  5657.    fflush(stdin);  /* flush the input stream in case of bad input */  

  5658. /* Input number of entries as an integer */  
  5659.    printf("How many entries will there be? (less than 20) ");  
  5660.    scanf("%d", &entries);  
  5661.    fflush(stdin);   /* flush the input stream in case of bad input */  

  5662. /* input a name restricting input to only letters upper or lower case */  
  5663.    for (loop=0;loop<entries;++loop)  
  5664.    {  
  5665.       printf("Entry %d\n", loop);  
  5666.       printf("  Name   : ");  
  5667.       scanf("%[A-Za-z]", entry[loop].name);  
  5668.       fflush(stdin);  /* flush the input stream in case of bad input */  

  5669. /* input an age as an integer */  
  5670.       printf("  Age    : ");  
  5671.       scanf("%d", &entry[loop].age);  
  5672.       fflush(stdin);  /* flush the input stream in case of bad input */  

  5673. /* input a salary as a float */  
  5674.       printf("  Salary : ");  
  5675.       scanf("%f", &entry[loop].salary);  
  5676.       fflush(stdin); /* flush the input stream in case of bad input */  
  5677.    }  

  5678. /* Input a name, age and salary as a string, integer, and double */  
  5679.    printf("\nPlease enter your name, age and salary\n");  
  5680.    scanf("%20s %d %lf", name, &age, &salary);  
  5681.    

  5682. /* Print out the data that was input */  
  5683.    printf("\n\nTable %s\n",label);  
  5684.    printf("Compiled by %s  age %d  $%15.2lf\n", name, age, salary);  
  5685.    printf("-----------------------------------------------------\n");  
  5686.    for (loop=0;loop<entries;++loop)  
  5687.       printf("%4d | %-20s | %5d | %15.2lf\n",  
  5688.          loop + 1,  
  5689.   entry[loop].name,  
  5690.   entry[loop].age,  
  5691.          entry[loop].salary);  
  5692.    printf("-----------------------------------------------------\n");  
  5693.    return 0;  
  5694. }  
  5695.    
  5696.    

  5697. 函数名: searchpath  
  5698. 功  能: 搜索DOS路径  
  5699. 用  法: char *searchpath(char *filename);  
  5700. 程序例:  

  5701. #include <stdio.h>  
  5702. #include <dir.h>  

  5703. int main(void)  
  5704. {  
  5705.    char *p;  

  5706.    /* Looks for TLINK and returns a pointer  
  5707.       to the path  */  
  5708.    p = searchpath("TLINK.EXE");  
  5709.    printf("Search for TLINK.EXE : %s\n", p);  

  5710.    /* Looks for non-existent file  */  
  5711.    p = searchpath("NOTEXIST.FIL");  
  5712.    printf("Search for NOTEXIST.FIL : %s\n", p);  

  5713.    return 0;  
  5714. }  
  5715.    
  5716.    

  5717. 函数名: sector  
  5718. 功  能: 画并填充椭圆扇区  
  5719. 用  法: void far sector(int x, int y, int stangle, int endangle);  
  5720. 程序例:  

  5721. #include <graphics.h>  
  5722. #include <stdlib.h>  
  5723. #include <stdio.h>  
  5724. #include <conio.h>  

  5725. int main(void)  
  5726. {  
  5727.    /* request auto detection */  
  5728.    int gdriver = DETECT, gmode, errorcode;  
  5729.    int midx, midy, i;  
  5730.    int stangle = 45, endangle = 135;  
  5731.    int xrad = 100, yrad = 50;  

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

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

  5743.    midx = getmaxx() / 2;  
  5744.    midy = getmaxy() / 2;  

  5745.    /* loop through the fill patterns */  
  5746.    for (i=EMPTY_FILL; i<USER_FILL; i++)  
  5747.    {  
  5748.       /* set the fill style */  
  5749.       setfillstyle(i, getmaxcolor());  

  5750.       /* draw the sector slice */  
  5751.       sector(midx, midy, stangle, endangle, xrad, yrad);  

  5752.       getch();  
  5753.    }  

  5754.    /* clean up */  
  5755.    closegraph();  
  5756.    return 0;  
  5757. }  
  5758.    

  5759. 函数名: segread  
  5760. 功  能: 读段寄存器值  
  5761. 用  法: void segread(struct SREGS *segtbl);  
  5762. 程序例:  

  5763. #include <stdio.h>  
  5764. #include <dos.h>  

  5765. int main(void)  
  5766. {  
  5767.    struct SREGS segs;  

  5768.    segread(&segs);  
  5769.    printf("Current segment register settings\n\n");  
  5770.    printf("CS: %X   DS: %X\n", segs.cs, segs.ds);  
  5771.    printf("ES: %X   SS: %X\n", segs.es, segs.ss);  

  5772.    return 0;  
  5773. }  
  5774.    
  5775.    

  5776. 函数名: setactivepage  
  5777. 功  能: 设置图形输出活动页  
  5778. 用  法: void far setactivepage(int pagenum);  
  5779. 程序例:  

  5780. #include <graphics.h>  
  5781. #include <stdlib.h>  
  5782. #include <stdio.h>  
  5783. #include <conio.h>  

  5784. int main(void)  
  5785. {  
  5786.    /* select a driver and mode that supports */  
  5787.    /* multiple pages.                        */  
  5788.    int gdriver = EGA, gmode = EGAHI, errorcode;  
  5789.    int x, y, ht;  

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

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

  5801.    x = getmaxx() / 2;  
  5802.    y = getmaxy() / 2;  
  5803.    ht = textheight("W");  

  5804.    /*  select the off screen page for drawing */  
  5805.    setactivepage(1);  

  5806.    /* draw a line on page #1 */  
  5807.    line(0, 0, getmaxx(), getmaxy());  

  5808.    /* output a message on page #1 */  
  5809.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  5810.    outtextxy(x, y, "This is page #1:");  
  5811.    outtextxy(x, y+ht, "Press any key to halt:");  

  5812.    /* select drawing to page #0 */  
  5813.    setactivepage(0);  

  5814.    /* output a message  on page #0 */  
  5815.    outtextxy(x, y, "This is page #0.");  
  5816.    outtextxy(x, y+ht, "Press any key to view page #1:");  
  5817.    getch();  

  5818.    /* select page #1 as the visible page */  
  5819.    setvisualpage(1);  

  5820.    /* clean up */  
  5821.    getch();  
  5822.    closegraph();  
  5823.    return 0;  
  5824. }  
  5825.    
  5826.    

  5827. 函数名: setallpallette  
  5828. 功  能: 按指定方式改变所有的调色板颜色  
  5829. 用  法: void far setallpallette(struct palette, far *pallette);  
  5830. 程序例:  

  5831. #include <graphics.h>  
  5832. #include <stdlib.h>  
  5833. #include <stdio.h>  
  5834. #include <conio.h>  

  5835. int main(void)  
  5836. {  
  5837.    /* request auto detection */  
  5838.    int gdriver = DETECT, gmode, errorcode;  
  5839.    struct palettetype pal;  
  5840.    int color, maxcolor, ht;  
  5841.    int y = 10;  
  5842.    char msg[80];  

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

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

  5854.    maxcolor = getmaxcolor();  
  5855.    ht = 2 * textheight("W");  

  5856.    /* grab a copy of the palette */  
  5857.    getpalette(&pal);  

  5858.    /* display the default palette colors */  
  5859.    for (color=1; color<=maxcolor; color++)  
  5860.    {  
  5861.       setcolor(color);  
  5862.       sprintf(msg, "Color: %d", color);  
  5863.       outtextxy(1, y, msg);  
  5864.       y += ht;  
  5865.    }  

  5866.    /* wait for a key */  
  5867.    getch();  

  5868.    /* black out the colors one by one */  
  5869.    for (color=1; color<=maxcolor; color++)  
  5870.    {  
  5871.       setpalette(color, BLACK);  
  5872.       getch();  
  5873.    }  

  5874.    /* restore the palette colors */  
  5875.    setallpalette(&pal);  

  5876.    /* clean up */  
  5877.    getch();  
  5878.    closegraph();  
  5879.    return 0;  
  5880. }  
  5881.    
  5882.    

  5883. 函数名: setaspectratio  
  5884. 功  能: 设置图形纵横比  
  5885. 用  法: void far setaspectratio(int xasp, int yasp);  
  5886. 程序例:  

  5887. #include <graphics.h>  
  5888. #include <stdlib.h>  
  5889. #include <stdio.h>  
  5890. #include <conio.h>  

  5891. int main(void)  
  5892. {  
  5893.    /* request auto detection */  
  5894.    int gdriver = DETECT, gmode, errorcode;  
  5895.    int xasp, yasp, midx, midy;  

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

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

  5907.    midx = getmaxx() / 2;  
  5908.    midy = getmaxy() / 2;  
  5909.    setcolor(getmaxcolor());  

  5910.    /* get current aspect ratio settings */  
  5911.    getaspectratio(&xasp, &yasp);  

  5912.    /* draw normal circle */  
  5913.    circle(midx, midy, 100);  
  5914.    getch();  

  5915.    /* claer the screen */  
  5916.    cleardevice();  

  5917.    /* adjust the aspect for a wide circle */  
  5918.    setaspectratio(xasp/2, yasp);  
  5919.    circle(midx, midy, 100);  
  5920.    getch();  

  5921.    /* adjust the aspect for a narrow circle */  
  5922.    cleardevice();  
  5923.    setaspectratio(xasp, yasp/2);  
  5924.    circle(midx, midy, 100);  

  5925.    /* clean up */  
  5926.    getch();  
  5927.    closegraph();  
  5928.    return 0;  
  5929. }  
  5930.    
  5931.    

  5932. 函数名: setbkcolor  
  5933. 功  能: 用调色板设置当前背景颜色  
  5934. 用  法: void far setbkcolor(int color);  
  5935. 程序例:  

  5936. #include <graphics.h>  
  5937. #include <stdlib.h>  
  5938. #include <stdio.h>  
  5939. #include <conio.h>  

  5940. int main(void)  
  5941. {  
  5942.    /* select a driver and mode that supports */  
  5943.    /* multiple background colors.            */  
  5944.    int gdriver = EGA, gmode = EGAHI, errorcode;  
  5945.    int bkcol, maxcolor, x, y;  
  5946.    char msg[80];  

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

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

  5958.    /* maximum color index supported */  
  5959.    maxcolor = getmaxcolor();  

  5960.    /* for centering text messages */  
  5961.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  5962.    x = getmaxx() / 2;  
  5963.    y = getmaxy() / 2;  

  5964.    /* loop through the available colors */  
  5965.    for (bkcol=0; bkcol<=maxcolor; bkcol++)  
  5966.    {  
  5967.       /* clear the screen */  
  5968.       cleardevice();  

  5969.       /* select a new background color */  
  5970.       setbkcolor(bkcol);  

  5971.       /* output a messsage */  
  5972.       if (bkcol == WHITE)  
  5973.   setcolor(EGA_BLUE);  
  5974.       sprintf(msg, "Background color: %d", bkcol);  
  5975.       outtextxy(x, y, msg);  
  5976.       getch();  
  5977.    }  

  5978.    /* clean up */  
  5979.    closegraph();  
  5980.    return 0;  
  5981. }  
  5982.    
  5983.    

  5984. 函数名: setblock  
  5985. 功  能: 修改先前已分配的DOS存储段大小  
  5986. 用  法: int setblock(int seg, int newsize);  
  5987. 程序例:  

  5988. #include <dos.h>  
  5989. #include <alloc.h>  
  5990. #include <stdio.h>  
  5991. #include <stdlib.h>  

  5992. int main(void)  
  5993. {  
  5994.    unsigned int size, segp;  
  5995.    int stat;  

  5996.    size = 64; /* (64 x 16) = 1024 bytes */  
  5997.    stat = allocmem(size, &segp);  
  5998.    if (stat == -1)  
  5999.       printf("Allocated memory at segment: %X\n", segp);  
  6000.    else  
  6001.    {  
  6002.       printf("Failed: maximum number of paragraphs available is %d\n",  
  6003.       stat);  
  6004.       exit(1);  
  6005.    }  

  6006.    stat = setblock(segp, size * 2);  
  6007.    if (stat == -1)  
  6008.       printf("Expanded memory block at segment: %X\n", segp);  
  6009.    else  
  6010.       printf("Failed: maximum number of paragraphs available is %d\n",  
  6011.              stat);  

  6012.    freemem(segp);  

  6013.    return 0;  
  6014. }  
  6015.    
  6016.    

  6017. 函数名: setbuf  
  6018. 功  能: 把缓冲区与流相联  
  6019. 用  法: void setbuf(FILE *steam, char *buf);  
  6020. 程序例:  

  6021. #include <stdio.h>  

  6022. /* BUFSIZ is defined in stdio.h */  
  6023. char outbuf[BUFSIZ];  

  6024. int main(void)  
  6025. {  
  6026.    /* attach a buffer to the standard output stream */  
  6027.    setbuf(stdout, outbuf);  

  6028.    /* put some characters into the buffer */  
  6029.    puts("This is a test of buffered output.\n\n");  
  6030.    puts("This output will go into outbuf\n");  
  6031.    puts("and won't appear until the buffer\n");  
  6032.    puts("fills up or we flush the stream.\n");  

  6033.    /* flush the output buffer */  
  6034.    fflush(stdout);  

  6035.    return 0;  
  6036. }  
  6037.    
  6038.    

  6039. 函数名: setcbrk  
  6040. 功  能: 设置Control-break  
  6041. 用  法: int setcbrk(int value);  
  6042. 程序例:  

  6043. #include <dos.h>  
  6044. #include <conio.h>  
  6045. #include <stdio.h>  

  6046. int main(void)  
  6047. {  
  6048.    int break_flag;  

  6049.    printf("Enter 0 to turn control break off\n");  
  6050.    printf("Enter 1 to turn control break on\n");  

  6051.    break_flag = getch() - 0;  

  6052.    setcbrk(break_flag);  

  6053.    if (getcbrk())  
  6054.       printf("Cntrl-brk flag is on\n");  
  6055.    else  
  6056.       printf("Cntrl-brk flag is off\n");  
  6057.    return 0;  
  6058. }  
  6059.    
  6060.    
  6061.    

  6062. 函数名: setcolor  
  6063. 功  能: 设置当前画线颜色  
  6064. 用  法: void far setcolor(int color);  
  6065. 程序例:  

  6066. #include <graphics.h>  
  6067. #include <stdlib.h>  
  6068. #include <stdio.h>  
  6069. #include <conio.h>  

  6070. int main(void)  
  6071. {  
  6072.    /* select a driver and mode that supports */  
  6073.    /* multiple drawing colors.               */  
  6074.    int gdriver = EGA, gmode = EGAHI, errorcode;  
  6075.    int color, maxcolor, x, y;  
  6076.    char msg[80];  

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

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

  6088.    /* maximum color index supported */  
  6089.    maxcolor = getmaxcolor();  

  6090.    /* for centering text messages */  
  6091.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  6092.    x = getmaxx() / 2;  
  6093.    y = getmaxy() / 2;  

  6094.    /* loop through the available colors */  
  6095.    for (color=1; color<=maxcolor; color++)  
  6096.    {  
  6097.       /* clear the screen */  
  6098.       cleardevice();  

  6099.       /* select a new background color */  
  6100.       setcolor(color);  

  6101.       /* output a messsage */  
  6102.       sprintf(msg, "Color: %d", color);  
  6103.       outtextxy(x, y, msg);  
  6104.       getch();  
  6105.    }  

  6106.    /* clean up */  
  6107.    closegraph();  
  6108.    return 0;  
  6109. }  
  6110.    
  6111.    

  6112. 函数名: setdate  
  6113. 功  能: 设置DOS日期  
  6114. 用  法: void setdate(struct date *dateblk);  
  6115. 程序例:  

  6116. #include <stdio.h>  
  6117. #include <process.h>  
  6118. #include <dos.h>  

  6119. int main(void)  
  6120. {  
  6121.    struct date reset;  
  6122.    struct date save_date;  

  6123.    getdate(&save_date);  
  6124.    printf("Original date:\n");  
  6125.    system("date");  

  6126.    reset.da_year = 2001;  
  6127.    reset.da_day = 1;  
  6128.    reset.da_mon = 1;  
  6129.    setdate(&reset);  

  6130.    printf("Date after setting:\n");  
  6131.    system("date");  

  6132.    setdate(&save_date);  
  6133.    printf("Back to original date:\n");  
  6134.    system("date");  

  6135.    return 0;  
  6136. }  
  6137.    
  6138.    

  6139. 函数名: setdisk  
  6140. 功  能: 设置当前磁盘驱动器  
  6141. 用  法: int setdisk(int drive);  
  6142. 程序例:  

  6143. #include <stdio.h>  
  6144. #include <dir.h>  

  6145. int main(void)  
  6146. {  
  6147.    int save, disk, disks;  

  6148.    /* save original drive */  
  6149.    save = getdisk();  

  6150.    /* print number of logic drives */  
  6151.    disks = setdisk(save);  
  6152.    printf("%d logical drives on the system\n\n", disks);  

  6153.    /* print the drive letters available */  
  6154.    printf("Available drives:\n");  
  6155.    for (disk = 0;disk < 26;++disk)  
  6156.    {  
  6157.       setdisk(disk);  
  6158.       if (disk == getdisk())  
  6159.          printf("%c: drive is available\n", disk + 'a');  
  6160.    }  
  6161.    setdisk(save);  

  6162.    return 0;  
  6163. }  
  6164.    
  6165.    

  6166. 函数名: setdta  
  6167. 功  能: 设置磁盘传输区地址  
  6168. 用  法: void setdta(char far *dta);  
  6169. 程序例:  

  6170. #include <process.h>  
  6171. #include <string.h>  
  6172. #include <stdio.h>  
  6173. #include <dos.h>  

  6174. int main(void)  
  6175. {  
  6176.    char line[80], far *save_dta;  
  6177.    char buffer[256] = "SETDTA test!";  
  6178.    struct fcb blk;  
  6179.    int result;  

  6180.    /* get new file name from user */  
  6181.    printf("Enter a file name to create:");  
  6182.    gets(line);  

  6183.    /* parse the new file name to the dta */  
  6184.    parsfnm(line, &blk, 1);  
  6185.    printf("%d %s\n", blk.fcb_drive, blk.fcb_name);  

  6186.    /* request DOS services to create file */  
  6187.    if (bdosptr(0x16, &blk, 0) == -1)  
  6188.    {  
  6189.       perror("Error creating file");  
  6190.       exit(1);  
  6191.    }  

  6192.    /* save old dta and set new dta */  
  6193.    save_dta = getdta();  
  6194.    setdta(buffer);  

  6195.    /* write new records */  
  6196.    blk.fcb_recsize = 256;  
  6197.    blk.fcb_random = 0L;  
  6198.    result = randbwr(&blk, 1);  
  6199.    printf("result = %d\n", result);  

  6200.    if (!result)  
  6201.       printf("Write OK\n");  
  6202.    else  
  6203.    {  
  6204.       perror("Disk error");  
  6205.       exit(1);  
  6206.    }  

  6207.    /* request DOS services to close the file */  
  6208.    if (bdosptr(0x10, &blk, 0) == -1)  
  6209.    {  
  6210.       perror("Error closing file");  
  6211.       exit(1);  
  6212.    }  

  6213.    /* reset the old dta */  
  6214.    setdta(save_dta);  
  6215.    return 0;  
  6216. }  
  6217.    
  6218.    

  6219. 函数名: setfillpattern  
  6220. 功  能: 选择用户定义的填充模式  
  6221. 用  法: void far setfillpattern(char far *upattern, int color);  
  6222. 程序例:  

  6223. #include <graphics.h>  
  6224. #include <stdlib.h>  
  6225. #include <stdio.h>  
  6226. #include <conio.h>  

  6227. int main(void)  
  6228. {  
  6229.    /* request auto detection */  
  6230.    int gdriver = DETECT, gmode, errorcode;  
  6231.    int maxx, maxy;  

  6232.    /* a user defined fill pattern */  
  6233.    char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00};  

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

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

  6245.    maxx = getmaxx();  
  6246.    maxy = getmaxy();  
  6247.    setcolor(getmaxcolor());  

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

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

  6252.    /* clean up */  
  6253.    getch();  
  6254.    closegraph();  
  6255.    return 0;  
  6256. }  
  6257.    
  6258.    

  6259. 函数名: setfillstyle  
  6260. 功  能: 设置填充模式和颜色  
  6261. 用  法: void far setfillstyle(int pattern, int color);  
  6262. 程序例:  

  6263. #include <graphics.h>  
  6264. #include <stdlib.h>  
  6265. #include <string.h>  
  6266. #include <stdio.h>  
  6267. #include <conio.h>  

  6268. /* the names of the fill styles supported */  
  6269. char *fname[] = { "EMPTY_FILL",  
  6270.                   "SOLID_FILL",  
  6271.                   "LINE_FILL",  
  6272.                   "LTSLASH_FILL",  
  6273.                   "SLASH_FILL",  
  6274.                   "BKSLASH_FILL",  
  6275.                   "LTBKSLASH_FILL",  
  6276.     "HATCH_FILL",  
  6277.                   "XHATCH_FILL",  
  6278.                   "INTERLEAVE_FILL",  
  6279.                   "WIDE_DOT_FILL",  
  6280.                   "CLOSE_DOT_FILL",  
  6281.     "USER_FILL"  
  6282.                 };  

  6283. int main(void)  
  6284. {  
  6285.    /* request auto detection */  
  6286.    int gdriver = DETECT, gmode, errorcode;  
  6287.    int style, midx, midy;  
  6288.    char stylestr[40];  

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

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

  6300.    midx = getmaxx() / 2;  
  6301.    midy = getmaxy() / 2;  

  6302.    for (style = EMPTY_FILL; style < USER_FILL; style++)  
  6303.    {  
  6304.       /* select the fill style */  
  6305.       setfillstyle(style, getmaxcolor());  

  6306.       /* convert style into a string */  
  6307.       strcpy(stylestr, fname[style]);  

  6308.       /* fill a bar */  
  6309.       bar3d(0, 0, midx-10, midy, 0, 0);  

  6310.       /* output a message */  
  6311.       outtextxy(midx, midy, stylestr);  

  6312.       /* wait for a key */  
  6313.       getch();  
  6314.       cleardevice();  
  6315.    }  

  6316.    /* clean up */  
  6317.    getch();  
  6318.    closegraph();  
  6319.    return 0;  
  6320. }  
  6321.    
  6322.    

  6323. 函数名: setftime  
  6324. 功  能: 设置文件日期和时间  
  6325. 用  法: int setftime(int handle, struct ftime *ftimep);  
  6326. 程序例:  

  6327. #include <stdio.h>  
  6328. #include <process.h>  
  6329. #include <fcntl.h>  
  6330. #include <io.h>  

  6331. int main(void)  
  6332. {  
  6333.    struct ftime filet;  
  6334.    FILE *fp;  

  6335.    if ((fp = fopen("TEST.$", "w")) == NULL)  
  6336.    {  
  6337.       perror("Error:");  
  6338.       exit(1);  
  6339.    }  

  6340.    fprintf(fp, "testing...\n");  

  6341.    /* load ftime structure with new time and date */  
  6342.    filet.ft_tsec = 1;  
  6343.    filet.ft_min = 1;  
  6344.    filet.ft_hour = 1;  
  6345.    filet.ft_day = 1;  
  6346.    filet.ft_month = 1;  
  6347.    filet.ft_year = 21;  

  6348.    /* show current directory for time and date */  
  6349.    system("dir TEST.$");  

  6350.    /* change the time and date stamp*/  
  6351.    setftime(fileno(fp), &filet);  

  6352.    /* close and remove the temporary file */  
  6353.    fclose(fp);  

  6354.    system("dir TEST.$");  

  6355.    unlink("TEST.$");  
  6356.    return 0;  
  6357. }  
  6358.    
  6359.    

  6360. 函数名: setgraphbufsize  
  6361. 功  能: 改变内部图形缓冲区的大小  
  6362. 用  法: unsigned far setgraphbufsize(unsigned bufsize);  
  6363. 程序例:  

  6364. #include <graphics.h>  
  6365. #include <stdlib.h>  
  6366. #include <stdio.h>  
  6367. #include <conio.h>  

  6368. #define BUFSIZE 1000 /* internal graphics buffer size */  

  6369. int main(void)  
  6370. {  
  6371.    /* request auto detection */  
  6372.    int gdriver = DETECT, gmode, errorcode;  
  6373.    int x, y, oldsize;  
  6374.    char msg[80];  

  6375.    /* set the size of the internal graphics buffer */  
  6376.    /* before making a call to initgraph.           */  
  6377.    oldsize = setgraphbufsize(BUFSIZE);  

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

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

  6389.    x = getmaxx() / 2;  
  6390.    y = getmaxy() / 2;  

  6391.    /* output some messages */  
  6392.    sprintf(msg, "Graphics buffer size: %d", BUFSIZE);  
  6393.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  6394.    outtextxy(x, y, msg);  
  6395.    sprintf(msg, "Old graphics buffer size: %d", oldsize);  
  6396.    outtextxy(x, y+textheight("W"), msg);  

  6397.    /* clean up */  
  6398.    getch();  
  6399.    closegraph();  
  6400.    return 0;  
  6401. }  
  6402.    
  6403.    
  6404.    

  6405. 函数名: setgraphmode  
  6406. 功  能: 将系统设置成图形模式且清屏  
  6407. 用  法: void far setgraphmode(int mode);  
  6408. 程序例:  

  6409. #include <graphics.h>  
  6410. #include <stdlib.h>  
  6411. #include <stdio.h>  
  6412. #include <conio.h>  

  6413. int main(void)  
  6414. {  
  6415.    /* request auto detection */  
  6416.    int gdriver = DETECT, gmode, errorcode;  
  6417.    int x, y;  

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

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

  6429.    x = getmaxx() / 2;  
  6430.    y = getmaxy() / 2;  

  6431.    /* output a message */  
  6432.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  6433.    outtextxy(x, y, "Press any key to exit graphics:");  
  6434.    getch();  

  6435.    /* restore system to text mode */  
  6436.    restorecrtmode();  
  6437.    printf("We're now in text mode.\n");  
  6438.    printf("Press any key to return to graphics mode:");  
  6439.    getch();  

  6440.    /* return to graphics mode */  
  6441.    setgraphmode(getgraphmode());  

  6442.    /* output a message */  
  6443.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  6444.    outtextxy(x, y, "We're back in graphics mode.");  
  6445.    outtextxy(x, y+textheight("W"), "Press any key to halt:");  

  6446.    /* clean up */  
  6447.    getch();  
  6448.    closegraph();  
  6449.    return 0;  
  6450. }  
  6451. 函数名: setjmp  
  6452. 功  能: 非局部转移  
  6453. 用  法: int setjmp(jmp_buf env);  
  6454. 程序例:  

  6455. #include <stdio.h>  
  6456. #include <process.h>  
  6457. #include <setjmp.h>  

  6458. void subroutine(void);  

  6459. jmp_buf jumper;  

  6460. int main(void)  
  6461. {  
  6462.    int value;  

  6463.    value = setjmp(jumper);  
  6464.    if (value != 0)  
  6465.    {  
  6466.       printf("Longjmp with value %d\n", value);  
  6467.       exit(value);  
  6468.    }  
  6469.    printf("About to call subroutine ... \n");  
  6470.    subroutine();  
  6471.    return 0;  
  6472. }  

  6473. void subroutine(void)  
  6474. {  
  6475.    longjmp(jumper,1);  
  6476. }  
  6477.    
  6478.    

  6479. 函数名: setlinestyle  
  6480. 功  能: 设置当前画线宽度和类型  
  6481. 用  法: void far setlinestyle(int linestype, unsigned upattern);  
  6482. 程序例:  

  6483. #include <graphics.h>  
  6484. #include <stdlib.h>  
  6485. #include <string.h>  
  6486. #include <stdio.h>  
  6487. #include <conio.h>  

  6488. /* the names of the line styles supported */  
  6489. char *lname[] = {  
  6490.    "SOLID_LINE",  
  6491.    "DOTTED_LINE",  
  6492.    "CENTER_LINE",  
  6493.    "DASHED_LINE",  
  6494.    "USERBIT_LINE"  
  6495.    };  

  6496. int main(void)  
  6497. {  
  6498.    /* request auto detection */  
  6499.    int gdriver = DETECT, gmode, errorcode;  

  6500.    int style, midx, midy, userpat;  
  6501.    char stylestr[40];  

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

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

  6513.    midx = getmaxx() / 2;  
  6514.    midy = getmaxy() / 2;  

  6515.    /* a user defined line pattern */  
  6516.    /* binary: "0000000000000001"  */  
  6517.    userpat = 1;  

  6518.    for (style=SOLID_LINE; style<=USERBIT_LINE; style++)  
  6519.    {  
  6520.       /* select the line style */  
  6521.       setlinestyle(style, userpat, 1);  

  6522.       /* convert style into a string */  
  6523.       strcpy(stylestr, lname[style]);  

  6524.       /* draw a line */  
  6525.       line(0, 0, midx-10, midy);  

  6526.       /* draw a rectangle */  
  6527.       rectangle(0, 0, getmaxx(), getmaxy());  

  6528.       /* output a message */  
  6529.       outtextxy(midx, midy, stylestr);  

  6530.       /* wait for a key */  
  6531.       getch();  
  6532.       cleardevice();  
  6533.    }  

  6534.    /* clean up */  
  6535.    closegraph();  
  6536.    return 0;  
  6537. }  
  6538.    
  6539.    
  6540.    

  6541. 函数名: setmem  
  6542. 功  能: 存值到存储区  
  6543. 用  法: void setmem(void *addr, int len, char value);  
  6544. 程序例:  

  6545. #include <stdio.h>  
  6546. #include <alloc.h>  
  6547. #include <mem.h>  

  6548. int main(void)  
  6549. {  
  6550.    char *dest;  

  6551.    dest = calloc(21, sizeof(char));  
  6552.    setmem(dest, 20, 'c');  
  6553.    printf("%s\n", dest);  

  6554.    return 0;  
  6555. }  
  6556.    
  6557.    
  6558.    

  6559. 函数名: setmode  
  6560. 功  能: 设置打开文件方式  
  6561. 用  法: int setmode(int handle, unsigned mode);  
  6562. 程序例:  

  6563. #include <stdio.h>  
  6564. #include <fcntl.h>  
  6565. #include <io.h>  

  6566. int main(void)  
  6567. {  
  6568.    int result;  

  6569.    result = setmode(fileno(stdprn), O_TEXT);  
  6570.    if (result == -1)  
  6571.       perror("Mode not available\n");  
  6572.    else  
  6573.       printf("Mode successfully switched\n");  
  6574.    return 0;  
  6575. }  
  6576.    
  6577.    
  6578.    

  6579. 函数名: setpalette  
  6580. 功  能: 改变调色板的颜色  
  6581. 用  法: void far setpalette(int index, int actural_color);  
  6582. 程序例:  

  6583. #include <graphics.h>  
  6584. #include <stdlib.h>  
  6585. #include <stdio.h>  
  6586. #include <conio.h>  

  6587. int main(void)  
  6588. {  
  6589.    /* request auto detection */  
  6590.    int gdriver = DETECT, gmode, errorcode;  
  6591.    int color, maxcolor, ht;  
  6592.    int y = 10;  
  6593.    char msg[80];  

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

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

  6605.    maxcolor = getmaxcolor();  
  6606.    ht = 2 * textheight("W");  

  6607.    /* display the default colors */  
  6608.    for (color=1; color<=maxcolor; color++)  
  6609.    {  
  6610.       setcolor(color);  
  6611.       sprintf(msg, "Color: %d", color);  
  6612.       outtextxy(1, y, msg);  
  6613.       y += ht;  
  6614.    }  

  6615.    /* wait for a key */  
  6616.    getch();  

  6617.    /* black out the colors one by one */  
  6618.    for (color=1; color<=maxcolor; color++)  
  6619.    {  
  6620.       setpalette(color, BLACK);  
  6621.       getch();  
  6622.    }  

  6623.    /* clean up */  
  6624.    closegraph();  
  6625.    return 0;  
  6626. }  
  6627.    
  6628.    

  6629. 函数名: setrgbpalette  
  6630. 功  能: 定义IBM8514图形卡的颜色  
  6631. 用  法: void far setrgbpalette(int colornum, int red, int green, int blue);  
  6632. 程序例:  

  6633. #include <graphics.h>  
  6634. #include <stdlib.h>  
  6635. #include <stdio.h>  
  6636. #include <conio.h>  

  6637. int main(void)  
  6638. {  
  6639.    /* select a driver and mode that supports the use */  
  6640.    /* of the setrgbpalette function.                 */  
  6641.    int gdriver = VGA, gmode = VGAHI, errorcode;  
  6642.    struct palettetype pal;  
  6643.    int i, ht, y, xmax;  

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

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

  6655.    /* grab a copy of the palette */  
  6656.    getpalette(&pal);  

  6657.    /* create gray scale */  
  6658.    for (i=0; i<pal.size; i++)  
  6659.       setrgbpalette(pal.colors[i], i*4, i*4, i*4);  

  6660.    /* display the gray scale */  
  6661.    ht = getmaxy() / 16;  
  6662.    xmax = getmaxx();  
  6663.    y = 0;  
  6664.    for (i=0; i<pal.size; i++)  
  6665.    {  
  6666.       setfillstyle(SOLID_FILL, i);  
  6667.       bar(0, y, xmax, y+ht);  
  6668.       y += ht;  
  6669.    }  

  6670.    /* clean up */  
  6671.    getch();  
  6672.    closegraph();  
  6673.    return 0;  
  6674. }  
  6675.    
  6676.    
  6677.    

  6678. 函数名: settextjustify  
  6679. 功  能: 为图形函数设置文本的对齐方式  
  6680. 用  法: void far settextjustify(int horiz, int vert);  
  6681. 程序例:  

  6682. #include <graphics.h>  
  6683. #include <stdlib.h>  
  6684. #include <stdio.h>  
  6685. #include <conio.h>  

  6686. /* function prototype */  
  6687. void xat(int x, int y);  

  6688. /* horizontal text justification settings */  
  6689. char *hjust[] = { "LEFT_TEXT",  
  6690.                   "CENTER_TEXT",  
  6691.                   "RIGHT_TEXT"  
  6692.                 };  

  6693. /* vertical text justification settings */  
  6694. char *vjust[] = { "LEFT_TEXT",  
  6695.     "CENTER_TEXT",  
  6696.     "RIGHT_TEXT"  
  6697.                 };  

  6698. int main(void)  
  6699. {  
  6700.    /* request auto detection */  
  6701.    int gdriver = DETECT, gmode, errorcode;  
  6702.    int midx, midy, hj, vj;  
  6703.    char msg[80];  

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

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

  6715.    midx = getmaxx() / 2;  
  6716.    midy = getmaxy() / 2;  

  6717.    /* loop through text justifications */  
  6718.    for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++)  
  6719.       for (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++)  
  6720.       {  
  6721.          cleardevice();  
  6722.          /* set the text justification */  
  6723.          settextjustify(hj, vj);  

  6724.          /* create a message string */  
  6725.          sprintf(msg, "%s  %s", hjust[hj], vjust[vj]);  

  6726.   /* create cross hairs on the screen */  
  6727.   xat(midx, midy);  

  6728.          /* output the message */  
  6729.          outtextxy(midx, midy, msg);  
  6730.          getch();  
  6731.       }  

  6732.    /* clean up */  
  6733.    closegraph();  
  6734.    return 0;  
  6735. }  

  6736. /* draw an "x" at (x, y) */  
  6737. void xat(int x, int y)  
  6738. {  
  6739.   line(x-4, y, x+4, y);  
  6740.   line(x, y-4, x, y+4);  
  6741. }  
  6742.    
  6743.    

  6744. 函数名: settextstyle  
  6745. 功  能: 为图形输出设置当前的文本属性  
  6746. 用  法: void far settextstyle (int font, int direction, char size);  
  6747. 程序例:  

  6748. #include <graphics.h>  
  6749. #include <stdlib.h>  
  6750. #include <stdio.h>  
  6751. #include <conio.h>  

  6752. /* the names of the text styles supported */  
  6753. char *fname[] = { "DEFAULT font",  
  6754.                   "TRIPLEX font",  
  6755.                   "SMALL font",  
  6756.                   "SANS SERIF font",  
  6757.                   "GOTHIC font"  
  6758.                 };  

  6759. int main(void)  
  6760. {  
  6761.    /* request auto detection */  
  6762.    int gdriver = DETECT, gmode, errorcode;  
  6763.    int style, midx, midy;  
  6764.    int size = 1;  

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

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

  6776.    midx = getmaxx() / 2;  
  6777.    midy = getmaxy() / 2;  

  6778.    settextjustify(CENTER_TEXT, CENTER_TEXT);  

  6779.    /* loop through the available text styles */  
  6780.    for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)  
  6781.    {  
  6782.       cleardevice();  
  6783.       if (style == TRIPLEX_FONT)  
  6784.          size = 4;  

  6785.       /* select the text style */  
  6786.       settextstyle(style, HORIZ_DIR, size);  

  6787.       /* output a message */  
  6788.       outtextxy(midx, midy, fname[style]);  
  6789.       getch();  
  6790.    }  

  6791.    /* clean up */  
  6792.    closegraph();  
  6793.    return 0;  
  6794. }  
  6795.    
  6796.    

  6797. 函数名: settextstyle  
  6798. 功  能: 为图形输出设置当前的文本属性  
  6799. 用  法: void far settextstyle (int font, int direction, char size);  
  6800. 程序例:  

  6801. #include <graphics.h>  
  6802. #include <stdlib.h>  
  6803. #include <stdio.h>  
  6804. #include <conio.h>  

  6805. /* the names of the text styles supported */  
  6806. char *fname[] = { "DEFAULT font",  
  6807.                   "TRIPLEX font",  
  6808.                   "SMALL font",  
  6809.                   "SANS SERIF font",  
  6810.                   "GOTHIC font"  
  6811.                 };  

  6812. int main(void)  
  6813. {  
  6814.    /* request auto detection */  
  6815.    int gdriver = DETECT, gmode, errorcode;  
  6816.    int style, midx, midy;  
  6817.    int size = 1;  

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

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

  6829.    midx = getmaxx() / 2;  
  6830.    midy = getmaxy() / 2;  

  6831.    settextjustify(CENTER_TEXT, CENTER_TEXT);  

  6832.    /* loop through the available text styles */  
  6833.    for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)  
  6834.    {  
  6835.       cleardevice();  
  6836.       if (style == TRIPLEX_FONT)  
  6837.          size = 4;  

  6838.       /* select the text style */  
  6839.       settextstyle(style, HORIZ_DIR, size);  

  6840.       /* output a message */  
  6841.       outtextxy(midx, midy, fname[style]);  
  6842.       getch();  
  6843.    }  

  6844.    /* clean up */  
  6845.    closegraph();  
  6846.    return 0;  
  6847. }  
  6848.    
  6849.    

  6850. 函数名: settime  
  6851. 功  能: 设置系统时间  
  6852. 用  法: void settime(struct time *timep);  
  6853. 程序例:  

  6854. #include <stdio.h>  
  6855. #include <dos.h>  

  6856. int main(void)  
  6857. {  
  6858.    struct  time t;  

  6859.    gettime(&t);  
  6860.    printf("The current minute is: %d\n", t.ti_min);  
  6861.    printf("The current hour is: %d\n", t.ti_hour);  
  6862.    printf("The current hundredth of a second is: %d\n", t.ti_hund);  
  6863.    printf("The current second is: %d\n", t.ti_sec);  

  6864.    /* Add one to the minutes struct element and then call settime  */  
  6865.    t.ti_min++;  
  6866.    settime(&t);  

  6867.    return 0;  
  6868. }  
  6869.    
  6870.    

  6871. 函数名: setusercharsize  
  6872. 功  能: 为矢量字体改变字符宽度和高度  
  6873. 用  法: void far setusercharsize(int multx, int dirx, int multy, int diry);  
  6874. 程序例:  

  6875. #include <graphics.h>  
  6876. #include <stdlib.h>  
  6877. #include <stdio.h>  
  6878. #include <conio.h>  

  6879. int main(void)  
  6880. {  
  6881.    /* request autodetection */  
  6882.    int gdriver = DETECT, gmode, errorcode;  

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

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

  6894.    /* select a text style */  
  6895.    settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);  

  6896.    /* move to the text starting position */  
  6897.    moveto(0, getmaxy() / 2);  

  6898.    /* output some normal text */  
  6899.    outtext("Norm ");  

  6900.    /* make the text 1/3 the normal width */  
  6901.    setusercharsize(1, 3, 1, 1);  
  6902.    outtext("Short ");  

  6903.    /* make the text 3 times normal width */  
  6904.    setusercharsize(3, 1, 1, 1);  
  6905.    outtext("Wide");  

  6906.    /* clean up */  
  6907.    getch();  
  6908.    closegraph();  
  6909.    return 0;  
  6910. }  
  6911.    

  6912. 函数名: setvbuf  
  6913. 功  能: 把缓冲区与流相关  
  6914. 用  法: int setvbuf(FILE *stream, char *buf, int type, unsigned size);  
  6915. 程序例:  

  6916. #include <stdio.h>  

  6917. int main(void)  
  6918. {  
  6919.    FILE *input, *output;  
  6920.    char bufr[512];  

  6921.    input = fopen("file.in", "r+b");  
  6922.    output = fopen("file.out", "w");  

  6923.    /* set up input stream for minimal disk access,  
  6924.       using our own character buffer */  
  6925.    if (setvbuf(input, bufr, _IOFBF, 512) != 0)  
  6926.       printf("failed to set up buffer for input file\n");  
  6927.    else  
  6928.       printf("buffer set up for input file\n");  

  6929.    /* set up output stream for line buffering using space that  
  6930.       will be obtained through an indirect call to malloc */  
  6931.    if (setvbuf(output, NULL, _IOLBF, 132) != 0)  
  6932.       printf("failed to set up buffer for output file\n");  
  6933.    else  
  6934.       printf("buffer set up for output file\n");  

  6935.    /* perform file I/O here */  

  6936.    /* close files */  
  6937.    fclose(input);  
  6938.    fclose(output);  
  6939.    return 0;  
  6940. }  
  6941.    
  6942.    
  6943.    

  6944. 函数名: setvect  
  6945. 功  能: 设置中断矢量入口  
  6946. 用  法: void setvect(int intr_num, void interrupt(*isr)());  
  6947. 程序例:  

  6948. /***NOTE:  
  6949.     This is an interrupt service routine.  You can NOT compile this  
  6950.     program with Test Stack Overflow turned on and get an executable  
  6951.     file which will operate correctly. */  

  6952. #include <stdio.h>  
  6953. #include <dos.h>  
  6954. #include <conio.h>  

  6955. #define INTR 0X1C    /* The clock tick interrupt */  

  6956. void interrupt ( *oldhandler)(void);  

  6957. int count=0;  

  6958. void interrupt handler(void)  
  6959. {  
  6960. /* increase the global counter */  
  6961.    count++;  

  6962. /* call the old routine */  
  6963.    oldhandler();  
  6964. }  

  6965. int main(void)  
  6966. {  
  6967. /* save the old interrupt vector */  
  6968.    oldhandler = getvect(INTR);  

  6969. /* install the new interrupt handler */  
  6970.    setvect(INTR, handler);  

  6971. /* loop until the counter exceeds 20 */  
  6972.    while (count < 20)  
  6973.       printf("count is %d\n",count);  

  6974. /* reset the old interrupt handler */  
  6975.    setvect(INTR, oldhandler);  

  6976.    return 0;  
  6977. }  
  6978.    
  6979.    

  6980. 函数名: setverify  
  6981. 功  能: 设置验证状态  
  6982. 用  法: void setverify(int value);  
  6983. 程序例:  

  6984. #include <stdio.h>  
  6985. #include <conio.h>  
  6986. #include <dos.h>  

  6987. int main(void)  
  6988. {  
  6989.    int verify_flag;  

  6990.    printf("Enter 0 to set verify flag off\n");  
  6991.    printf("Enter 1 to set verify flag on\n");  

  6992.    verify_flag = getch() - 0;  

  6993.    setverify(verify_flag);  

  6994.    if (getverify())  
  6995.       printf("DOS verify flag is on\n");  
  6996.    else  
  6997.       printf("DOS verify flag is off\n");  

  6998.    return 0;  
  6999. }  
  7000.    
  7001.    

  7002. 函数名: setviewport  
  7003. 功  能: 为图形输出设置当前视口  
  7004. 用  法: void far setviewport(int left, int top, int right,  
  7005.         int bottom, int clipflag);  
  7006. 程序例:  

  7007. #include <graphics.h>  
  7008. #include <stdlib.h>  
  7009. #include <stdio.h>  
  7010. #include <conio.h>  

  7011. #define CLIP_ON 1   /* activates clipping in viewport */  

  7012. int main(void)  
  7013. {  
  7014.    /* request auto detection */  
  7015.    int gdriver = DETECT, gmode, errorcode;  

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

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

  7027.    setcolor(getmaxcolor());  

  7028.    /* message in default full-screen viewport */  
  7029.    outtextxy(0, 0, "* <-- (0, 0) in default viewport");  

  7030.    /* create a smaller viewport */  
  7031.    setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);  

  7032.    /* display some text */  
  7033.    outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");  

  7034.    /* clean up */  
  7035.    getch();  
  7036.    closegraph();  
  7037.    return 0;  
  7038. }  
  7039.    
  7040.    

  7041. 函数名: setvisualpage  
  7042. 功  能: 设置可见图形页号  
  7043. 用  法: void far setvisualpage(int pagenum);  
  7044. 程序例:  

  7045. #include <graphics.h>  
  7046. #include <stdlib.h>  
  7047. #include <stdio.h>  
  7048. #include <conio.h>  

  7049. int main(void)  
  7050. {  
  7051.    /* select a driver and mode that supports */  
  7052.    /* multiple pages.                        */  
  7053.    int gdriver = EGA, gmode = EGAHI, errorcode;  
  7054.    int x, y, ht;  

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

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

  7066.    x = getmaxx() / 2;  
  7067.    y = getmaxy() / 2;  
  7068.    ht = textheight("W");  

  7069.    /*  select the off screen page for drawing */  
  7070.    setactivepage(1);  

  7071.    /* draw a line on page #1 */  
  7072.    line(0, 0, getmaxx(), getmaxy());  

  7073.    /* output a message on page #1 */  
  7074.    settextjustify(CENTER_TEXT, CENTER_TEXT);  
  7075.    outtextxy(x, y, "This is page #1:");  
  7076.    outtextxy(x, y+ht, "Press any key to halt:");  

  7077.    /* select drawing to page #0 */  
  7078.    setactivepage(0);  

  7079.    /* output a message  on page #0 */  
  7080.    outtextxy(x, y, "This is page #0.");  
  7081.    outtextxy(x, y+ht, "Press any key to view page #1:");  
  7082.    getch();  

  7083.    /* select page #1 as the visible page */  
  7084.    setvisualpage(1);  

  7085.    /* clean up */  
  7086.    getch();  
  7087.    closegraph();  
  7088.    return 0;  
  7089. }  
  7090.    
  7091.    

  7092. 函数名: setwritemode  
  7093. 功  能: 设置图形方式下画线的输出模式  
  7094. 用  法: void far setwritemode(int mode);  
  7095. 程序例:  

  7096. #include <graphics.h>  
  7097. #include <stdlib.h>  
  7098. #include <stdio.h>  
  7099. #include <conio.h>  

  7100. int main()  
  7101. {  
  7102.    /* request auto detection */  
  7103.    int gdriver = DETECT, gmode, errorcode;  
  7104.    int xmax, ymax;  

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

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

  7116.    xmax = getmaxx();  
  7117.    ymax = getmaxy();  

  7118.    /* select XOR drawing mode */  
  7119.    setwritemode(XOR_PUT);  

  7120.    /* draw a line */  
  7121.    line(0, 0, xmax, ymax);  
  7122.    getch();  

  7123.    /* erase the line by drawing over it */  
  7124.    line(0, 0, xmax, ymax);  
  7125.    getch();  

  7126.    /* select overwrite drawing mode */  
  7127.    setwritemode(COPY_PUT);  

  7128.    /* draw a line */  
  7129.    line(0, 0, xmax, ymax);  

  7130.    /* clean up */  
  7131.    getch();  
  7132.    closegraph();  
  7133.    return 0;  
  7134. }  
  7135.    
  7136.    

  7137. 函数名: signal  
  7138. 功  能: 设置某一信号的对应动作  
  7139. 用  法: int signal(int sig, sigfun fname);  
  7140. 程序例:  

  7141. /* This example installs a signal handler routine for SIGFPE,  
  7142.    catches an integer overflow condition, makes an adjustment  
  7143.    to AX register, and returns. This example program MAY cause  
  7144.    your computer to crash, and will produce runtime errors  
  7145.    depending on which memory model is used.  
  7146. */  

  7147. #pragma inline  
  7148. #include <stdio.h>  
  7149. #include <signal.h>  

  7150. void Catcher(int sig, int type, int *reglist)  
  7151. {  
  7152.    printf("Caught it!\n");  
  7153.    *(reglist + 8) = 3;             /* make return AX = 3 */  
  7154. }  

  7155. int main(void)  
  7156. {  
  7157.    signal(SIGFPE, Catcher);  
  7158.    asm     mov     ax,07FFFH       /* AX = 32767 */  
  7159.    asm     inc     ax              /* cause overflow */  
  7160.    asm     into                    /* activate handler */  

  7161.    /* The handler set AX to 3 on return. If that hadn't happened,  
  7162.       there would have been another exception when the next 'into'  
  7163.       was executed after the 'dec' instruction. */  
  7164.    asm     dec     ax              /* no overflow now */  
  7165.    asm     into                    /* doesn't activate */  
  7166.    return 0;  
  7167. }  
  7168.    
  7169.    
  7170.    

  7171. 函数名: sin  
  7172. 功  能: 正弦函数  
  7173. 用  法: double sin(double x);  
  7174. 程序例:  

  7175. #include <stdio.h>  
  7176. #include <math.h>  

  7177. int main(void)  
  7178. {  
  7179.    double result, x = 0.5;  

  7180.    result = sin(x);  
  7181.    printf("The sin() of %lf is %lf\n", x, result);  
  7182.    return 0;  
  7183. }  
  7184.    
  7185.    

  7186. 函数名: sinh  
  7187. 功  能: 双曲正弦函数  
  7188. 用  法: double sinh(double x);  
  7189. 程序例:  

  7190. #include <stdio.h>  
  7191. #include <math.h>  

  7192. int main(void)  
  7193. {  
  7194.    double result, x = 0.5;  

  7195.    result = sinh(x);  
  7196.    printf("The hyperbolic sin() of %lf is %lf\n", x, result);  
  7197.    return 0;  
  7198. }  
  7199.    
  7200.    
  7201.    

  7202. 函数名: sleep  
  7203. 功  能: 执行挂起一段时间  
  7204. 用  法: unsigned sleep(unsigned seconds);  
  7205. 程序例:  

  7206. #include <dos.h>  
  7207. #include <stdio.h>  

  7208. int main(void)  
  7209. {  
  7210.    int i;  

  7211.    for (i=1; i<5; i++)  
  7212.    {  
  7213.       printf("Sleeping for %d seconds\n", i);  
  7214.       sleep(i);  
  7215.    }  
  7216.    return 0;  
  7217. }  
  7218.    
  7219.    
  7220.    

  7221. 函数名: sopen  
  7222. 功  能: 打开一共享文件  
  7223. 用  法: int sopen(char *pathname, int access, int shflag, int permiss);  
  7224. 程序例:  

  7225. #include <io.h>  
  7226. #include <fcntl.h>  
  7227. #include <sys\stat.h>  
  7228. #include <process.h>  
  7229. #include <share.h>  
  7230. #include <stdio.h>  

  7231. int main(void)  
  7232. {  
  7233.    int handle;  
  7234.    int status;  

  7235.    handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYNO, S_IREAD);  

  7236.    if (!handle)  
  7237.    {  
  7238.       printf("sopen failed\n");  
  7239.       exit(1);  
  7240.    }  

  7241.    status = access("c:\\autoexec.bat", 6);  
  7242.    if (status == 0)  
  7243.       printf("read/write access allowed\n");  
  7244.    else  
  7245.       printf("read/write access not allowed\n");  

  7246.    close(handle);  
  7247.    return 0;  
  7248. }  函数名: sound  
  7249. 功  能: 以指定频率打开PC扬声器  
  7250. 用  法: void sound(unsigned frequency);  
  7251. 程序例:  

  7252. /* Emits a 7-Hz tone for 10 seconds.  
  7253.    Your PC may not be able to emit a 7-Hz tone. */  
  7254. #include <dos.h>  

  7255. int main(void)  
  7256. {  
  7257.    sound(7);  
  7258.    delay(10000);  
  7259.    nosound();  
  7260.    return 0;  
  7261. }  
  7262.    
  7263.    
  7264.    

  7265. 函数名: spawnl  
  7266. 功  能: 创建并运行子程序  
  7267. 用  法: int spawnl(int mode, char *pathname, char *arg0,  
  7268.      arg1, ... argn, NULL);  
  7269. 程序例:  

  7270. #include <process.h>  
  7271. #include <stdio.h>  
  7272. #include <conio.h>  

  7273. int main(void)  
  7274. {  
  7275.    int result;  

  7276.    clrscr();  
  7277.    result = spawnl(P_WAIT, "tcc.exe", NULL);  
  7278.    if (result == -1)  
  7279.    {  
  7280.       perror("Error from spawnl");  
  7281.       exit(1);  
  7282.    }  
  7283.    return 0;  
  7284. }  
  7285.    
  7286.    

  7287. 函数名: spawnle  
  7288. 功  能: 创建并运行子程序  
  7289. 用  法: int spawnle(int mode, char *pathname, char *arg0,  
  7290.       arg1,..., argn, NULL);  
  7291. 程序例:  

  7292. /* spawnle() example */  

  7293. #include <process.h>  
  7294. #include <stdio.h>  
  7295. #include <conio.h>  

  7296. int main(void)  
  7297. {  
  7298.    int result;  

  7299.    clrscr();  
  7300.    result = spawnle(P_WAIT, "tcc.exe", NULL, NULL);  
  7301.    if (result == -1)  
  7302.    {  
  7303.       perror("Error from spawnle");  
  7304.       exit(1);  
  7305.    }  
  7306.    return 0;  
  7307. }  
  7308.    
  7309.    
  7310.    

  7311. 函数名: sprintf  
  7312. 功  能: 送格式化输出到字符串中  
  7313. 用  法: int sprintf(char *string, char *farmat [,argument,...]);  
  7314. 程序例:  

  7315. #include <stdio.h>  
  7316. #include <math.h>  

  7317. int main(void)  
  7318. {  
  7319.    char buffer[80];  

  7320.    sprintf(buffer, "An approximation of Pi is %f\n", M_PI);  
  7321.    puts(buffer);  
  7322.    return 0;  
  7323. }  
  7324.    
  7325.    

  7326. 函数名: sqrt  
  7327. 功  能: 计算平方根  
  7328. 用  法: double sqrt(double x);  
  7329. 程序例:  

  7330. #include <math.h>  
  7331. #include <stdio.h>  

  7332. int main(void)  
  7333. {  
  7334.     double x = 4.0, result;  

  7335.     result = sqrt(x);  
  7336.     printf("The square root of %lf is %lf\n", x, result);  
  7337.     return 0;  
  7338. }  
  7339.    

  7340. 函数名: srand  
  7341. 功  能: 初始化随机数发生器  
  7342. 用  法: void srand(unsigned seed);  
  7343. 程序例:  

  7344. #include <stdlib.h>  
  7345. #include <stdio.h>  
  7346. #include <time.h>  

  7347. int main(void)  
  7348. {  
  7349.    int i;  
  7350.    time_t t;  

  7351.    srand((unsigned) time(&t));  
  7352.    printf("Ten random numbers from 0 to 99\n\n");  
  7353.    for(i=0; i<10; i++)  
  7354.        printf("%d\n", rand() % 100);  
  7355.    return 0;  
  7356. }  
  7357.    
  7358.    

  7359. 函数名: sscanf  
  7360. 功  能: 执行从字符串中的格式化输入  
  7361. 用  法: int sscanf(char *string, char *format[,argument,...]);  
  7362. 程序例:  

  7363. #include <stdio.h>  
  7364. #include <conio.h>  

  7365. int main(void)  
  7366. {  
  7367.    char label[20];  
  7368.    char name[20];  
  7369.    int entries = 0;  
  7370.    int loop, age;  
  7371.    double salary;  

  7372.    struct Entry_struct  
  7373.    {  
  7374.       char  name[20];  
  7375.       int   age;  
  7376.       float salary;  
  7377.    } entry[20];  

  7378. /* Input a label as a string of characters restricting to 20 characters */  
  7379.    printf("\n\nPlease enter a label for the chart: ");  
  7380.    scanf("%20s", label);  
  7381.    fflush(stdin);  /* flush the input stream in case of bad input */  

  7382. /* Input number of entries as an integer */  
  7383.    printf("How many entries will there be? (less than 20) ");  
  7384.    scanf("%d", &entries);  
  7385.    fflush(stdin);   /* flush the input stream in case of bad input */  

  7386. /* input a name restricting input to only letters upper or lower case */  
  7387.    for (loop=0;loop<entries;++loop)  
  7388.    {  
  7389.       printf("Entry %d\n", loop);  
  7390.       printf("  Name   : ");  
  7391.       scanf("%[A-Za-z]", entry[loop].name);  
  7392.       fflush(stdin);  /* flush the input stream in case of bad input */  

  7393. /* input an age as an integer */  
  7394.       printf("  Age    : ");  
  7395.       scanf("%d", &entry[loop].age);  
  7396.       fflush(stdin);  /* flush the input stream in case of bad input */  

  7397. /* input a salary as a float */  
  7398.       printf("  Salary : ");  
  7399.       scanf("%f", &entry[loop].salary);  
  7400.       fflush(stdin); /* flush the input stream in case of bad input */  
  7401.    }  

  7402. /* Input a name, age and salary as a string, integer, and double */  
  7403.    printf("\nPlease enter your name, age and salary\n");  
  7404.    scanf("%20s %d %lf", name, &age, &salary);  
  7405.    

  7406. /* Print out the data that was input */  
  7407.    printf("\n\nTable %s\n",label);  
  7408.    printf("Compiled by %s  age %d  $%15.2lf\n", name, age, salary);  
  7409.    printf("-----------------------------------------------------\n");  
  7410.    for (loop=0;loop<entries;++loop)  
  7411.       printf("%4d | %-20s | %5d | %15.2lf\n",  
  7412.          loop + 1,  
  7413.   entry[loop].name,  
  7414.   entry[loop].age,  
  7415.   entry[loop].salary);  
  7416.    printf("-----------------------------------------------------\n");  
  7417.    return 0;  
  7418. }  
  7419.    
  7420.    

  7421. 函数名: stat  
  7422. 功  能: 读取打开文件信息  
  7423. 用  法: int stat(char *pathname, struct stat *buff);  
  7424. 程序例:  

  7425. #include <sys\stat.h>  
  7426. #include <stdio.h>  
  7427. #include <time.h>  

  7428. #define FILENAME "TEST.$"  

  7429. int main(void)  
  7430. {  
  7431.    struct stat statbuf;  
  7432.    FILE *stream;  

  7433.    /* open a file for update */  
  7434.    if ((stream = fopen(FILENAME, "w+")) == NULL)  
  7435.    {  
  7436.       fprintf(stderr, "Cannot open output file.\n");  
  7437.       return(1);  
  7438.    }  

  7439.    /* get information about the file */  
  7440.    stat(FILENAME, &statbuf);  

  7441.    fclose(stream);  

  7442.    /* display the information returned */  
  7443.    if (statbuf.st_mode & S_IFCHR)  
  7444.       printf("Handle refers to a device.\n");  
  7445.    if (statbuf.st_mode & S_IFREG)  
  7446.       printf("Handle refers to an ordinary file.\n");  
  7447.    if (statbuf.st_mode & S_IREAD)  
  7448.       printf("User has read permission on file.\n");  
  7449.    if (statbuf.st_mode & S_IWRITE)  
  7450.       printf("User has write permission on file.\n");  

  7451.    printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);  
  7452.    printf("Size of file in bytes: %ld\n", statbuf.st_size);  
  7453.    printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));  
  7454.    return 0;  
  7455. }  
  7456.    
  7457.    
  7458.    

  7459. 函数名: _status87  
  7460. 功  能: 取浮点状态  
  7461. 用  法: unsigned int _status87(void);  
  7462. 程序例:  

  7463. #include <stdio.h>  
  7464. #include <float.h>  

  7465. int main(void)  
  7466. {  
  7467.    float x;  
  7468.    double y = 1.5e-100;  

  7469.    printf("Status 87 before error: %x\n", _status87());  

  7470.    x = y;  /* <-- force an error to occur */  
  7471.    y = x;  

  7472.    printf("Status 87 after error : %x\n", _status87());  
  7473.    return 0;  
  7474. }  
  7475.    
  7476.    

  7477. 函数名: stime  
  7478. 功  能: 设置时间  
  7479. 用  法: int stime(long *tp);  
  7480. 程序例:  

  7481. #include <stdio.h>  
  7482. #include <time.h>  
  7483. #include <dos.h>  

  7484. int main(void)  
  7485. {  
  7486.    time_t t;  
  7487.    struct tm *area;  

  7488.    t = time(NULL);  
  7489.    area = localtime(&t);  
  7490.    printf("Number of seconds since 1/1/1970 is: %ld\n", t);  
  7491.    printf("Local time is: %s", asctime(area));  

  7492.    t++;  
  7493.    area = localtime(&t);  
  7494.    printf("Add a second:  %s", asctime(area));  

  7495.    t += 60;  
  7496.    area = localtime(&t);  
  7497.    printf("Add a minute:  %s", asctime(area));  

  7498.    t += 3600;  
  7499.    area = localtime(&t);  
  7500.    printf("Add an hour:   %s", asctime(area));  

  7501.    t += 86400L;  
  7502.    area = localtime(&t);  
  7503.    printf("Add a day:     %s", asctime(area));  

  7504.    t += 2592000L;  
  7505.    area = localtime(&t);  
  7506.    printf("Add a month:   %s", asctime(area));  

  7507.    t += 31536000L;  
  7508.    area = localtime(&t);  
  7509.    printf("Add a year:    %s", asctime(area));  
  7510.    return 0;  
  7511. }  
  7512.    
  7513.    
  7514.    

  7515. 函数名: stpcpy  
  7516. 功  能: 拷贝一个字符串到另一个  
  7517. 用  法: char *stpcpy(char *destin, char *source);  
  7518. 程序例:  

  7519. #include <stdio.h>  
  7520. #include <string.h>  

  7521. int main(void)  
  7522. {  
  7523.    char string[10];  
  7524.    char *str1 = "abcdefghi";  

  7525.    stpcpy(string, str1);  
  7526.    printf("%s\n", string);  
  7527.    return 0;  
  7528. }  
  7529.    
  7530.    
  7531.    

  7532. 函数名: strcat  
  7533. 功  能: 字符串拼接函数  
  7534. 用  法: char *strcat(char *destin, char *source);  
  7535. 程序例:  

  7536. #include <string.h>  
  7537. #include <stdio.h>  

  7538. int main(void)  
  7539. {  
  7540.    char destination[25];  
  7541.    char *blank = " ", *c = "C++", *Borland = "Borland";  

  7542.    strcpy(destination, Borland);  
  7543.    strcat(destination, blank);  
  7544.    strcat(destination, c);  

  7545.    printf("%s\n", destination);  
  7546.    return 0;  
  7547. }  
  7548.    
  7549.    
  7550.    

  7551. 函数名: strchr  
  7552. 功  能: 在一个串中查找给定字符的第一个匹配之处\  
  7553. 用  法: char *strchr(char *str, char c);  
  7554. 程序例:  

  7555. #include <string.h>  
  7556. #include <stdio.h>  

  7557. int main(void)  
  7558. {  
  7559.     char string[15];  
  7560.     char *ptr, c = 'r';  

  7561.     strcpy(string, "This is a string");  
  7562.     ptr = strchr(string, c);  
  7563.     if (ptr)  
  7564.        printf("The character %c is at position: %d\n", c, ptr-string);  
  7565.     else  
  7566.        printf("The character was not found\n");  
  7567.     return 0;  
  7568. }  
  7569.    
  7570.    
  7571.    

  7572. 函数名: strcmp  
  7573. 功  能: 串比较  
  7574. 用  法: int strcmp(char *str1, char *str2);  
  7575. 程序例:  

  7576. #include <string.h>  
  7577. #include <stdio.h>  

  7578. int main(void)  
  7579. {  
  7580.     char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";  
  7581.     int ptr;  

  7582.     ptr = strcmp(buf2, buf1);  
  7583.     if (ptr > 0)  
  7584.        printf("buffer 2 is greater than buffer 1\n");  
  7585.     else  
  7586.        printf("buffer 2 is less than buffer 1\n");  

  7587.     ptr = strcmp(buf2, buf3);  
  7588.     if (ptr > 0)  
  7589.        printf("buffer 2 is greater than buffer 3\n");  
  7590.     else  
  7591.        printf("buffer 2 is less than buffer 3\n");  

  7592.     return 0;  
  7593. }  
  7594.    
  7595.    
  7596.    

  7597. 函数名: strncmpi  
  7598. 功  能: 将一个串中的一部分与另一个串比较, 不管大小写  
  7599. 用  法: int strncmpi(char *str1, char *str2, unsigned maxlen);  
  7600. 程序例:  

  7601. #include <string.h>  
  7602. #include <stdio.h>  

  7603. int main(void)  
  7604. {  
  7605.    char *buf1 = "BBB", *buf2 = "bbb";  
  7606.    int ptr;  

  7607.    ptr = strcmpi(buf2, buf1);  

  7608.    if (ptr > 0)  
  7609.       printf("buffer 2 is greater than buffer 1\n");  

  7610.    if (ptr < 0)  
  7611.       printf("buffer 2 is less than buffer 1\n");  

  7612.    if (ptr == 0)  
  7613.       printf("buffer 2 equals buffer 1\n");  

  7614.    return 0;  
  7615. }  
  7616.    
  7617.    
  7618.    

  7619. 函数名: strcpy  
  7620. 功  能: 串拷贝  
  7621. 用  法: char *strcpy(char *str1, char *str2);  
  7622. 程序例:  

  7623. #include <stdio.h>  
  7624. #include <string.h>  

  7625. int main(void)  
  7626. {  
  7627.     char string[10];  
  7628.     char *str1 = "abcdefghi";  

  7629.     strcpy(string, str1);  
  7630.     printf("%s\n", string);  
  7631.     return 0;  
  7632. }  
  7633.    
  7634.    
  7635.    

  7636. 函数名: strcspn  
  7637. 功  能: 在串中查找第一个给定字符集内容的段  
  7638. 用  法: int strcspn(char *str1, char *str2);  
  7639. 程序例:  

  7640. #include <stdio.h>  
  7641. #include <string.h>  
  7642. #include <alloc.h>  

  7643. int main(void)  
  7644. {  
  7645.     char *string1 = "1234567890";  
  7646.     char *string2 = "747DC8";  
  7647.     int length;  

  7648.     length = strcspn(string1, string2);  
  7649.     printf("Character where strings intersect is at position %d\n", length);  

  7650.     return 0;  
  7651. }  
  7652.    
  7653.    
  7654.    

  7655. 函数名: strdup  
  7656. 功  能: 将串拷贝到新建的位置处  
  7657. 用  法: char *strdup(char *str);  
  7658. 程序例:  

  7659. #include <stdio.h>  
  7660. #include <string.h>  
  7661. #include <alloc.h>  

  7662. int main(void)  
  7663. {  
  7664.     char *dup_str, *string = "abcde";  

  7665.     dup_str = strdup(string);  
  7666.     printf("%s\n", dup_str);  
  7667.     free(dup_str);  

  7668.     return 0;  
  7669. }  
  7670.    
  7671.    
  7672.    

  7673. 函数名: stricmp  
  7674. 功  能: 以大小写不敏感方式比较两个串  
  7675. 用  法: int stricmp(char *str1, char *str2);  
  7676. 程序例:  

  7677. #include <string.h>  
  7678. #include <stdio.h>  

  7679. int main(void)  
  7680. {  
  7681.    char *buf1 = "BBB", *buf2 = "bbb";  
  7682.    int ptr;  

  7683.    ptr = stricmp(buf2, buf1);  

  7684.    if (ptr > 0)  
  7685.       printf("buffer 2 is greater than buffer 1\n");  

  7686.    if (ptr < 0)  
  7687.       printf("buffer 2 is less than buffer 1\n");  

  7688.    if (ptr == 0)  
  7689.       printf("buffer 2 equals buffer 1\n");  

  7690.    return 0;  
  7691. }  
  7692.    
  7693.    

  7694. 函数名: strerror  
  7695. 功  能: 返回指向错误信息字符串的指针  
  7696. 用  法: char *strerror(int errnum);  
  7697. 程序例:  

  7698. #include <stdio.h>  
  7699. #include <errno.h>  

  7700. int main(void)  
  7701. {  
  7702.    char *buffer;  
  7703.    buffer = strerror(errno);  
  7704.    printf("Error: %s\n", buffer);  
  7705.    return 0;  
  7706. }  
  7707.    
  7708.    
  7709.    

  7710. 函数名: strcmpi  
  7711. 功  能: 将一个串与另一个比较, 不管大小写  
  7712. 用  法: int strcmpi(char *str1, char *str2);  
  7713. 程序例:  

  7714. #include <string.h>  
  7715. #include <stdio.h>  

  7716. int main(void)  
  7717. {  
  7718.    char *buf1 = "BBB", *buf2 = "bbb";  
  7719.    int ptr;  

  7720.    ptr = strcmpi(buf2, buf1);  

  7721.    if (ptr > 0)  
  7722.       printf("buffer 2 is greater than buffer 1\n");  

  7723.    if (ptr < 0)  
  7724.       printf("buffer 2 is less than buffer 1\n");  

  7725.    if (ptr == 0)  
  7726.       printf("buffer 2 equals buffer 1\n");  

  7727.    return 0;  
  7728. }  
  7729.    
  7730.    
  7731.    

  7732. 函数名: strncmp  
  7733. 功  能: 串比较  
  7734. 用  法: int strncmp(char *str1, char *str2, int maxlen);  
  7735. 程序例:  

  7736. #include <string.h>  
  7737. #include <stdio.h>  

  7738. int  main(void)  

  7739. {  
  7740.    char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";  
  7741.    int ptr;  

  7742.    ptr = strncmp(buf2,buf1,3);  
  7743.    if (ptr > 0)  
  7744.       printf("buffer 2 is greater than buffer 1\n");  
  7745.    else  
  7746.       printf("buffer 2 is less than buffer 1\n");  

  7747.    ptr = strncmp(buf2,buf3,3);  
  7748.    if (ptr > 0)  
  7749.       printf("buffer 2 is greater than buffer 3\n");  
  7750.    else  
  7751.       printf("buffer 2 is less than buffer 3\n");  

  7752.    return(0);  
  7753. }  
  7754.    
  7755.    

  7756. 函数名: strncmpi  
  7757. 功  能: 把串中的一部分与另一串中的一部分比较, 不管大小写  
  7758. 用  法: int strncmpi(char *str1, char *str2);  
  7759. 程序例:  

  7760. #include <string.h>  
  7761. #include <stdio.h>  

  7762. int main(void)  
  7763. {  
  7764.    char *buf1 = "BBBccc", *buf2 = "bbbccc";  
  7765.    int ptr;  

  7766.    ptr = strncmpi(buf2,buf1,3);  

  7767.    if (ptr > 0)  
  7768.       printf("buffer 2 is greater than buffer 1\n");  

  7769.    if (ptr < 0)  
  7770.       printf("buffer 2 is less than buffer 1\n");  

  7771.    if (ptr == 0)  
  7772.       printf("buffer 2 equals buffer 1\n");  

  7773.    return 0;  
  7774. }  
  7775.    
  7776.    

  7777. 函数名: strncpy  
  7778. 功  能: 串拷贝  
  7779. 用  法: char *strncpy(char *destin, char *source, int maxlen);  
  7780. 程序例:  

  7781. #include <stdio.h>  
  7782. #include <string.h>  

  7783. int main(void)  
  7784. {  
  7785.    char string[10];  
  7786.    char *str1 = "abcdefghi";  

  7787.    strncpy(string, str1, 3);  
  7788.    string[3] = '\0';  
  7789.    printf("%s\n", string);  
  7790.    return 0;  
  7791. }  
  7792.    
  7793.    

  7794. 函数名: strnicmp  
  7795. 功  能: 不注重大小写地比较两个串  
  7796. 用  法: int strnicmp(char *str1, char *str2, unsigned maxlen);  
  7797. 程序例:  

  7798. #include <string.h>  
  7799. #include <stdio.h>  

  7800. int main(void)  
  7801. {  
  7802.    char *buf1 = "BBBccc", *buf2 = "bbbccc";  
  7803.    int ptr;  

  7804.    ptr = strnicmp(buf2, buf1, 3);  

  7805.    if (ptr > 0)  
  7806.       printf("buffer 2 is greater than buffer 1\n");  

  7807.    if (ptr < 0)  
  7808.       printf("buffer 2 is less than buffer 1\n");  

  7809.    if (ptr == 0)  
  7810.       printf("buffer 2 equals buffer 1\n");  

  7811.    return 0;  
  7812. }  
  7813.    
  7814.    
  7815.    

  7816. 函数名: strnset  
  7817. 功  能: 将一个串中的所有字符都设为指定字符  
  7818. 用  法: char *strnset(char *str, char ch, unsigned n);  
  7819. 程序例:  

  7820. #include <stdio.h>  
  7821. #include <string.h>  

  7822. int main(void)  
  7823. {  
  7824.    char *string = "abcdefghijklmnopqrstuvwxyz";  
  7825.    char letter = 'x';  

  7826.    printf("string before strnset: %s\n", string);  
  7827.    strnset(string, letter, 13);  
  7828.    printf("string after  strnset: %s\n", string);  

  7829.    return 0;  
  7830. }  
  7831.    
  7832.    

  7833. 函数名: strpbrk  
  7834. 功  能: 在串中查找给定字符集中的字符  
  7835. 用  法: char *strpbrk(char *str1, char *str2);  
  7836. 程序例:  

  7837. #include <stdio.h>  
  7838. #include <string.h>  

  7839. int main(void)  
  7840. {  
  7841.    char *string1 = "abcdefghijklmnopqrstuvwxyz";  
  7842.    char *string2 = "onm";  
  7843.    char *ptr;  

  7844.    ptr = strpbrk(string1, string2);  

  7845.    if (ptr)  
  7846.       printf("strpbrk found first character: %c\n", *ptr);  
  7847.    else  
  7848.       printf("strpbrk didn't find character in set\n");  

  7849.    return 0;  
  7850. }  
  7851.    
  7852.    
  7853.    

  7854. 函数名: strrchr  
  7855. 功  能: 在串中查找指定字符的最后一个出现  
  7856. 用  法: char *strrchr(char *str, char c);  
  7857. 程序例:  

  7858. #include <string.h>  
  7859. #include <stdio.h>  

  7860. int main(void)  
  7861. {  
  7862.    char string[15];  
  7863.    char *ptr, c = 'r';  

  7864.    strcpy(string, "This is a string");  
  7865.    ptr = strrchr(string, c);  
  7866.    if (ptr)  
  7867.       printf("The character %c is at position: %d\n", c, ptr-string);  
  7868.    else  
  7869.       printf("The character was not found\n");  
  7870.    return 0;  
  7871. }  
  7872.    
  7873.    
  7874.    

  7875. 函数名: strrev  
  7876. 功  能: 串倒转  
  7877. 用  法: char *strrev(char *str);  
  7878. 程序例:  

  7879. #include <string.h>  
  7880. #include <stdio.h>  

  7881. int main(void)  
  7882. {  
  7883.    char *forward = "string";  

  7884.    printf("Before strrev(): %s\n", forward);  
  7885.    strrev(forward);  
  7886.    printf("After strrev():  %s\n", forward);  
  7887.    return 0;  
  7888. }  
  7889.    

  7890. 函数名: strset  
  7891. 功  能: 将一个串中的所有字符都设为指定字符  
  7892. 用  法: char *strset(char *str, char c);  
  7893. 程序例:  

  7894. #include <stdio.h>  
  7895. #include <string.h>  

  7896. int main(void)  
  7897. {  
  7898.    char string[10] = "123456789";  
  7899.    char symbol = 'c';  

  7900.    printf("Before strset(): %s\n", string);  
  7901.    strset(string, symbol);  
  7902.    printf("After strset():  %s\n", string);  
  7903.    return 0;  
  7904. }  
  7905.    
  7906.    
  7907.    

  7908. 函数名: strspn  
  7909. 功  能: 在串中查找指定字符集的子集的第一次出现  
  7910. 用  法: int strspn(char *str1, char *str2);  
  7911. 程序例:  

  7912. #include <stdio.h>  
  7913. #include <string.h>  
  7914. #include <alloc.h>  

  7915. int main(void)  
  7916. {  
  7917.    char *string1 = "1234567890";  
  7918.    char *string2 = "123DC8";  
  7919.    int length;  

  7920.    length = strspn(string1, string2);  
  7921.    printf("Character where strings differ is at position %d\n", length);  
  7922.    return 0;  
  7923. }  
  7924.    
  7925.    

  7926. 函数名: strstr  
  7927. 功  能: 在串中查找指定字符串的第一次出现  
  7928. 用  法: char *strstr(char *str1, char *str2);  
  7929. 程序例:  

  7930. #include <stdio.h>  
  7931. #include <string.h>  

  7932. int main(void)  
  7933. {  
  7934.    char *str1 = "Borland International", *str2 = "nation", *ptr;  

  7935.    ptr = strstr(str1, str2);  
  7936.    printf("The substring is: %s\n", ptr);  
  7937.    return 0;  
  7938. }  
  7939.    
  7940.    

  7941. 函数名: strtod  
  7942. 功  能: 将字符串转换为double型值  
  7943. 用  法: double strtod(char *str, char **endptr);  
  7944. 程序例:  

  7945. #include <stdio.h>  
  7946. #include <stdlib.h>  

  7947. int main(void)  
  7948. {  
  7949.    char input[80], *endptr;  
  7950.    double value;  

  7951.    printf("Enter a floating point number:");  
  7952.    gets(input);  
  7953.    value = strtod(input, &endptr);  
  7954.    printf("The string is %s the number is %lf\n", input, value);  
  7955.    return 0;  
  7956. }  
  7957.    
  7958.    
  7959.    

  7960. 函数名: strtok  
  7961. 功  能: 查找由在第二个串中指定的分界符分隔开的单词  
  7962. 用  法: char *strtok(char *str1, char *str2);  
  7963. 程序例:  

  7964. #include <string.h>  
  7965. #include <stdio.h>  

  7966. int main(void)  
  7967. {  
  7968.    char input[16] = "abc,d";  
  7969.    char *p;  

  7970.    /* strtok places a NULL terminator  
  7971.    in front of the token, if found */  
  7972.    p = strtok(input, ",");  
  7973.    if (p)   printf("%s\n", p);  

  7974.    /* A second call to strtok using a NULL  
  7975.    as the first parameter returns a pointer  
  7976.    to the character following the token  */  
  7977.    p = strtok(NULL, ",");  
  7978.    if (p)   printf("%s\n", p);  
  7979.    return 0;  
  7980. }  
  7981.    
  7982.    
  7983.    

  7984. 函数名: strtol  
  7985. 功  能: 将串转换为长整数  
  7986. 用  法: long strtol(char *str, char **endptr, int base);  
  7987. 程序例:  

  7988. #include <stdlib.h>  
  7989. #include <stdio.h>  

  7990. int main(void)  
  7991. {  
  7992.    char *string = "87654321", *endptr;  
  7993.    long lnumber;  

  7994.    /* strtol converts string to long integer  */  
  7995.    lnumber = strtol(string, &endptr, 10);  
  7996.    printf("string = %s  long = %ld\n", string, lnumber);  

  7997.    return 0;  
  7998. }  
  7999.    

  8000. 函数名: strupr  
  8001. 功  能: 将串中的小写字母转换为大写字母  
  8002. 用  法: char *strupr(char *str);  
  8003. 程序例:  

  8004. #include <stdio.h>  
  8005. #include <string.h>  

  8006. int main(void)  
  8007. {  
  8008.    char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;  

  8009.    /* converts string to upper case characters */  
  8010.    ptr = strupr(string);  
  8011.    printf("%s\n", ptr);  
  8012.    return 0;  
  8013. }  
  8014.    
  8015.    
  8016.    

  8017. 函数名: swab  
  8018. 功  能: 交换字节  
  8019. 用  法: void swab (char *from, char *to, int nbytes);  
  8020. 程序例:  

  8021. #include <stdlib.h>  
  8022. #include <stdio.h>  
  8023. #include <string.h>  

  8024. char source[15] = "rFna koBlrna d";  
  8025. char target[15];  

  8026. int main(void)  
  8027. {  
  8028.    swab(source, target, strlen(source));  
  8029.    printf("This is target: %s\n", target);  
  8030.    return 0;  
  8031. }  
  8032.    
  8033.    
  8034.    

  8035. 函数名: system  
  8036. 功  能: 发出一个DOS命令  
  8037. 用  法: int system(char *command);  
  8038. 程序例:  

  8039. #include <stdlib.h>  
  8040. #include <stdio.h>  

  8041. int main(void)  
  8042. {  
  8043.    printf("About to spawn command.com and run a DOS command\n");  
  8044.    system("dir");  
  8045.    return 0;  
  8046. }  
  8047. 函数名: tan  
  8048. 功  能: 正切函数  
  8049. 用  法: double tan(double x);  
  8050. 程序例:  

  8051. #include <stdio.h>  
  8052. #include <math.h>  

  8053. int main(void)  
  8054. {  
  8055.    double result, x;  

  8056.    x = 0.5;  
  8057.    result = tan(x);  
  8058.    printf("The tan of %lf is %lf\n", x, result);  
  8059.    return 0;  
  8060. }  
  8061.    
  8062.    
  8063.    

  8064. 函数名: tanh  
  8065. 功  能: 双曲正切函数  
  8066. 用  法: double tanh(double x);  
  8067. 程序例:  

  8068. #include <stdio.h>  
  8069. #include <math.h>  

  8070. int main(void)  
  8071. {  
  8072.    double result, x;  

  8073.    x = 0.5;  
  8074.    result = tanh(x);  
  8075.    printf("The hyperbolic tangent of %lf is %lf\n", x, result);  
  8076.    return 0;  
  8077. }  
  8078.    
  8079.    
  8080.    
  8081.    

  8082. 函数名: tell  
  8083. 功  能: 取文件指针的当前位置  
  8084. 用  法: long tell(int handle);  
  8085. 程序例:  

  8086. #include <string.h>  
  8087. #include <stdio.h>  
  8088. #include <fcntl.h>  
  8089. #include <io.h>  

  8090. int main(void)  
  8091. {  
  8092.    int handle;  
  8093.    char msg[] = "Hello world";  

  8094.    if ((handle = open("TEST.$", O_CREAT | O_TEXT | O_APPEND)) == -1)  
  8095.    {  
  8096.       perror("Error:");  
  8097.       return 1;  
  8098.    }  
  8099.    write(handle, msg, strlen(msg));  
  8100.    printf("The file pointer is at byte %ld\n", tell(handle));  
  8101.    close(handle);  
  8102.    return 0;  
  8103. }  
  8104.    
  8105.    
  8106.    
  8107.    

  8108. 函数名: textattr  
  8109. 功  能: 设置文本属性  
  8110. 用  法: void textattr(int attribute);  
  8111. 程序例:  

  8112. #include <conio.h>  

  8113. int main(void)  
  8114. {  
  8115.    int i;  

  8116.    clrscr();  
  8117.    for (i=0; i<9; i++)  
  8118.    {  
  8119.        textattr(i + ((i+1) << 4));  
  8120.        cprintf("This is a test\r\n");  
  8121.    }  

  8122.    return 0;  
  8123. }  
  8124.    
  8125.    
  8126.    

  8127. 函数名: textbackground  
  8128. 功  能: 选择新的文本背景颜色  
  8129. 用  法: void textbackground(int color);  
  8130. 程序例:  

  8131. #include <conio.h>  

  8132. int main(void)  
  8133. {  
  8134.    int i, j;  

  8135.    clrscr();  
  8136.    for (i=0; i<9; i++)  
  8137.    {  
  8138.        for (j=0; j<80; j++)  
  8139.          cprintf("C");  
  8140.        cprintf("\r\n");  
  8141.        textcolor(i+1);  
  8142.        textbackground(i);  
  8143.    }  

  8144.    return 0;  
  8145. }  
  8146.    
  8147.    
  8148.    

  8149. 函数名: textcolor  
  8150. 功  能: 在文本模式中选择新的字符颜色  
  8151. 用  法: void textcolor(int color);  
  8152. 程序例:  
  8153. #include <conio.h>  

  8154. int main(void)  
  8155. {  
  8156.    int i;  

  8157.    for (i=0; i<15; i++)  
  8158.    {  
  8159.        textcolor(i);  
  8160.        cprintf("Foreground Color\r\n");  
  8161.    }  

  8162.    return 0;  
  8163. }  
  8164.    
  8165.    
  8166.    

  8167. 函数名: textheight  
  8168. 功  能: 返回以像素为单位的字符串高度  
  8169. 用  法: int far textheight(char far *textstring);  
  8170. 程序例:  

  8171. #include <graphics.h>  
  8172. #include <stdlib.h>  
  8173. #include <stdio.h>  
  8174. #include <conio.h>  

  8175. int main(void)  
  8176. {  
  8177.    /* request auto detection */  
  8178.    int gdriver = DETECT, gmode, errorcode;  
  8179.    int y = 0;  
  8180.    int i;  
  8181.    char msg[80];  

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

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

  8193.    /* draw some text on the screen */  
  8194.    for (i=1; i<11; i++)  
  8195.    {  
  8196.       /* select the text style, direction, and size */  
  8197.       settextstyle(TRIPLEX_FONT, HORIZ_DIR, i);  

  8198.       /* create a message string */  
  8199.       sprintf(msg, "Size: %d", i);  

  8200.       /* output the message */  
  8201.       outtextxy(1, y, msg);  

  8202.       /* advance to the next text line */  
  8203.       y += textheight(msg);  
  8204.    }  

  8205.    /* clean up */  
  8206.    getch();  
  8207.    closegraph();  
  8208.    return 0;  
  8209. }  
  8210.    
  8211.    
  8212.    

  8213. 函数名: textmode  
  8214. 功  能: 将屏幕设置成文本模式  
  8215. 用  法: void textmode(int mode);  
  8216. 程序例:  

  8217. #include <conio.h>  

  8218. int main(void)  
  8219. {  
  8220.    textmode(BW40);  
  8221.    cprintf("ABC");  
  8222.    getch();  

  8223.    textmode(C40);  
  8224.    cprintf("ABC");  
  8225.    getch();  

  8226.    textmode(BW80);  
  8227.    cprintf("ABC");  
  8228.    getch();  

  8229.    textmode(C80);  
  8230.    cprintf("ABC");  
  8231.    getch();  

  8232.    textmode(MONO);  
  8233.    cprintf("ABC");  
  8234.    getch();  

  8235.    return 0;  
  8236. }  
  8237.    
  8238.    

  8239. 函数名: textwidth  
  8240. 功  能: 返回以像素为单位的字符串宽度  
  8241. 用  法: int far textwidth(char far *textstring);  
  8242. 程序例:  

  8243. #include <graphics.h>  
  8244. #include <stdlib.h>  
  8245. #include <stdio.h>  
  8246. #include <conio.h>  

  8247. int main(void)  
  8248. {  
  8249.    /* request auto detection */  
  8250.    int gdriver = DETECT, gmode, errorcode;  
  8251.    int x = 0, y = 0;  
  8252.    int i;  
  8253.    char msg[80];  

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

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

  8265.    y = getmaxy() / 2;  

  8266.    settextjustify(LEFT_TEXT, CENTER_TEXT);  
  8267.    for (i=1; i<11; i++)  
  8268.    {  
  8269.       /* select the text style, direction, and size */  
  8270.       settextstyle(TRIPLEX_FONT, HORIZ_DIR, i);  

  8271.       /* create a message string */  
  8272.       sprintf(msg, "Size: %d", i);  

  8273.       /* output the message */  
  8274.       outtextxy(x, y, msg);  

  8275.       /* advance to the end of the text */  
  8276.       x += textwidth(msg);  
  8277.    }  

  8278.    /* clean up */  
  8279.    getch();  
  8280.    closegraph();  
  8281.    return 0;  
  8282. }  
  8283.    
  8284.    

  8285. 函数名: time  
  8286. 功  能: 取一天的时间  
  8287. 用  法: logn time(long *tloc);  
  8288. 程序例:  

  8289. #include <time.h>  
  8290. #include <stdio.h>  
  8291. #include <dos.h>  

  8292. int main(void)  
  8293. {  
  8294.    time_t t;  

  8295.    t = time(NULL);  
  8296.    printf("The number of seconds since January 1, 1970 is %ld",t);  
  8297.    return 0;  
  8298. }  
  8299.    
  8300.    
  8301.    

  8302. 函数名: tmpfile  
  8303. 功  能: 以二进制方式打开暂存文件  
  8304. 用  法: FILE *tmpfile(void);  
  8305. 程序例:  

  8306. #include <stdio.h>  
  8307. #include <process.h>  

  8308. int main(void)  
  8309. {  
  8310.    FILE *tempfp;  

  8311.    tempfp = tmpfile();  
  8312.    if (tempfp)  
  8313.       printf("Temporary file created\n");  
  8314.    else  
  8315.    {  
  8316.       printf("Unable to create temporary file\n");  
  8317.       exit(1);  
  8318.    }  

  8319.    return 0;  
  8320. }  
  8321.    
  8322.    
  8323.    

  8324. 函数名: tmpnam  
  8325. 功  能: 创建一个唯一的文件名  
  8326. 用  法: char *tmpnam(char *sptr);  
  8327. 程序例:  

  8328. #include <stdio.h>  

  8329. int main(void)  
  8330. {  
  8331.    char name[13];  

  8332.    tmpnam(name);  
  8333.    printf("Temporary name: %s\n", name);  
  8334.    return 0;  
  8335. }  
  8336.    
  8337.    
  8338.    

  8339. 函数名: tolower  
  8340. 功  能: 把字符转换成小写字母  
  8341. 用  法: int tolower(int c);  
  8342. 程序例:  

  8343. #include <string.h>  
  8344. #include <stdio.h>  
  8345. #include <ctype.h>  

  8346. int main(void)  
  8347. {  
  8348.    int length, i;  
  8349.    char *string = "THIS IS A STRING";  

  8350.    length = strlen(string);  
  8351.    for (i=0; i<length; i++)  
  8352.    {  
  8353.        string[i] = tolower(string[i]);  
  8354.    }  
  8355.    printf("%s\n",string);  

  8356.    return 0;  
  8357. }  
  8358.    
  8359.    

  8360. 函数名: toupper  
  8361. 功  能: 把字符转换成大写字母  
  8362. 用  法: int toupper(int c);  
  8363. 程序例:  

  8364. #include <string.h>  
  8365. #include <stdio.h>  
  8366. #include <ctype.h>  

  8367. int main(void)  
  8368. {  
  8369.    int length, i;  
  8370.    char *string = "this is a string";  

  8371.    length = strlen(string);  
  8372.    for (i=0; i<length; i++)  
  8373.    {  
  8374.       string[i] = toupper(string[i]);  
  8375.    }  

  8376.    printf("%s\n",string);  

  8377.    return 0;  
  8378. }  
  8379.    
  8380.    

  8381. 函数名: tzset  
  8382. 功  能: UNIX时间兼容函数  
  8383. 用  法: void tzset(void);  
  8384. 程序例:  

  8385. #include <time.h>  
  8386. #include <stdlib.h>  
  8387. #include <stdio.h>  

  8388. int main(void)  
  8389. {  
  8390.    time_t td;  

  8391.    putenv("TZ=PST8PDT");  
  8392.    tzset();  
  8393.    time(&td);  
  8394.    printf("Current time = %s\n", asctime(localtime(&td)));  
  8395.    return 0;  
  8396. } 函数名: ultoa  
  8397. 功  能: 转换一个无符号长整型数为字符串  
  8398. 用  法: char *ultoa(unsigned long value, char *string, int radix);  
  8399. 程序例:  

  8400. #include <stdlib.h>  
  8401. #include <stdio.h>  

  8402. int main( void )  
  8403. {  
  8404.    unsigned long lnumber = 3123456789L;  
  8405.    char string[25];  

  8406.    ultoa(lnumber,string,10);  
  8407.    printf("string = %s  unsigned long = %lu\n",string,lnumber);  

  8408.    return 0;  
  8409. }  
  8410.    
  8411.    
  8412.    

  8413. 函数名: ungetc  
  8414. 功  能: 把一个字符退回到输入流中  
  8415. 用  法: int ungetc(char c, FILE *stream);  
  8416. 程序例:  

  8417. #include <stdio.h>  
  8418. #include <ctype.h>  

  8419. int main( void )  
  8420. {  
  8421.    int i=0;  
  8422.    char ch;  

  8423.    puts("Input an integer followed by a char:");  

  8424.    /* read chars until non digit or EOF */  
  8425.    while((ch = getchar()) != EOF && isdigit(ch))  
  8426.       i = 10 * i + ch - 48; /* convert ASCII into int value */  

  8427.    /* if non digit char was read, push it back into input buffer */  
  8428.    if (ch != EOF)  
  8429.       ungetc(ch, stdin);  

  8430.    printf("i = %d, next char in buffer = %c\n", i, getchar());  
  8431.    return 0;  
  8432. }  
  8433.    
  8434.    
  8435.    

  8436. 函数名: ungetch  
  8437. 功  能: 把一个字符退回到键盘缓冲区中  
  8438. 用  法: int ungetch(int c);  
  8439. 程序例:  

  8440. #include <stdio.h>  
  8441. #include <ctype.h>  
  8442. #include <conio.h>  

  8443. int main( void )  
  8444. {  
  8445.    int i=0;  
  8446.    char ch;  

  8447.    puts("Input an integer followed by a char:");  

  8448.    /* read chars until non digit or EOF */  
  8449.    while((ch = getche()) != EOF && isdigit(ch))  
  8450.       i = 10 * i + ch - 48; /* convert ASCII into int value */  

  8451.    /* if non digit char was read, push it back into input buffer */  
  8452.    if (ch != EOF)  
  8453.       ungetch(ch);  

  8454.    printf("\n\ni = %d, next char in buffer = %c\n", i, getch());  
  8455.    return 0;  
  8456. }  
  8457.    
  8458.    
  8459.    

  8460. 函数名: unixtodos  
  8461. 功  能: 把日期和时间转换成DOS格式  
  8462. 用  法: void unixtodos(long utime, struct date *dateptr,  
  8463.    struct time *timeptr);  
  8464. 程序例:  

  8465. #include <stdio.h>  
  8466. #include <dos.h>  

  8467. char *month[] = {"---", "Jan", "Feb", "Mar", "Apr", "May", "Jun",  
  8468.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};  

  8469. #define SECONDS_PER_DAY 86400L  /* the number of seconds in one day */  

  8470. struct date dt;  
  8471. struct time tm;  

  8472. int main(void)  
  8473. {  
  8474.    unsigned long val;  

  8475. /* get today's date and time */  
  8476.    getdate(&dt);  
  8477.    gettime(&tm);  
  8478.    printf("today is %d %s %d\n", dt.da_day, month[dt.da_mon], dt.da_year);  

  8479. /* convert date and time to unix format (number of seconds since Jan 1, 1970 */  
  8480.    val = dostounix(&dt, &tm);  
  8481. /* subtract 42 days worth of seconds */  
  8482.    val -= (SECONDS_PER_DAY * 42);  

  8483. /* convert back to dos time and date */  
  8484.    unixtodos(val, &dt, &tm);  
  8485.    printf("42 days ago it was %d %s %d\n",  
  8486.         dt.da_day, month[dt.da_mon], dt.da_year);  
  8487.    return 0;  
  8488. }  
  8489.    
  8490.    
  8491.    

  8492. 函数名: unlink  
  8493. 功  能: 删掉一个文件  
  8494. 用  法: int unlink(char *filename);  
  8495. 程序例:  

  8496. #include <stdio.h>  
  8497. #include <io.h>  

  8498. int main(void)  
  8499. {  
  8500.    FILE *fp = fopen("junk.jnk","w");  
  8501.    int status;  

  8502.    fprintf(fp,"junk");  

  8503.    status = access("junk.jnk",0);  
  8504.    if (status == 0)  
  8505.       printf("File exists\n");  
  8506.    else  
  8507.       printf("File doesn't exist\n");  

  8508.    fclose(fp);  
  8509.    unlink("junk.jnk");  
  8510.    status = access("junk.jnk",0);  
  8511.    if (status == 0)  
  8512.       printf("File exists\n");  
  8513.    else  
  8514.       printf("File doesn't exist\n");  
  8515.    

  8516.    return 0;  
  8517. }  
  8518.    
  8519.    
  8520.    

  8521. 函数名: unlock  
  8522. 功  能: 解除文件共享锁  
  8523. 用  法: int unlock(int handle, long offset, long length);  
  8524. 程序例:  

  8525. #include <io.h>  
  8526. #include <fcntl.h>  
  8527. #include <sys\stat.h>  
  8528. #include <process.h>  
  8529. #include <share.h>  
  8530. #include <stdio.h>  

  8531. int main(void)  
  8532. {  
  8533.    int handle, status;  
  8534.    long length;  

  8535.    handle = sopen("c:\\autoexec.bat",O_RDONLY,SH_DENYNO,S_IREAD);  

  8536.    if (handle < 0)  
  8537.    {  
  8538.        printf("sopen failed\n");  
  8539.        exit(1);  
  8540.    }  

  8541.    length = filelength(handle);  
  8542.    status = lock(handle,0L,length/2);  

  8543.    if (status == 0)  
  8544.       printf("lock succeeded\n");  
  8545.    else  
  8546.       printf("lock failed\n");  

  8547.    status = unlock(handle,0L,length/2);  

  8548.    if (status == 0)  
  8549.       printf("unlock succeeded\n");  
  8550.    else  
  8551.       printf("unlock failed\n");  

  8552.    close(handle);  
  8553.    return 0;  
  8554. }  
  8555. 函数名: vfprintf  
  8556. 功  能: 送格式化输出到一流中  
  8557. 用  法: int vfprintf(FILE *stream, char *format, va_list param);  
  8558. 程序例:  

  8559. #include <stdio.h>  
  8560. #include <stdlib.h>  
  8561. #include <stdarg.h>  

  8562. FILE *fp;  

  8563. int vfpf(char *fmt, ...)  
  8564. {  
  8565.    va_list argptr;  
  8566.    int cnt;  

  8567.    va_start(argptr, fmt);  
  8568.    cnt = vfprintf(fp, fmt, argptr);  
  8569.    va_end(argptr);  

  8570.    return(cnt);  
  8571. }  

  8572. int main(void)  
  8573. {  
  8574.    int inumber = 30;  
  8575.    float fnumber = 90.0;  
  8576.    char string[4] = "abc";  

  8577.    fp = tmpfile();  
  8578.    if (fp == NULL)  
  8579.    {  
  8580.       perror("tmpfile() call");  
  8581.       exit(1);  
  8582.    }  

  8583.    vfpf("%d %f %s", inumber, fnumber, string);  
  8584.    rewind(fp);  
  8585.    fscanf(fp,"%d %f %s", &inumber, &fnumber, string);  
  8586.    printf("%d %f %s\n", inumber, fnumber, string);  
  8587.    fclose(fp);  

  8588.    return 0;  
  8589. }  
  8590.    
  8591.    
  8592.    

  8593. 函数名: vfscanf  
  8594. 功  能: 从流中执行格式化输入  
  8595. 用  法: int vfscanf(FILE *stream, char *format, va_list param);  
  8596. 程序例:  

  8597. #include <stdio.h>  
  8598. #include <stdlib.h>  
  8599. #include <stdarg.h>  

  8600. FILE *fp;  

  8601. int vfsf(char *fmt, ...)  
  8602. {  
  8603.    va_list  argptr;  
  8604.    int cnt;  

  8605.    va_start(argptr, fmt);  
  8606.    cnt = vfscanf(fp, fmt, argptr);  
  8607.    va_end(argptr);  

  8608.    return(cnt);  
  8609. }  

  8610. int main(void)  
  8611. {  
  8612.    int inumber = 30;  
  8613.    float fnumber = 90.0;  
  8614.          char string[4] = "abc";  

  8615.    fp = tmpfile();  
  8616.    if (fp == NULL)  
  8617.    {  
  8618.       perror("tmpfile() call");  
  8619.       exit(1);  
  8620.    }  
  8621.    fprintf(fp,"%d %f %s\n",inumber,fnumber,string);  
  8622.    rewind(fp);  

  8623.    vfsf("%d %f %s",&inumber,&fnumber,string);  
  8624.    printf("%d %f %s\n",inumber,fnumber,string);  
  8625.    fclose(fp);  

  8626.    return 0;  
  8627. }  
  8628.    
  8629.    

  8630. 函数名: vprintf  
  8631. 功  能: 送格式化输出到stdout中  
  8632. 用  法: int vprintf(char *format, va_list param);  
  8633. 程序例:  

  8634. #include <stdio.h>  
  8635. #include <stdarg.h>  

  8636. int vpf(char *fmt, ...)  
  8637. {  
  8638.    va_list argptr;  
  8639.    int cnt;  

  8640.    va_start(argptr, format);  
  8641.    cnt = vprintf(fmt, argptr);  
  8642.    va_end(argptr);  

  8643.    return(cnt);  
  8644. }  

  8645. int main(void)  
  8646. {  
  8647.    int inumber = 30;  
  8648.    float fnumber = 90.0;  
  8649.    char *string = "abc";  

  8650.    vpf("%d %f %s\n",inumber,fnumber,string);  

  8651.    return 0;  
  8652. }  
  8653.    
  8654.    

  8655. 函数名: vscanf  
  8656. 功  能: 从stdin中执行格式化输入  
  8657. 用  法: int vscanf(char *format, va_list param);  
  8658. 程序例:  

  8659. #include <stdio.h>  
  8660. #include <conio.h>  
  8661. #include <stdarg.h>  

  8662. int vscnf(char *fmt, ...)  
  8663. {  
  8664.    va_list argptr;  
  8665.    int cnt;  

  8666.    printf("Enter an integer, a float,  and a string (e.g. i,f,s,)\n");  
  8667.    va_start(argptr, fmt);  
  8668.    cnt = vscanf(fmt, argptr);  
  8669.    va_end(argptr);  

  8670.    return(cnt);  
  8671. }  

  8672. int main(void)  
  8673. {  
  8674.    int inumber;  
  8675.    float fnumber;  
  8676.    char string[80];  

  8677.    vscnf("%d, %f, %s", &inumber, &fnumber, string);  
  8678.    printf("%d %f %s\n", inumber, fnumber, string);  

  8679.    return 0;  
  8680. }  
  8681.    
  8682.    
  8683.    

  8684. 函数名: vsprintf  
  8685. 功  能: 送格式化输出到串中  
  8686. 用  法: int vsprintf(char *string, char *format, va_list param);  
  8687. 程序例:  

  8688. #include <stdio.h>  
  8689. #include <conio.h>  
  8690. #include <stdarg.h>  

  8691. char buffer[80];  

  8692. int vspf(char *fmt, ...)  
  8693. {  
  8694.    va_list argptr;  
  8695.    int cnt;  

  8696.    va_start(argptr, fmt);  
  8697.    cnt = vsprintf(buffer, fmt, argptr);  
  8698.    va_end(argptr);  

  8699.    return(cnt);  
  8700. }  

  8701. int main(void)  
  8702. {  
  8703.    int inumber = 30;  
  8704.    float fnumber = 90.0;  
  8705.    char string[4] = "abc";  

  8706.    vspf("%d %f %s", inumber, fnumber, string);  
  8707.    printf("%s\n", buffer);  
  8708.    return 0;  
  8709. }  
  8710.    
  8711.    
  8712.    

  8713. 函数名: vsscanf  
  8714. 功  能: 从流中执行格式化输入  
  8715. 用  法: int vsscanf(char *s, char *format, va_list param);  
  8716. 程序例:  

  8717. #include <stdio.h>  
  8718. #include <conio.h>  
  8719. #include <stdarg.h>  

  8720. char buffer[80] = "30 90.0 abc";  

  8721. int vssf(char *fmt, ...)  
  8722. {  
  8723.    va_list  argptr;  
  8724.    int cnt;  

  8725.    fflush(stdin);  

  8726.    va_start(argptr, fmt);  
  8727.    cnt = vsscanf(buffer, fmt, argptr);  
  8728.    va_end(argptr);  

  8729.    return(cnt);  
  8730. }  

  8731. int main(void)  
  8732. {  
  8733.    int inumber;  
  8734.    float fnumber;  
  8735.    char string[80];  

  8736.    vssf("%d %f %s", &inumber, &fnumber, string);  
  8737.    printf("%d %f %s\n", inumber, fnumber, string);  
  8738.    return 0;  
  8739. }  
  8740. 函数名: wherex
  8741. 功  能: 返回窗口内水平光标位置
  8742. 用  法: int wherex(void);
  8743. 程序例:  

  8744. #include <conio.h>  

  8745. int main(void)
  8746. {
  8747.    clrscr();
  8748.    gotoxy(10,10);
  8749.    cprintf("Current location is X: %d  Y: %d\r\n", wherex(), wherey());
  8750.    getch();  

  8751.    return 0;
  8752. }
  8753.   
  8754.   
  8755.    

  8756. 函数名: wherey
  8757. 功  能: 返回窗口内垂直光标位置
  8758. 用  法: int wherey(void);
  8759. 程序例:  

  8760. #include <conio.h>  

  8761. int main(void)
  8762. {
  8763.    clrscr();
  8764.    gotoxy(10,10);
  8765.    cprintf("Current location is X: %d  Y: %d\r\n", wherex(), wherey());
  8766.    getch();  

  8767.    return 0;
  8768. }
  8769.   
  8770.   
  8771.    

  8772. 函数名: window
  8773. 功  能: 定义活动文本模式窗口
  8774. 用  法: void window(int left, int top, int right, int bottom);
  8775. 程序例:  

  8776. #include <conio.h>  

  8777. int main(void)
  8778. {  

  8779.    window(10,10,40,11);
  8780.    textcolor(BLACK);
  8781.    textbackground(WHITE);
  8782.    cprintf("This is a test\r\n");  

  8783.    return 0;
  8784. }
  8785.   
  8786.   
  8787.    

  8788. 函数名: write
  8789. 功  能: 写到一文件中
  8790. 用  法: int write(int handel, void *buf, int nbyte);
  8791. 程序例:  

  8792. #include <stdio.h>
  8793. #include <stdlib.h>
  8794. #include <fcntl.h>
  8795. #include <sys\stat.h>
  8796. #include <io.h>
  8797. #include <string.h>  

  8798. int main(void)
  8799. {
  8800.    int handle;
  8801.    char string[40];
  8802.    int length, res;  

  8803.    /*
  8804.     Create a file named "TEST.
  8805. $"inthecurrentdirectoryandwriteastringtoit.If"TEST.
  8806. $
  8807. "








































  8808. .


  8809. "




  8810. .
  8811. $" already exists, it will be overwritten.
  8812.    */  

  8813.    if ((handle = open("TEST.$", O_WRONLY | O_CREAT | O_TRUNC,
  8814.                          S_IREAD | S_IWRITE)) == -1)
  8815.    {
  8816.       printf("Error opening file.\n");
  8817.       exit(1);
  8818.    }  

  8819.    strcpy(string, "Hello, world!\n");
  8820.    length = strlen(string);  

  8821.    if ((res = write(handle, string, length)) != length)
  8822.    {
  8823.       printf("Error writing to the file.\n");
  8824.       exit(1);
  8825.    }
  8826.    printf("Wrote %d bytes to the file.\n", res);  

  8827.    close(handle);
  8828.    return 0;
  8829. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|紫影基地

GMT+8, 2025-1-12 12:09 , Processed in 0.098067 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表