dos函数

本文详细介绍了dos.h文件中的多个核心函数,包括absread、abswrite、allocmem等,这些函数分别用于磁盘扇区读写、内存分配等关键操作,并提供了相应的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
windows下的dos函数 一)MD——建立子目录   1.功能:创建新的子目录  2.类型:内部命令  3.格式:MD[盘符:][路径名]〈子目录名〉  4.使用说明:  (1)“盘符”:指定要建立子目录的磁盘驱动器字母,若省略,则为当前驱动器;  (2)“路径名”:要建立的子目录的上级目录名,若缺省则建在当前目录下。  例:(1)在C盘的根目录下创建名为FOX的子目录;(2)在FOX子目录下再创建USER子目录。  C:、>MD FOX (在当前驱动器C盘下创建子目录FOX)  C:、>MD FOX 、USER (在FOX 子目录下再创建USER子目录)  (二)CD——改变当前目录  1.功能:显示当前目录  2.类型:内部命令  3.格式:CD[盘符:][路径名][子目录名]  4.使用说明:  (1)如果省略路径和子目录名则显示当前目录;  (2)如采用“CD、”格式,则退回到根目录;  (3)如采用“CD.。”格式则退回到上一级目录。  例:(1)进入到USER子目录;(2)从USER子目录退回到子目录;(3)返回到根目录。  C:、>CD FOX 、USER(进入FOX子目录下的USER子目录)  C:、FOX、USER>CD.。 (退回上一级根目录)  C:、FOX>CD、 (返回到根目录)  C:、>  (三)RD——删除子目录命令  1.功能:从指定的磁盘删除了目录。  2.类型:内部命令  3.格式:RD[盘符:][路径名][子目录名]  4.使用说明:  (1)子目录在删除前必须是空的,也就是说需要先进入该子目录,使用DEL(删除文件的命令)将其子目录下的文件删空,然后再退回到上一级目录,用RD命令删除该了目录本身;  (2)不能删除根目录和当前目录。  例:要求把C盘FOX子目录下的USER子目录删除,操作如下:  第一步:先将USER子目录下的文件删空;  C、>DEL C:、FOX、USER、*。*  第二步,删除USER子目录。  C、>RD C:、FOX、USER  (四)DIR——显示磁盘目录命令  1.功能:显示磁盘目录的内容。  2.类型:内部命令  3.格式:DIR [盘符][路径][/P][/W]  4. 使用说明:/P的使用;当欲查看的目录太多,无法在一屏显示完屏幕会一直往上卷,不容易看清,加上/P参数后,屏幕上会分面一次显示23行的文件信息,然后暂停,并提示;Press any key to continue  /W的使用:加上/W只显示文件名,至于文件大小及建立的日期和时间则都省略。加上参数后,每行可以显示五个文件名。  PATH——路径设置命令  1.功能:设备可执行文件的搜索路径,只对文件有效。  2.类型:内部命令  3.格式:PATH[盘符1]目录[路径名1]{[;盘符2:],〈目录路径名2〉…}  4.使用说明:  (1)当运行一个可执行文件时,DOS会先在当前目录中搜索该文件,若找到则运行之;若找不到该文件,则根据PATH命令所设置的路径,顺序逐条地到目录中搜索该文件;  (2)PATH命令中的路径,若有两条以上,各路径之间以一个分号“;”隔开;  (3)PATH命令有三种使用方法:  PATH[盘符1:][路径1][盘符2:][路径2]…(设定可执行文件的搜索路径)  PATH:(取消所有路径)  PATH:(显示目前所设的路径)  (六)TREE——显示磁盘目录结构命令  1.功能:显示指定驱动器上所有目录路径和这些目录下的所有文件名。  2.类型:外部命令  3.格式:TREE[盘符:][/F][》PRN]  4.使用说明:  (1)使用/F参数时显示所有目录及目录下的所有文件,省略时,只显示目录,不显示目录下的文件;  (2)选用>PRN参数时,则把所列目录及目录中的文件名打印输出。  (七)DELTREE——删除整个目录命令  1.功能:将整个目录及其下属子目录和文件删除。  2.类型:外部命令  3.格式:DELTREE[盘符:]〈路径名〉  4.使用说明:该命令可以一步就将目录及其下的所有文件、子目录、更下层的子目录一并删除,而且不管文件的属性为隐藏、系统或只读,只要该文件位于删除的目录之下,DELTREE都一视同仁,照删不误。使用时务必小心!!!  五、磁盘操作类命令  (一)formAT——磁盘格式化命令  1.功能:对磁盘进行格式化,划分磁道和扇区;同时检查出整个磁盘上有无带缺陷的磁道,对坏道加注标记;建立目录区和文件分配表,使磁盘作好接收DOS的准备。  2.类型:外部命令  3.格式:formAT〈盘符:〉[/S][/4][/Q] 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值