CSDN博客

img luoboqingcai

不常用的函数的用法

发表于2004/9/21 13:16:00  1726人阅读

分类: 实用的东西

这里提到的都是一些不常用的函数的用法,常用函数请参考有关书籍。推荐谭浩强的《C程序设计》(清华大学出版社出版) 收集整理:Jenkin
函数名
[absread abswrite][bdos bdosptr][bioscom][biosdisk][biosequip][biosmemory][biosprint][biostime][cprintf]
[cputs puts][cscanfscanf][disable][dosexterr][enable][geninterrupt][getch][getdisk][getvect][getverify][harderr]
[inport][int86][intdos][intr][ioctl][kbhit][keep][peek][perror][randbrd][randbwr][segread][setvect][sleep]
[ungetc ungetch] [cgets] [[返回首页]]

1.函数说明:int absread(int drive, int nsects, int sectno, void *buffer); int abswrite(int drive, int nsects, in tsectno, void *buffer);
包含文件:<dos.h>
功能解释:磁盘绝对读写函数。返回整型值,driver为驱动器名,0代表A盘,1代表B盘. nsects是扇区数.tsectno是起始扇区号.buffer是被读取的内存地址.
用法: 返回值为0时,表示成功,为-1时,表示出错。
程序例:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h>
int main(void)
{ int i, strt, ch_out, sector;
char buf[512];
printf("Insert a diskette into drive A and press any key/n");
getch(); sector = 0;
if (absread(0, 1, sector, &buf) != 0) /*若读 A 驱动器出错*/
{ perror("Disk problem"); exit(1); /*退出程序*/ }
printf("Read OK/n");
strt = 3;
for (i=0; i<80; i++)
{ ch_out = buf[strt+i];
putchar(ch_out); /*读80个字符到buf中*/ }
printf("/n"); return(0); }
2.函数说明:  int bdos(int dosfun, unsigned dosdx, unsigned dosal);
           int bdosptr(int dosfun, void *argument, unsigned dosal);
包含文件:  <dos.h>
功能解释:  DOS系统调用.这两个函数提供了许多系统调用的直接访问.
用    法:  bdos函数用于整型参数的系统调用,可直接访问许多DOS系统资源.
           bdosptr函数用于指针参数系统调用,一般在大数据模式下使用.argument为
           指针入口参数.dossal为AL寄存器值.

程序例:
/*bdos函数用法例子*/

#include <stdio.h>
#include <dos.h>
/* Get current drive as 'A', 'B', ... */
char current_drive(void)
{
   char curdrive;

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

int main(void)
{
   printf("The current drive is %c:/n", current_drive());
   return 0;
}

/*bdosptr函数用法程序例*/

#include <string.h>
#include <stdio.h>
#include <dir.h>
#include <dos.h>
#include <errno.h>
#include <stdlib.h>
#define  BUFLEN  80
int main(void)
{
   char  buffer[BUFLEN];
   int   test;
printf("Enter full pathname of a directory/n");
   gets(buffer);
test = bdosptr(0x3B,buffer,0);
      if(test)
      {
  printf("DOS error message: %d/n", errno);
  /* See errno.h for error listings */
  exit (1);
      }
getcwd(buffer, BUFLEN);
   printf("The current directory is: %s/n", buffer);
return 0;
}

3.函数说明:  int bioscom(int cmd, char abyte, int port);

包含文件:  <bios.h>

功能解释:  执行串行I/O通信.

用    法:  使用前包含头文件.cmd 可取 0,1,2,3.abyte为一定的组合,用于设置
           数据位、停止位、奇偶校检和波特率.如: abyte值为 0xeb(0xe0|0x08|0x00|0x03)时,
           将设置通信口为9600波特率,奇校检,1停止位和8数据位.

程序例:

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

#define COM1       0
#define DATA_READY 0x100
#define TRUE       1
#define FALSE      0

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

int main(void)
{
   int in, out, status, DONE = FALSE;

   bioscom(0, SETTINGS, COM1);
   cprintf("... BIOSCOM [ESC] to exit .../n");
   while (!DONE)
   {
      status = bioscom(3, 0, COM1);
      if (status & DATA_READY)
  if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
     putch(out);
  if (kbhit())
  {
     if ((in = getch()) == '/x1B')
        DONE = TRUE;
     bioscom(1, in, COM1);
  }
   }
   return 0;
}

4.函数说明:  int biosdisk(int cmd, int drive, int head, int track, int sector
                          int nsects, void *buffer);

包含文件:  <bios.h>

功能解释:  软硬盘I/O 服务.使用中断0x13,把磁盘操作直接转给BIOS.

用    法:  调用前请包含头文件.
           cmd可取0到5(适用于IBM PC及其兼容机)6到20(仅适用于XT,AT,PS/2及其兼容
           机),具体含义请参阅有关书籍.driver为磁盘驱动器.0代表第一软驱,1代表第
           二软驱等.0x80代表第一硬驱,0x81代表第二硬驱等.注意:本函数队磁盘进行操
           作,会破坏磁盘上的内容,使用时要小心.

程序例:

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

int main(void)
{
   int result;
   char buffer[512];

   printf("Testing to see if drive a: is ready/n");
   result = biosdisk(4,0,0,0,0,1,buffer);
   result &= 0x02;
   (result) ? (printf("Drive A: Ready/n")) :
       (printf("Drive A: Not Ready/n"));

   return 0;
}

5.函数说明:  int biosequip(void);

包含文件:  <bios.h>

功能解释:  检查设备.使用了BIOS中断0x11.在IBM PC机上,监测的设备有:
           打印机数目及打印方式(串,并行),
           是否连接了游戏I/O设备,
           COM端口数,
           有无直接访问内存,
           硬盘数,视频模式,主板RAM大小.

用    法:  使用前包含头文件,返回值为一个位域的集合,集合中包含了设备情况.具体情况
           请参阅有关书籍.

程序例:

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

int main(void)
{
   int result;
   char buffer[512];

   printf("Testing to see if drive a: is ready/n");
   result = biosdisk(4,0,0,0,0,1,buffer);
   result &= 0x02;
   (result) ? (printf("Drive A: Ready/n")) :
       (printf("Drive A: Not Ready/n"));

   return 0;
}

6.函数说明:  int biosmemory(void);

包含文件:  <bios.h>

功能解释:  返回存储块大小.不含扩展内存和扩充内存.

用    法:  使用前包含头文件.返回一个整型值,以K为单位.

程序例:

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

int main(void)
{
   int memory_size;

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

7.函数说明:  int biosprint(int cmd, int byte, int port);

包含文件:  <bios.h>

功能解释:  直接使用BIOS服务的打印机I/O .被函数使用BIOS中断0x17来实现.

用    法:  port 为0对应于LTP1,为1对应于LPT2等.
           abort 的值在0到255之间.
           cmd 可取0,1,2.
           返回值是位值,用来表示打印机状态.

程序例:

#include <stdio.h>
#include <conio.h>
#include <bios.h>

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

   int status, abyte=0;

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

   if (status & 0x10)
      printf("Selected./n");
   if (status & 0x20)
      printf("Out of paper./n");

   if (status & 0x40)
      printf("Acknowledge./n");
   if (status & 0x80)
      printf("Not busy./n");

   return 0;
}

8.函数说明:  long biostime(int cmd, long newtime);

包含文件:  <bios.h>

功能解释:  读取或设置BIOS时间.使用BIOS的0x1a中断实现.

用    法:  cmd 为0时,返回计时器的当前值;为1时,计时器设置为long型的newtime值.

程序例:

#include <stdio.h>
#include <bios.h>
#include <time.h>
#include <conio.h>

int main(void)
{
   long bios_time;

   clrscr();
   cprintf("The number of clock ticks since midnight is:/r/n");
   cprintf("The number of seconds since midnight is:/r/n");
   cprintf("The number of minutes since midnight is:/r/n");
   cprintf("The number of hours since midnight is:/r/n");
   cprintf("/r/nPress any key to quit:");
   while(!kbhit())
   {
      bios_time = biostime(0, 0L);

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

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

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

      gotoxy(50, 4);
      cprintf("%.4f", bios_time / CLK_TCK / 3600);
   }
   return 0;
}

9.函数名: cprintf
头文件: <conio.h>
功  能: 送格式化输出至屏幕
用  法: int cprintf(const char *format[, argument, ...]); 可设置颜色.
程序例:

#include <conio.h>

int main(void)
{
   /* clear the screen */
   clrscr();

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

   /* output some text in the window */
   cprintf("Hello world/r/n");

   /* wait for a key */
   getch();
   return 0;
}

10.函数名: cputs
头文件: <conio.h>
功  能: 写字符到屏幕
用  法: void cputs(const char *string);
程序例:

#include <conio.h>

int main(void)
{
   /* clear the screen */
   clrscr();

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

   /* output some text in the window */
   cputs("This is within the window/r/n");

   /* wait for a key */
   getch();
   return 0;
}

函数名: puts
头文件: <stdio.h>
功  能: 送一字符串到流中
用  法: int puts(char *string);
程序例:

#include <stdio.h>
int main(void)
{
   char string[] = "This is an example output string/n";

   puts(string);
   return 0;
}

11.函数名: cscanf
头文件: <conio.h>
功  能: 从控制台执行格式化输入
用  法: int cscanf(char *format[,argument, ...]);
程序例:

#include <conio.h>

int main(void)
{
   char string[80];

   /* clear the screen */
   clrscr();

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

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

   /* display what was read */
   cprintf("/r/nThe string entered is: %s", string);
   return 0;
}
函数名: scanf
头文件: <stdio.h>
功  能: 执行格式化输入
用  法: int scanf(char *format[,argument,...]);
程序例:

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

int main(void)
{
   char label[20];
   char name[20];
   int entries = 0;
   int loop, age;
   double salary;

   struct Entry_struct
   {
      char  name[20];
      int   age;
      float salary;
   } entry[20];

/* Input a label as a string of characters restricting to 20 characters */
   printf("/n/nPlease enter a label for the chart: ");
   scanf("%20s", label);
   fflush(stdin);  /* flush the input stream in case of bad input */

/* Input number of entries as an integer */
   printf("How many entries will there be? (less than 20) ");
   scanf("%d", &entries);
   fflush(stdin);   /* flush the input stream in case of bad input */

/* input a name restricting input to only letters upper or lower case */
   for (loop=0;loop<entries;++loop)
   {
      printf("Entry %d/n", loop);
      printf("  Name   : ");
      scanf("%[A-Za-z]", entry[loop].name);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input an age as an integer */
      printf("  Age    : ");
      scanf("%d", &entry[loop].age);
      fflush(stdin);  /* flush the input stream in case of bad input */

/* input a salary as a float */
      printf("  Salary : ");
      scanf("%f", &entry[loop].salary);
      fflush(stdin); /* flush the input stream in case of bad input */
   }

/* Input a name, age and salary as a string, integer, and double */
   printf("/nPlease enter your name, age and salary/n");
   scanf("%20s %d %lf", name, &age, &salary);
 

/* Print out the data that was input */
   printf("/n/nTable %s/n",label);
   printf("Compiled by %s  age %d  $%15.2lf/n", name, age, salary);
   printf("-----------------------------------------------------/n");
   for (loop=0;loop<entries;++loop)
      printf("%4d | %-20s | %5d | %15.2lf/n",
         loop + 1,
  entry[loop].name,
  entry[loop].age,
         entry[loop].salary);
   printf("-----------------------------------------------------/n");
   return 0;
}
 

12.函数名: disable
头文件: <dos.h>
功  能: 屏蔽中断
用  法: void disable(void);
程序例:

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

#include <stdio.h>
#include <dos.h>
#include <conio.h>

#define INTR 0X1C    /* The clock tick
   interrupt */

void interrupt ( *oldhandler)(void);

int count=0;

void interrupt handler(void)
{
/* disable interrupts during the handling of
   the interrupt */
   disable();
/* increase the global counter */
   count++;
/* reenable interrupts at the end of the
   handler */
   enable();
/* call the old routine */
   oldhandler();
}

int main(void)
{
/* save the old interrupt vector */
   oldhandler = getvect(INTR);

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

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

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

   return 0;
}

13.函数名: dosexterr
头文件: <dos.h>
功  能: 获取扩展DOS错误信息
用  法: int dosexterr(struct DOSERR *dblkp);
程序例:

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

int main(void)
{
   FILE *fp;
   struct DOSERROR info;

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

   printf("Extended DOS error /
   information:/n");
   printf("   Extended error: /
   %d/n",info.exterror);
   printf("             Class: /
   %x/n",info.class);
   printf("            Action: /
   %x/n",info.action);
   printf("      Error Locus: /
   %x/n",info.locus);

   return 0;
}

14.函数名: enable
头文件: <dos.h>
功  能: 开放硬件中断
用  法: void enable(void);
程序例:

/* ** NOTE:
This is an interrupt service routine. You can NOT compile this program
with Test Stack Overflow turned on and get an executable file which will
operate correctly.
*/

#include <stdio.h>
#include <dos.h>
#include <conio.h>

/* The clock tick interrupt */
#define INTR 0X1C

void interrupt ( *oldhandler)(void);

int count=0;

void interrupt handler(void)
{
/*
   disable interrupts during the handling of the interrupt
*/
   disable();
/* increase the global counter */
   count++;
/*
   re enable interrupts at the end of the handler
*/
   enable();
/* call the old routine */
   oldhandler();
}

int main(void)
{
/* save the old interrupt vector */
   oldhandler = getvect(INTR);

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

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

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

   return 0;
}

15.函数名: geninterrupt
头文件: <dos.h>
功  能: 产生一个软中断
用  法: void geninterrupt(int intr_num);
程序例:

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

/* function prototype */
void writechar(char ch);

int main(void)
{
   clrscr();
   gotoxy(80,25);
   writechar('*');
   getch();
   return 0;
}

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

void writechar(char ch)
{
   struct text_info ti;
   /* grab current text settings */
   gettextinfo(&ti);
   /* interrupt 0x10 sub-function 9 */
   _AH = 9;
   /* character to be output */
   _AL = ch;
   _BH = 0;                   /* video page */
   _BL = ti.attribute;  /* video attribute */
   _CX = 1;           /* repetition factor */
   geninterrupt(0x10);  /* output the char */
}
 

16.函数名: getch
头文件: <conio.h>
功  能: 从控制台无回显地取一个字符
用  法: int getch(void);
程序例:

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

int main(void)
{
   char ch;

   printf("Input a character:");
   ch = getche();
   printf("/nYou input a '%c'/n", ch);
   return 0;
}
 

函数名: getche
头文件: <conio.h>
功  能: 从控制台取字符(带回显)
用  法: int getche(void);
程序例:

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

int main(void)
{
   char ch;

   printf("Input a character:");
   ch = getche();
   printf("/nYou input a '%c'/n", ch);
   return 0;
}

函数名: getc
头文件: <stdio.h>
功  能: 从流中取字符
用  法: int getc(FILE *stream);
程序例:

#include <stdio.h>

int main(void)
{
   char ch;

   printf("Input a character:");
/* read a character from the
   standard input stream */
   ch = getc(stdin);
   printf("The character input was: '%c'/n",
          ch);
   return 0;
}

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

#include <stdio.h>

int main(void)
{
   int c;

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

   while ((c = getchar()) != '/n')
      printf("%c", c);

   return 0;
}

17.函数名: getdisk
头文件: <dir.h>
功  能: 取当前磁盘驱动器号
用  法: int getdisk(void);
程序例:

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

int main(void)
{
   int disk;

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

函数名: getdta
头文件: <dos.h>
功  能: 取磁盘传输地址
用  法: char far *getdta(void);
程序例:

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

int main(void)
{
   char far *dta;

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

18.函数名: getvect
头文件: <dos.h>
功  能: 取得中断向量入口
用  法: void interrupt(*getvect(int intr_num));
程序例:

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

void interrupt get_out(); /* interrupt prototype */

void interrupt (*oldfunc)(); /* interrupt function pointer */
int looping = 1;

int main(void)
{
  puts("Press <Shift><Prt Sc> to terminate");

  /* save the old interrupt */
  oldfunc  = getvect(5);

   /* install interrupt handler */
  setvect(5,get_out);

   /* do nothing */
  while (looping);

   /* restore to original interrupt routine */
   setvect(5,oldfunc);

  puts("Success");
  return 0;
}
void interrupt get_out()
{
  looping = 0; /* change global variable to get out of loop */
}
 

19.函数名: getverify
头文件: <dos.h>
功  能: 返回DOS校验标志状态
用  法: int getverify(void);
程序例:

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

int main(void)
{
   if (getverify())
      printf("DOS verify flag is on/n");
   else
      printf("DOS verify flag is off/n");
   return 0;
}

20.函数名: harderr
头文件: <dos.h>
功  能: 建立一个硬件错误处理程序
用  法: void harderr(int (*fptr)());
程序例:
/*This program will trap disk errors and prompt
the user for action. Try running it with no
disk in drive A: to invoke its functions.*/

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define IGNORE  0
#define RETRY   1
#define ABORT   2
int buf[500];
/*define the error messages for trapping disk problems*/
static char *err_msg[] = {
    "write protect",
    "unknown unit",
    "drive not ready",
    "unknown command",
    "data error (CRC)",
    "bad request",
    "seek error",
    "unknown media type",
    "sector not found",
    "printer out of paper",
    "write fault",
    "read fault",
    "general failure",
    "reserved",
    "reserved",
    "invalid disk change"
};

error_win(char *msg)
{
   int retval;

   cputs(msg);

/*prompt for user to press a key to abort, retry, ignore*/
   while(1)
   {
       retval= getch();
       if (retval == 'a' || retval == 'A')
       {
    retval = ABORT;
    break;
       }
       if (retval == 'r' || retval == 'R')
       {
    retval = RETRY;
    break;
       }
       if (retval == 'i' || retval == 'I')
       {
           retval = IGNORE;
           break;
       }
   }

   return(retval);
}

/*pragma warn -par reduces warnings which occur
due to the non use of the parameters errval,
bp and si to the handler.*/
#pragma warn -par

int handler(int errval,int ax,int bp,int si)
{
   static char msg[80];
   unsigned di;
   int drive;
   int errorno;
   di= _DI;
/*if this is not a disk error then it was
another device having trouble*/

   if (ax < 0)
   {
      /* report the error */
      error_win("Device error");
      /* and return to the program directly requesting abort */
      hardretn(ABORT);
   }
/* otherwise it was a disk error */
   drive = ax & 0x00FF;
   errorno = di & 0x00FF;
/* report which error it was */
   sprintf(msg, "Error: %s on drive %c/r/nA)bort, R)etry, I)gnore: ",
    err_msg[errorno], 'A' + drive);
/*
return to the program via dos interrupt 0x23 with abort, retry,
or ignore as input by the user.
*/
   hardresume(error_win(msg));
   return ABORT;
}
#pragma warn +par

int main(void)
{
/*
install our handler on the hardware problem interrupt
*/
   harderr(handler);
   clrscr();
   printf("Make sure there is no disk in drive A:/n");
   printf("Press any key ..../n");
   getch();
   printf("Trying to access drive A:/n");
   printf("fopen returned %p/n",fopen("A:temp.dat", "w"));
   return 0;
}
 

21.函数名: inport
头文件: <dos.h>
功  能: 从硬件端口中输入
用  法: int inp(int protid);
程序例:

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

int main(void)
{
   int result;
   int port = 0;  /* serial port 0 */

   result = inport(port);
   printf("Word read from port %d = 0x%X/n", port, result);
   return 0;
}

22.函数名: int86
头文件: <dos.h>
功  能: 通用8086软中断接口
用  法: int int86(int intr_num, union REGS *inregs, union REGS *outregs);
程序例:

#include <stdio.h>
#include <conio.h>
#include <dos.h>

#define VIDEO 0x10

void movetoxy(int x, int y)
{
   union REGS regs;

   regs.h.ah = 2;  /* set cursor postion */
   regs.h.dh = y;
   regs.h.dl = x;
   regs.h.bh = 0;  /* video page 0 */
   int86(VIDEO, &regs, &regs);
}

int main(void)
{
   clrscr();
   movetoxy(35, 10);
   printf("Hello/n");
   return 0;
}

函数名: int86x
头文件: <dos.h>
功  能: 通用8086软中断接口
用  法: int int86x(int intr_num, union REGS *insegs, union REGS *outregs,
     struct SREGS *segregs);
程序例:

#include <dos.h>
#include <process.h>
#include <stdio.h>

int main(void)
{
   char filename[80];
   union REGS inregs, outregs;
   struct SREGS segregs;

   printf("Enter filename: ");
   gets(filename);
   inregs.h.ah = 0x43;
   inregs.h.al = 0x21;
   inregs.x.dx = FP_OFF(filename);
   segregs.ds = FP_SEG(filename);
   int86x(0x21, &inregs, &outregs, &segregs);
   printf("File attribute: %X/n", outregs.x.cx);
   return 0;
}
 

23.函数名: intdos
头文件: <dos.h>
功  能: 通用DOS接口
用  法: int intdos(union REGS *inregs, union REGS *outregs);
程序例:

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

/* deletes file name; returns 0 on success, nonzero on failure */
int delete_file(char near *filename)
{
   union REGS regs;
   int ret;
   regs.h.ah = 0x41;                             /* delete file */
   regs.x.dx = (unsigned) filename;
   ret = intdos(&regs, &regs);

   /* if carry flag is set, there was an error */
   return(regs.x.cflag ? ret : 0);
}

int main(void)
{
   int err;
   err = delete_file("NOTEXIST.$$$");
   if (!err)
      printf("Able to delete NOTEXIST.$$$/n");
   else
      printf("Not Able to delete NOTEXIST.$$$/n");
   return 0;
}
 
  函数名: intdosx
头文件: <dos.h>
功  能: 通用DOS中断接口
用  法: int intdosx(union REGS *inregs, union REGS *outregs,
      struct SREGS *segregs);
程序例:

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

/* deletes file name; returns 0 on success, nonzero on failure */
int delete_file(char far *filename)
{
   union REGS regs; struct SREGS sregs;
   int ret;
   regs.h.ah = 0x41;                       /* delete file */
   regs.x.dx = FP_OFF(filename);
   sregs.ds = FP_SEG(filename);
   ret = intdosx(&regs, &regs, &sregs);

   /* if carry flag is set, there was an error */
   return(regs.x.cflag ? ret : 0);
}

int main(void)
{
   int err;
   err = delete_file("NOTEXIST.$$$");
   if (!err)
      printf("Able to delete NOTEXIST.$$$/n");
   else
      printf("Not Able to delete NOTEXIST.$$$/n");
   return 0;
}
 

24.函数名: intr
头文件: <dos.h>
功  能: 改变软中断接口
用  法: void intr(int intr_num, struct REGPACK *preg);
程序例:

#include <stdio.h>
#include <string.h>
#include <dir.h>
#include <dos.h>

#define CF 1  /* Carry flag */

int main(void)
{
   char directory[80];
   struct REGPACK reg;

   printf("Enter directory to change to: ");
   gets(directory);
   reg.r_ax = 0x3B << 8;          /* shift 3Bh into  AH */
   reg.r_dx = FP_OFF(directory);
   reg.r_ds = FP_SEG(directory);
   intr(0x21, &reg);
   if (reg.r_flags & CF)
      printf("Directory change failed/n");
   getcwd(directory, 80);
   printf("The current directory is: %s/n", directory);
   return 0;
}

25.函数名: ioctl
头文件: <io.h>
功  能: 控制I/O设备
用  法: int ioctl(int handle, int cmd[,int *argdx, int argcx]);
程序例:

#include <stdio.h>
#include <dir.h>
#include <io.h>

int main(void)
{
   int stat;

   /* use func 8 to determine if the default drive is removable */
   stat = ioctl(0, 8, 0, 0);
   if (!stat)
      printf("Drive %c is removable./n", getdisk() + 'A');
   else
      printf("Drive %c is not removable./n", getdisk() + 'A');
   return 0;
}
 

26.函数名: kbhit
头文件: <kbhit>
功  能: 检查当前按下的键
用  法: int kbhit(void);
程序例:

#include <conio.h>

int main(void)
{
   cprintf("Press any key to continue:");
   while (!kbhit()) /* do nothing */ ;
   cprintf("/r/nA key was pressed.../r/n");
   return 0;
}

27.函数名: keep
头文件: <dos.h>
功  能: 退出并继续驻留
用  法: void keep(int status, int size);
程序例:

/***NOTE:
   This is an interrupt service routine.  You
   can NOT compile this program with Test
   Stack Overflow turned on and get an
   executable file which will operate
   correctly.  Due to the nature of this
   function the formula used to compute
   the number of paragraphs may not
   necessarily work in all cases.  Use with
   care!  Terminate Stay Resident (TSR)
   programs are complex and no other support
   for them is provided.  Refer to the
   MS-DOS technical documentation
   for more information.  */
#include <dos.h>
/* The clock tick interrupt */
#define INTR 0x1C
/* Screen attribute (blue on grey) */
#define ATTR 0x7900

/* reduce heaplength and stacklength
to make a smaller program in memory */
extern unsigned _heaplen = 1024;
extern unsigned _stklen  = 512;

void interrupt ( *oldhandler)(void);

void interrupt handler(void)
{
   unsigned int (far *screen)[80];
   static int count;

/* For a color screen the video memory
   is at B800:0000.  For a monochrome
   system use B000:000 */
   screen = MK_FP(0xB800,0);

/* increase the counter and keep it
   within 0 to 9 */
   count++;
   count %= 10;

/* put the number on the screen */
   screen[0][79] = count + '0' + ATTR;

/* call the old interrupt handler */
   oldhandler();
}

int main(void)
{

/* get the address of the current clock
   tick interrupt */
oldhandler = getvect(INTR);

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

/* _psp is the starting address of the
   program in memory.  The top of the stack
   is the end of the program.  Using _SS and
   _SP together we can get the end of the
   stack.  You may want to allow a bit of
   saftey space to insure that enough room
   is being allocated ie:
   (_SS + ((_SP + safety space)/16) - _psp)
*/
keep(0, (_SS + (_SP/16) - _psp));
return 0;
}

28.函数名: peek
头文件: <dos.h>
功  能: 检查存储单元
用  法: int peek(int segment, unsigned offset);
程序例:

#include <stdio.h>
#include <conio.h>
#include <dos.h>

int main(void)
{
   int value = 0;

   printf("The current status of your keyboard is:/n");
   value = peek(0x0040, 0x0017);
   if (value & 1)
      printf("Right shift on/n");
   else
      printf("Right shift off/n");

   if (value & 2)
      printf("Left shift on/n");
   else
      printf("Left shift off/n");

   if (value & 4)
      printf("Control key on/n");
   else
      printf("Control key off/n");

   if (value & 8)
      printf("Alt key on/n");
   else
      printf("Alt key off/n");

   if (value & 16)
      printf("Scroll lock on/n");
   else
      printf("Scroll lock off/n");

   if (value & 32)
      printf("Num lock on/n");
   else
      printf("Num lock off/n");

   if (value & 64)
      printf("Caps lock on/n");
   else
      printf("Caps lock off/n");

   return 0;
}
 
 

函数名: peekb
头文件: <dos.h>
功  能: 检查存储单元
用  法: char peekb (int segment, unsigned offset);
程序例:

#include <stdio.h>
#include <conio.h>
#include <dos.h>

int main(void)
{
   int value = 0;

   printf("The current status of your keyboard is:/n");
   value = peekb(0x0040, 0x0017);
   if (value & 1)
      printf("Right shift on/n");
   else
      printf("Right shift off/n");

   if (value & 2)
      printf("Left shift on/n");
   else
      printf("Left shift off/n");

   if (value & 4)
      printf("Control key on/n");
   else
      printf("Control key off/n");

   if (value & 8)
      printf("Alt key on/n");
   else
      printf("Alt key off/n");

   if (value & 16)
      printf("Scroll lock on/n");
   else
      printf("Scroll lock off/n");

   if (value & 32)
      printf("Num lock on/n");
   else
      printf("Num lock off/n");

   if (value & 64)
      printf("Caps lock on/n");
   else
      printf("Caps lock off/n");

   return 0;
}
 

29.函数名: perror
头文件: <stdio.h>
功  能: 系统错误信息
用  法: void perror(char *string);
程序例:

#include <stdio.h>

int main(void)
{
   FILE *fp;

   fp = fopen("perror.dat", "r");
   if (!fp)
      perror("Unable to open file for reading");
   return 0;
}

30.函数名: randbrd
头文件: <process.h>
功  能: 随机块读
用  法: int randbrd(struct fcb *fcbptr, int reccnt);
程序例:

#include <process.h>
#include <string.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
   char far *save_dta;
   char line[80], buffer[256];
   struct fcb blk;
   int i, result;

   /* get user input file name for dta */
   printf("Enter drive and file name (no path - i.e. a:file.dat)/n");
   gets(line);

   /* put file name in fcb */
   if (!parsfnm(line, &blk, 1))
   {
      printf("Error in call to parsfnm/n");
      exit(1);
   }
   printf("Drive #%d  File: %s/n/n", blk.fcb_drive, blk.fcb_name);

   /* open file with DOS FCB open file */
   bdosptr(0x0F, &blk, 0);

   /* save old dta, and set new one */
   save_dta = getdta();
   setdta(buffer);

   /* set up info for the new dta */
   blk.fcb_recsize = 128;
   blk.fcb_random = 0L;
   result = randbrd(&blk, 1);

   /* check results from randbrd */
   if (!result)
      printf("Read OK/n/n");
   else
   {
      perror("Error during read");
      exit(1);
   }

   /* read in data from the new dta */
   printf("The first 128 characters are:/n");
   for (i=0; i<128; i++)
      putchar(buffer[i]);

   /* restore previous dta */
   setdta(save_dta);

   return 0;
}

31.函数名: randbwr
头文件: <process.h>
功  能: 随机块写
用  法: int randbwr(struct fcp *fcbptr, int reccnt);
程序例:

#include <process.h>
#include <string.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
   char far *save_dta;
   char line[80];
   char buffer[256] = "RANDBWR test!";
   struct fcb blk;
   int result;

   /* get new file name from user */
   printf("Enter a file name to create (no path - ie. a:file.dat/n");
   gets(line);

   /* parse the new file name to the dta */
   parsfnm(line,&blk,1);
   printf("Drive #%d  File: %s/n", blk.fcb_drive, blk.fcb_name);

   /* request DOS services to create file */
   if (bdosptr(0x16, &blk, 0) == -1)
   {
      perror("Error creating file");
      exit(1);
   }

   /* save old dta and set new dta */
   save_dta = getdta();
   setdta(buffer);

   /* write new records */
   blk.fcb_recsize = 256;
   blk.fcb_random = 0L;
   result = randbwr(&blk, 1);

   if (!result)
      printf("Write OK/n");
   else
   {
      perror("Disk error");
      exit(1);
   }

   /* request DOS services to close the file */
   if (bdosptr(0x10, &blk, 0) == -1)
   {
      perror("Error closing file");
      exit(1);
   }

   /* reset the old dta */
   setdta(save_dta);

   return 0;
}

32.函数名: segread
头文件: <dos.h>
功  能: 读段寄存器值
用  法: void segread(struct SREGS *segtbl);
程序例:

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

int main(void)
{
   struct SREGS segs;

   segread(&segs);
   printf("Current segment register settings/n/n");
   printf("CS: %X   DS: %X/n", segs.cs, segs.ds);
   printf("ES: %X   SS: %X/n", segs.es, segs.ss);

   return 0;
}
 

33.函数名: setvect
头文件: <dos.h>
功  能: 设置中断矢量入口
用  法: void setvect(int intr_num, void interrupt(*isr)());
程序例:

/***NOTE:
    This is an interrupt service routine.  You can NOT compile this
    program with Test Stack Overflow turned on and get an executable
    file which will operate correctly. */

#include <stdio.h>
#include <dos.h>
#include <conio.h>

#define INTR 0X1C    /* The clock tick interrupt */

void interrupt ( *oldhandler)(void);

int count=0;

void interrupt handler(void)
{
/* increase the global counter */
   count++;

/* call the old routine */
   oldhandler();
}

int main(void)
{
/* save the old interrupt vector */
   oldhandler = getvect(INTR);

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

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

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

   return 0;
}
 

34.函数名: sleep
头文件: <dos.h>
功  能: 执行挂起一段时间
用  法: unsigned sleep(unsigned seconds);
程序例:

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

int main(void)
{
   int i;

   for (i=1; i<5; i++)
   {
      printf("Sleeping for %d seconds/n", i);
      sleep(i);
   }
   return 0;
}
 

35.函数名: ungetc
头文件: <stdio.h>
功  能: 把一个字符退回到输入流中
用  法: int ungetc(char c, FILE *stream);
程序例:

#include <stdio.h>
#include <ctype.h>

int main( void )
{
   int i=0;
   char ch;

   puts("Input an integer followed by a char:");

   /* read chars until non digit or EOF */
   while((ch = getchar()) != EOF && isdigit(ch))
      i = 10 * i + ch - 48; /* convert ASCII into int value */

   /* if non digit char was read, push it back into input buffer */
   if (ch != EOF)
      ungetc(ch, stdin);

   printf("i = %d, next char in buffer = %c/n", i, getchar());
   return 0;
}
 
 
 

函数名: ungetch
头文件: <conio.h>
功  能: 把一个字符退回到键盘缓冲区中
用  法: int ungetch(int c);
程序例:

#include <stdio.h>
#include <ctype.h>
#include <conio.h>

int main( void )
{
   int i=0;
   char ch;

   puts("Input an integer followed by a char:");

   /* read chars until non digit or EOF */
   while((ch = getche()) != EOF && isdigit(ch))
      i = 10 * i + ch - 48; /* convert ASCII into int value */

   /* if non digit char was read, push it back into input buffer */
   if (ch != EOF)
      ungetch(ch);

   printf("/n/ni = %d, next char in buffer = %c/n", i, getch());
   return 0;
}
 

36.函数说明:  char *cgets(char *str);

功能解释:  从控制台(一般为键盘)读字符串.

用    法:  函数返回指向str[2]的指针,str可以是指针,也可以是一维数组.此函数支持颜色方式.

程序例:

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

int main(void)
{
   char buffer[83];
   char *p;

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

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

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

   printf("Input some chars:");
   p = cgets(buffer);
   printf("/ncgets read %d characters: /"%s/"/n", buffer[1], p);
   printf("The returned pointer is %p, buffer[0] is at %p/n", p, &buffer);
   return 0;
}

0 0

相关博文

我的热门文章

img
取 消
img