dos.h文件函数详解
@函数名称: absread
函数原型: int absread(int drive, int nsects, int sectnum, void *buffer)
函数功能: 对磁盘扇区读数据
函数返回: 0-操作成功,其他-操作失败
参数说明: driver-读取的驱动器号 0-表示驱动器A,以此类推
sectnum-读取的扇区号
msects-读取的扇区数
所属文件: <dos.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h>
int main()
{
int i,strt,ch_out,sector;char buf[512];
printf("Insert a diskette into drive A and press any key");
getch();
sector=0;
if(absread(0,1,sector,&buf)!=0)
{
perror("Disk problem");
exit(1);
}
printf("Read OK");
strt=3;
for (i=0;i<80;i++)
{
ch_out=buf[strt+i];
putchar(ch_out);
}
printf("\n");
return(0);
}
@函数名称: abswrite
函数原型: int abswrite(int drive, int nsects, int sectnum, void *buffer)
函数功能: 对磁盘扇区写数据
函数返回: 0-操作成功,其他-操作失败
参数说明: driver-写入的驱动器号
sectnum-写入的扇区号
msects-写入的扇区数
buf-写入内容
所属文件: <dos.h>
#include <dos.h>
#include <stdio.h>
unsignedchar buff[512];
/* 此程企图将FAT区和ROOT区清空从而快速格式化磁盘 */
int main()
{
int i;char c;
printf("Quick Format 1.44MB");
printf("ALL DATA IN THE FLOPPY DISK WILL BE LOST!!");
printf("Insert a diskette for drive A:");
printf("and press ENTER when ready. . .");c=getchar();
printf("Cleaning FAT area. . .");
buff[0]=0xf0;buff[1]=buff[2]=0xff;
for(i=3;i<512;i++)buff=0;
abswrite(0,1,1,buff);
abswrite(0,1,10,buff);
for(i=0;i<512;i++)buff=0;
for(i=2;i<10;i++)abswrite(0,1,i,buff);
for(i=11;i<19;i++)abswrite(0,1,i,buff);
printf("Cleaning ROOT area. . .");
for(i=19;i<33;i++)abswrite(0,1,i,buff);
printf("QuickFormat Completed!");
return 0;
}
@函数名称: allocmem
函数原型: int allocmem(unsigned size, unsigned * seg)
函数功能: 按节(16字节)分配内存块
函数返回: -1:分配成功,其他数值:实际分配的节数
参数说明: size-分配的节数,seg-分配后的段地址
所属文件: <dos.h>
#include <dos.h>
#include <malloc.h>
#include <stdio.h>
int main()
{
unsignedint size,segp;
int stat;
size=64; /* (64 x 16)=1024 bytes */
stat=allocmem(size,&segp);
if (stat==-1)
printf("Allocated memory at segment: %x",segp);
else
printf("Failed: maximum number of paragraphs available is %u",stat);
return 0;
}
@函数名称: bdos
函数原型: int bdos(int fnum, unsigneddx, unsignedal)
函数功能: 执行DOS系统调用INT 21H
函数返回: 寄存器AX的值
参数说明: fnum-系统调用号,dx,al-赋给DX,AL寄存器的值
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
char current_drive(void)
{
char curdrive;
curdrive=bdos(0x19,0,0);
return('A'+curdrive);
}
int main()
{
printf("The current drive is %c:",current_drive());
return 0;
}
@函数名称: country
函数原型: struct country *country(int countrycode, struct country *countryptr)
函数功能: 用于设置一些与国家相关的项目,如货币,时间日期的表示方式等
函数返回: 国家相关信息
参数说明: 结构定义:struct country{
int co_date; /*日期格式*/
char co_curr[5]; /*货币符号*/
char co_thsep[2]; /*数字分隔符*/
char co_desep[2]; /*小数点*/
char co_dtsep[2]; /*日期分隔符*/
char co_tmsep[2]; /*时间分隔符*/
char co_currstyle; /*货币形式*/
char co_digits; /*有效数字*/
int (far *co_case)(); /*事件处理函数*/
char co_dasep; /*数据分隔符*/
char co_fill[10]; /*补充字符*/
}
所属文件: <dos.h>
#include <dos.h>
#include <stdio.h>
#define USA 0
int main()
{
struct COUNTRY country_info;
country(USA,&country_info);
printf("The currency symbol for the USA is: %s",country_info.co_curr);
return 0;
}
@函数名称: ctrlbrk
函数原型: void ctrlbrk(int (*pfun)(vpod))
函数功能: 设置当Ctrl-Break组合键按下时,应执行的函数
函数返回:
参数说明: pfun-要执行的函数指针,该函数应返回0值
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
#define ABORT 0
int c_break()
{
printf("Control-Break pressed. Program aborting ...");
return (ABORT);
}
int main()
{
ctrlbrk(c_break);
for(;;)
printf("Looping... Press <Ctrl-Break> to quit:");
return 0;
}
@函数名称: dosexterr
函数原型: int dosexterr(struct DOSERR *err)
函数功能: 得到DOS扩展调用失败时的错误信息
函数返回: 错误信息保存在err结构中
参数说明: err-用于保存错误信息的结构指针
关于DOS extended error代码(即:struct DOSERR.exterror)各系统差别极大,不一一列出
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
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\t",info.exterror);
printf("Class: %x\n",info.class);
printf("Action: %x\t",info.action);
printf("Error Locus: %x\n",info.locus);
return 0;
}
@函数名称: getcbrk
函数原型: int getcbrk(void)
函数功能: 检测对Ctrl-Break按下的检测是否允许
函数返回: 0-允许检测,1-不允许检测
参数说明:
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
if (getcbrk())
printf("Cntrl-brk flag is on");
else
printf("Cntrl-brk flag is off");
return 0;
}
@函数名称: getdate
函数原型: void getdate(struct date *d)
函数功能: 得到系统日期
函数返回:
参数说明: d 存放得到的日期信息
所属文件: <dos.h>
#include <dos.h>
#include <stdio.h>
int main()
{
struct date d;
getdate(&d);
printf("The current year is: %d",d.da_year);
printf("The current day is: %d",d.da_day);
printf("The current month is: %d",d.da_mon);
return 0;
}
@函数名称: setdate
函数原型: void setdate(struct date *datep)
函数功能: 设置DOS日期
函数返回:
参数说明: datep-要设置的日期数值,struct date定义格式如下:
struct date{
int da_year;
char da_day;
char da_mon;
};
所属文件: <dos.h>
#include <stdio.h>
#include <process.h>
#include <dos.h>
int main()
{
struct date reset;
struct date save_date;
getdate(&save_date);
printf("Original date:");
system("date");
reset.da_year=2001;
reset.da_day=1;
reset.da_mon=1;
setdate(&reset);
printf("Date after setting:");
system("date");
setdate(&save_date);
printf("Back to original date:");
system("date");
return 0;
}
@函数名称: settime
函数原型: void settime(struct time *timep)
函数功能: 设置DOS时间
函数返回:
参数说明: timep-要设置的时间数值,struct time定义格式如下:
struct time{
unsignedchar ti_min;
unsignedchar ti_hour;
unsignedchar ti_hund;
unsignedchar ti_sec;
};
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
struct time t;
gettime(&t);
printf("The current minute is: %d",t.ti_min);
printf("The current hour is: %d",t.ti_hour);
printf("The current hundredth of a second is: %d",t.ti_hund);
printf("The current second is: %d",t.ti_sec);
t.ti_min++;
settime(&t);
return 0;
}
@函数名称: gettime
函数原型: void gettime(struct date *t)
函数功能: 得到系统时间
函数返回:
参数说明: d 存放得到的日期信息
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
struct time t;
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d",t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund);
return 0;
}
@函数名称: getdfree
函数原型: void getdfree(int drive, struct dfree *dfptr)
函数功能: 得到驱动器的剩余磁盘空间
函数返回: 以结构返回:struct dfree{
unsigned df_avail; /* 可用簇数 */
unsigned df_total; /* 总簇数 */
unsigned df_bsec; /* 每扇区字节数 */
unsigned df_sclus; /* 每簇扇区数 */
}
参数说明: drive-驱动器号:1-A盘,2-B盘……
所属文件: <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <dos.h>
int main()
{
struct dfree free;
long avail;
int drive;
drive=getdisk();
getdfree(drive+1,&free);
if(free.df_sclus==0xFFFF)
{
printf("Error in getdfree() call\n");
exit(1);
}
avail=(long)free.df_avail * (long)free.df_bsec * (long)free.df_sclus;
printf("Drive %c: has %ld bytes available\n",'A'+drive,avail);
return 0;
}
@函数名称: absread
函数原型: int absread(int drive, int nsects, int sectnum, void *buffer)
函数功能: 对磁盘扇区读数据
函数返回: 0-操作成功,其他-操作失败
参数说明: driver-读取的驱动器号 0-表示驱动器A,以此类推
sectnum-读取的扇区号
msects-读取的扇区数
所属文件: <dos.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h>
int main()
{
int i,strt,ch_out,sector;char buf[512];
printf("Insert a diskette into drive A and press any key");
getch();
sector=0;
if(absread(0,1,sector,&buf)!=0)
{
perror("Disk problem");
exit(1);
}
printf("Read OK");
strt=3;
for (i=0;i<80;i++)
{
ch_out=buf[strt+i];
putchar(ch_out);
}
printf("\n");
return(0);
}
@函数名称: abswrite
函数原型: int abswrite(int drive, int nsects, int sectnum, void *buffer)
函数功能: 对磁盘扇区写数据
函数返回: 0-操作成功,其他-操作失败
参数说明: driver-写入的驱动器号
sectnum-写入的扇区号
msects-写入的扇区数
buf-写入内容
所属文件: <dos.h>
#include <dos.h>
#include <stdio.h>
unsignedchar buff[512];
/* 此程企图将FAT区和ROOT区清空从而快速格式化磁盘 */
int main()
{
int i;char c;
printf("Quick Format 1.44MB");
printf("ALL DATA IN THE FLOPPY DISK WILL BE LOST!!");
printf("Insert a diskette for drive A:");
printf("and press ENTER when ready. . .");c=getchar();
printf("Cleaning FAT area. . .");
buff[0]=0xf0;buff[1]=buff[2]=0xff;
for(i=3;i<512;i++)buff=0;
abswrite(0,1,1,buff);
abswrite(0,1,10,buff);
for(i=0;i<512;i++)buff=0;
for(i=2;i<10;i++)abswrite(0,1,i,buff);
for(i=11;i<19;i++)abswrite(0,1,i,buff);
printf("Cleaning ROOT area. . .");
for(i=19;i<33;i++)abswrite(0,1,i,buff);
printf("QuickFormat Completed!");
return 0;
}
@函数名称: allocmem
函数原型: int allocmem(unsigned size, unsigned * seg)
函数功能: 按节(16字节)分配内存块
函数返回: -1:分配成功,其他数值:实际分配的节数
参数说明: size-分配的节数,seg-分配后的段地址
所属文件: <dos.h>
#include <dos.h>
#include <malloc.h>
#include <stdio.h>
int main()
{
unsignedint size,segp;
int stat;
size=64; /* (64 x 16)=1024 bytes */
stat=allocmem(size,&segp);
if (stat==-1)
printf("Allocated memory at segment: %x",segp);
else
printf("Failed: maximum number of paragraphs available is %u",stat);
return 0;
}
@函数名称: bdos
函数原型: int bdos(int fnum, unsigneddx, unsignedal)
函数功能: 执行DOS系统调用INT 21H
函数返回: 寄存器AX的值
参数说明: fnum-系统调用号,dx,al-赋给DX,AL寄存器的值
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
char current_drive(void)
{
char curdrive;
curdrive=bdos(0x19,0,0);
return('A'+curdrive);
}
int main()
{
printf("The current drive is %c:",current_drive());
return 0;
}
@函数名称: country
函数原型: struct country *country(int countrycode, struct country *countryptr)
函数功能: 用于设置一些与国家相关的项目,如货币,时间日期的表示方式等
函数返回: 国家相关信息
参数说明: 结构定义:struct country{
int co_date; /*日期格式*/
char co_curr[5]; /*货币符号*/
char co_thsep[2]; /*数字分隔符*/
char co_desep[2]; /*小数点*/
char co_dtsep[2]; /*日期分隔符*/
char co_tmsep[2]; /*时间分隔符*/
char co_currstyle; /*货币形式*/
char co_digits; /*有效数字*/
int (far *co_case)(); /*事件处理函数*/
char co_dasep; /*数据分隔符*/
char co_fill[10]; /*补充字符*/
}
所属文件: <dos.h>
#include <dos.h>
#include <stdio.h>
#define USA 0
int main()
{
struct COUNTRY country_info;
country(USA,&country_info);
printf("The currency symbol for the USA is: %s",country_info.co_curr);
return 0;
}
@函数名称: ctrlbrk
函数原型: void ctrlbrk(int (*pfun)(vpod))
函数功能: 设置当Ctrl-Break组合键按下时,应执行的函数
函数返回:
参数说明: pfun-要执行的函数指针,该函数应返回0值
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
#define ABORT 0
int c_break()
{
printf("Control-Break pressed. Program aborting ...");
return (ABORT);
}
int main()
{
ctrlbrk(c_break);
for(;;)
printf("Looping... Press <Ctrl-Break> to quit:");
return 0;
}
@函数名称: dosexterr
函数原型: int dosexterr(struct DOSERR *err)
函数功能: 得到DOS扩展调用失败时的错误信息
函数返回: 错误信息保存在err结构中
参数说明: err-用于保存错误信息的结构指针
关于DOS extended error代码(即:struct DOSERR.exterror)各系统差别极大,不一一列出
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
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\t",info.exterror);
printf("Class: %x\n",info.class);
printf("Action: %x\t",info.action);
printf("Error Locus: %x\n",info.locus);
return 0;
}
@函数名称: getcbrk
函数原型: int getcbrk(void)
函数功能: 检测对Ctrl-Break按下的检测是否允许
函数返回: 0-允许检测,1-不允许检测
参数说明:
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
if (getcbrk())
printf("Cntrl-brk flag is on");
else
printf("Cntrl-brk flag is off");
return 0;
}
@函数名称: getdate
函数原型: void getdate(struct date *d)
函数功能: 得到系统日期
函数返回:
参数说明: d 存放得到的日期信息
所属文件: <dos.h>
#include <dos.h>
#include <stdio.h>
int main()
{
struct date d;
getdate(&d);
printf("The current year is: %d",d.da_year);
printf("The current day is: %d",d.da_day);
printf("The current month is: %d",d.da_mon);
return 0;
}
@函数名称: setdate
函数原型: void setdate(struct date *datep)
函数功能: 设置DOS日期
函数返回:
参数说明: datep-要设置的日期数值,struct date定义格式如下:
struct date{
int da_year;
char da_day;
char da_mon;
};
所属文件: <dos.h>
#include <stdio.h>
#include <process.h>
#include <dos.h>
int main()
{
struct date reset;
struct date save_date;
getdate(&save_date);
printf("Original date:");
system("date");
reset.da_year=2001;
reset.da_day=1;
reset.da_mon=1;
setdate(&reset);
printf("Date after setting:");
system("date");
setdate(&save_date);
printf("Back to original date:");
system("date");
return 0;
}
@函数名称: settime
函数原型: void settime(struct time *timep)
函数功能: 设置DOS时间
函数返回:
参数说明: timep-要设置的时间数值,struct time定义格式如下:
struct time{
unsignedchar ti_min;
unsignedchar ti_hour;
unsignedchar ti_hund;
unsignedchar ti_sec;
};
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
struct time t;
gettime(&t);
printf("The current minute is: %d",t.ti_min);
printf("The current hour is: %d",t.ti_hour);
printf("The current hundredth of a second is: %d",t.ti_hund);
printf("The current second is: %d",t.ti_sec);
t.ti_min++;
settime(&t);
return 0;
}
@函数名称: gettime
函数原型: void gettime(struct date *t)
函数功能: 得到系统时间
函数返回:
参数说明: d 存放得到的日期信息
所属文件: <dos.h>
#include <stdio.h>
#include <dos.h>
int main()
{
struct time t;
gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d",t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund);
return 0;
}
@函数名称: getdfree
函数原型: void getdfree(int drive, struct dfree *dfptr)
函数功能: 得到驱动器的剩余磁盘空间
函数返回: 以结构返回:struct dfree{
unsigned df_avail; /* 可用簇数 */
unsigned df_total; /* 总簇数 */
unsigned df_bsec; /* 每扇区字节数 */
unsigned df_sclus; /* 每簇扇区数 */
}
参数说明: drive-驱动器号:1-A盘,2-B盘……
所属文件: <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <dos.h>
int main()
{
struct dfree free;
long avail;
int drive;
drive=getdisk();
getdfree(drive+1,&free);
if(free.df_sclus==0xFFFF)
{
printf("Error in getdfree() call\n");
exit(1);
}
avail=(long)free.df_avail * (long)free.df_bsec * (long)free.df_sclus;
printf("Drive %c: has %ld bytes available\n",'A'+drive,avail);
return 0;
}