当前位置: 首页 > 图文教程 > 开发语言 > C/C++ > C/C++:小编谈C语言函数那些事(4)

C/C++
C和C++的特点
pragma 预处理指令详解
C++ 中什么是内联函数
C/C++没有数组
C/C++返回内部静态成员的陷阱
学好C/C++的办法
C/C++中时间函数的介绍
c/c++混合编程
c/C++内存分配
[转]浅谈C语言学习与C++语言学习的关系
托管C++
windows进程中的内存结构
C++学习重点分析
浅析scanf()函数中%[]格式控制符
C/C++:一个跨平台的 C++ 内存泄漏检测器
C/C++:C/C++时间函数使用方法
C/C++:线程冲突你了解多少?
C/C++:小编浅谈函数宏应用优缺点
C/C++:小编谈C语言函数那些事(1)
C/C++:小编谈C语言函数那些事(2)

C/C++:小编谈C语言函数那些事(4)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 55 ::
收藏到网摘: n/a

C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。

1.       delline函数

delline函数的功能是在文本窗口中删去一行,其用法为:void delline(void);程序例子如下:

#include <conio.h>

int main(void)

{

   clrscr();

   cprintf("The function DELLINE deletes \

    the line containing the\r\n");

   cprintf("cursor and moves all lines \

    below it one line up.\r\n");

   cprintf("DELLINE operates within the \

    currently active text\r\n");

   cprintf("window.  Press any key to \

    continue . . .");

   gotoxy(1,2);  /* Move the cursor to the

      second line and first column */

   getch();

   delline();

   getch();

   return 0;

}

2.       detectgraph函数

detectgraph函数的功能是通过检测硬件确定图形驱动程序和模式,其用法为:void far detectgraph(int far *graphdriver, int far *graphmode); 程序例子如下:

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

/* names of the various cards supported */

char *dname[] = { "requests detection",

    "a CGA",

    "an MCGA",

    "an EGA",

    "a 64K EGA",

    "a monochrome EGA",

    "an IBM 8514",

    "a Hercules monochrome",

    "an AT&T 6300 PC",

    "a VGA",

    "an IBM 3270 PC"

  };

int main(void)

{

   /* returns detected hardware info. */

   int gdriver, gmode, errorcode;

  /* detect graphics hardware available */

   detectgraph(&gdriver, &gmode);

   /* read result of detectgraph call */

   errorcode = graphresult();

   if (errorcode != grOk)  /* an error

         occurred */

   {

      printf("Graphics error: %s\n", \

      grapherrormsg(errorcode));

      printf("Press any key to halt:");

      getch();

      exit(1); /* terminate with an error

    code */

   }

   /* display the information detected */

   clrscr();

   printf("You have %s video display \

   card.\n", dname[gdriver]);

   printf("Press any key to halt:");

   getch();

   return 0;

}

 

3.       difftime函数

difftime函数的功能是计算两个时刻之间的时间差,其用法为:double difftime(time_t time2, time_t time1);  程序例子如下:

#include <time.h>

#include <stdio.h>

#include <dos.h>

#include <conio.h>

int main(void)

{

   time_t first, second;

   clrscr();

   first = time(NULL);  /* Gets system

      time */

   delay(2000);         /* Waits 2 secs */

   second = time(NULL); /* Gets system time

      again */

   printf("The difference is: %f \

   seconds\n",difftime(second,first));

   getch();

   return 0;

}

4.       dup函数

dup函数的功能是复制一个文件句柄,其用法为:int dup(int handle); 程序例子如下:

#include <string.h>

#include <stdio.h>

#include <conio.h>

#include <io.h>

void flush(FILE *stream);

int main(void)

{

   FILE *fp;

   char msg[] = "This is a test";

   /* create a file */

   fp = fopen("DUMMY.FIL", "w");

   /* write some data to the file */

   fwrite(msg, strlen(msg), 1, fp);

   clrscr();

   printf("Press any key to flush \

   DUMMY.FIL:");

   getch();

   /* flush the data to DUMMY.FIL without

      closing it */

   flush(fp);

   printf("\nFile was flushed, Press any \

   key to quit:");

   getch();

   return 0;

}

void flush(FILE *stream)

{

   int duphandle;

   /* flush TC's internal buffer */

   fflush(stream);

   /* make a duplicate file handle */

   duphandle = dup(fileno(stream));

   /* close the duplicate handle to flush the

      DOS buffer */

   close(duphandle);

}