borland c++ bulder的文件操作总结

在bcb中也提供了文件操作的函数,这些函数的功能与前面所介绍的大致相同,但这类函数与bcb关系紧密,能使用bcb中的ansistring等数据类型,在bcb中用这种方式的文件操作是最方便的,下面我就把这种文件操作详细介绍.

1.文件名函数

  在bcb提供的这组文件操作函数中,可分为三种类型,就是:1.文件名函数,2.文件管理函数;3.文件i/o函数. 【推荐阅读:Use Nid to Change db

函数 说明 【扩展信息:palmT5发布了

  文件名函数可以对文件的名称.所在子目录.驱动器与扩展名等进行操作.下表列出这些函数及其功能.

 

expandfilename() 返回文件的全路径(含驱动器.路径)

 

extractfileext() 从文件名中抽取扩展名

 

extractfilename() 从文件名中抽取不含路径的文件名

 

extractfilepath() 从文件名中抽取路径名

 

extractfiledir() 从文件名中抽取目录名

 

extractfiledrive() 从文件名中抽取驱动器名

 

changefileext() 改变文件的扩展名

 

expanduncfilename() 返回含有网络驱动器的文件全路径

 

extractrelativepath() 从文件名中抽取相对路径信息

 

extractshortpathname() 把文件名转化为dos的8·3格式

 

matchesmask() 检查文件是否与指定的文件名格式匹配

 

  下面就把这些函数作一一介绍:

⑴expandfilename()

  原型:extern package ansistring __fastcall expandfilename(const ansistring filename);

 

  功能:返回文件的全路径(含驱动器.路径)

  参数:filename:要处理的文件名

  例:showmessage(expandfilename(application->exename));//显示你的程序文件名,如c:/mybcb/sample1.exe

⑵extractfileext()

  原型:extern package ansistring __fastcall extractfileext(const ansistring filename);

 

  功能:从文件名中抽取扩展名

  参数:filename:要处理的文件名(全路径)

  例:showmessage(extractfileext(application->exename));//显示".exe"

⑶extractfilename()

  原型:extern package ansistring __fastcall extractfilename(const ansistring filename);

 

  功能:从文件名中抽取不含路径的文件名

  参数:filename:要处理的文件名

  例:showmessage(extractfileext("c://winnt//sol.exe"));//显示"sol.exe"

⑷extractfilepath()

  原型:extern package ansistring __fastcall extractfilepath(const ansistring filename);

 

  功能:从文件名中抽取路径名

参数:filename:要处理的文件名

⑸extractfiledir() 【程序编程相关:WAP初级教程笔记---2

  例:showmessage(extractfilepath("winnt//sol.exe"));//显示"winnt/" 【推荐阅读:Use Nid to Change db

  功能:从文件名中抽取目录名(与上个函数不同,不包括最后的"/") 【扩展信息:palmT5发布了

  原型:extern package ansistring __fastcall extractfiledir(const ansistring filename);

 

  参数:filename:要处理的文件名

  例:showmessage(extractfiledir("winnt//sol.exe"));//显示"winnt",注意与上个函数的区别

⑹extractfiledrive()

  原型:extern package ansistring __fastcall extractfiledrive(const ansistring filename);

 

  功能:从文件名中抽取驱动器名

  参数:filename:要处理的文件名

  例:showmessage(extractfiledrive("c://winnt//sol.exe"));//显示"c:"

⑺changefileext()

  原型:extern package system::ansistring __fastcall changefileext(const system::ansistring filename, const system::ansistring extension);

 

  功能:更改文件名的扩展名,不是对真正的文件进行改名,只是对文件名这个字符串进行处理

  参数:filename:要改名的文件名,extension:新的扩展名

  例:showmessage(changefileext("c://winnt//sol.exe",".ooo"));//显示"c:/winnt/sol.ooo"

⑻expanduncfilename()

  原型:extern package ansistring __fastcall expanduncfilename(const ansistring filename);

 

  功能:返回含有网络驱动器的文件全路径,格式为://机器名/共享名/文件名

  参数:filename:要处理的文件名

  例:showmessage(expanduncfilename("f://winnt//sol.exe"));/*如果f:是映射的网络驱动器//nt40/winnt,则显示"//nt40/winnt/sol.exe"*/

⑼extractrelativepath()

  原型:extern package ansistring __fastcall extractrelativepath(const ansistring basename, const ansistring destname);

 

  功能:从文件名中抽取相对路径信息,如"../sss/ss.asd"这种形式

参数:basename:基准文件名;destname:目标文件名

⑽extractshortpathname() 【程序编程相关:WAP初级教程笔记---2

  例:showmessage(extractrelativepath("d://source//c//1.123","d://source//asm//dz.asm"));/*显示"../asm/dz.asm"*/ 【推荐阅读:Use Nid to Change db

  功能:把文件名转换为dos的8.3格式 【扩展信息:palmT5发布了

  原型:extern package ansistring __fastcall extractshortpathname(const ansistring filename);

 

  参数:filename:要处理的文件名

  例:showmessage(extractshortpathname("e://program files//dual wheel mouse//4dmain.exe"));/*显示"e:/progra~1/dualwh~1/4dmain.exe"*/

⑾matchesmask()

  原型:extern package bool __fastcall matchesmask(const ansistring filename, const ansistring mask);

 

  功能:检查文件是否与指定的文件名格式匹配

  参数:filename:要处理的文件名;mask:文件名格式,支持通配符

  例:showmessage(matchesmask("lxf.exe","*.?x?));//显示"true"

 

--------------------------------------------------------------------------------

 

2.文件管理函数

  这类函数包括设置与读取驱动器.子目录与文件的有关的各种操作,下表列出这类操作常用的函数及其功能.

 

函数 功能

createdir() 创建新的子目录

 

deletefile() 删除文件

 

directoryexists() 判断目录是否存在

 

diskfree() 获取磁盘剩余空间

 

disksize() 获取磁盘容量

 

fileexists() 判断文件是否存在

 

filegetattr() 获取文件属性

 

filegetdate() 获取文件日期

 

getcurrentdir() 获取当前目录

 

removedir() 删除目录

 

setcurrentdir() 设置当前目录

 

  下面就把这些函数作一一介绍:

⑴createdir()

  原型:extern package bool __fastcall createdir(const system::ansistring dir);

 

  功能:建立子目录,如果成功返回true,否则返回false

  参数:dir:要建立的子目录的名字

  例:create("asm");//在当前目录下建立一个名为asm的子目录

⑵deletefile()

  原型:extern package bool __fastcall deletefile(const system::ansistring filename);

功能:删除文件,如果成功返回true,否则返回false

  例:if(opendialog1->execute())deletefile(opendialog1->filename); 【程序编程相关:WAP初级教程笔记---2

  参数:filename:要删除的文件名 【推荐阅读:Use Nid to Change db

  原型:extern package bool __fastcall directoryexists(const system:: ansistring name); 【扩展信息:palmT5发布了

 

⑶directoryexists()

  功能:检测目录是否存在,如果存在返回true,否则返回false

  参数:name:要检测的目录名

  例:if(!directoryexists("asm"))createdir("asm");//如果asm这个目录不存在则创建之

⑷diskfree()

  原型:extern package __int64 __fastcall diskfree(byte drive);

 

  功能:检测磁盘剩余空间,返回值以字节为单位,如果指定的磁盘无效,返回-1

  参数:drive:磁盘的代号,0表示当前盘, 1=a,2=b,3=c 以此类推

  例:showmessage(diskfree(0));//显示当前盘的剩余空间

⑸disksize()

  原型:extern package __int64 __fastcall disksize(byte drive);

 

  功能:检测磁盘容量,返回值以字节为单位,如果指定的磁盘无效,返回-1

  参数:drive:磁盘的代号,0表示当前盘, 1=a,2=b,3=c 以此类推

  例:showmessage(diskfree(0));//显示当前盘的容量

⑹fileexists()

  原型:extern package bool __fastcall fileexists(const ansistring filename);

 

  功能:检测文件是否存在,如果存在返回true,否则返回false

  参数:filename:要检测的文件名

  例:if(fileexists("aaa.asm"))deletefile("aaa.asm");

⑺filegetattr()

  原型:extern package int __fastcall filegetattr(const ansistring filename);

 

  功能:取得文件属性,如果出错返回-1

返回值如下表,如果返回$00000006表示是一个具有隐含与系统属性的文件(4+2)

常量 值 含义

fareadonly $00000001 只读文件

 

fahidden $00000002 隐含文件

 

fasysfile $00000004 系统文件

 

favolumeid $00000008 卷标

 

fadirectory $00000010 目录

 

faarchive $00000020 归档文件

 

  例:if(filegetattr("lll.txt")&0x2)showmessage("这是一个有隐含属性的文件");

  与此对应的有filesetattr() ,请自已查阅帮助系统

  原型:extern package int __fastcall filegetdate(int handle); 【程序编程相关:WAP初级教程笔记---2

 

⑻filegetdate() 【推荐阅读:Use Nid to Change db

 

  参数:handle:用fileopen()打开的文件句柄. 【扩展信息:palmT5发布了

  功能:返回文件的建立时间到1970-1-1日0时的秒数

  例:

    int i=fileopen("c://autoexec.bat",fmopenread);

    showmessage(filegetdate(i));

 

    fileclose(i);

 

  与此对应的有filesetdate(),请自已查阅帮助系统

⑼getcurrentdir()

  原型:extern package ansistring __fastcall getcurrentdir();

 

  功能:取得当前的目录名

  例:showmessage(getcurrentdir());

⑽removedir()

  原型:extern package bool __fastcall removedir(const ansistring dir);

 

  功能:删除目录,如果成功返回true,否则返回false

  参数:dir:要删除的目录名

  例:if(diectoryexists("asm"))removedir("asm");

⑾setcurrentdir()

  原型:extern package bool __fastcall setcurrentdir(const ansistring dir);

 

  功能:设置当前目录,如果成功返回true,否则返回false

  参数:dir:要切换到的目录名

  例:setcurrentdir("c://windows");

 

--------------------------------------------------------------------------------

 

3.文件i/o函数

  这类函数完成对文件的读写相关的操作,这种类型的操作与c的基于i/o文件操作类似,下表列出这类操作常用的函数及其功能.

 

 

fileopen() 打开文件

 

fileclose() 关闭文件

 

fileread() 读文件

 

fileseek() 文件定位

 

filewrite() 写文件

 

filecreate() 创建文件

 

  下面就对这些函数作详细介绍.

⑴fileopen()

  原型:extern package int __fastcall fileopen(const ansistring filename, int mode);

 

  功能:打开文件,如果成功返回其句柄,否则返回-1

  参数:filename:要打开的文件名;mode:打开的方式,取值如下表,可用"或"("|")运算符连接.

常量 值 说明

fmopenread 0 以只读属性打开 【程序编程相关:WAP初级教程笔记---2

 

------------------------------------------------------------- 【推荐阅读:Use Nid to Change db

 

fmopenreadwrite 2 以读/写属性打开 【扩展信息:palmT5发布了

 

fmopenwrite 1 以只写属性打开

 

fmsharecompat 0 兼容fcb方式(汇编中有相应的dos功能调用,感兴趣自已查阅相关资料)

 

fmshareexclusive 16 共享方式:以独占方式打开,在关闭以前,别人不能访问

 

fmsharedenywrite 32 共享方式:拒绝写访问

 

fmsharedenyread 48 共享方式:拒绝读访问

 

fmsharedenynone 64 共享方式:无限制,允许读写

 

  例:int i=fileopen("c://windows//win.ini",fmopenreadwrite|fmshareexclusive);

 

⑵fileclose()

  原型:extern package void __fastcall fileclose(int handle);

 

  功能:关闭打开的句柄.

  参数:handle:要关闭的句柄

  例:fileclose(i);

⑶fileread()

  原型:extern package int __fastcall fileread(int handle, void *buffer, int count);

 

  功能:读文件,返回实际读取的字节数,句柄必须首先由fileopen或filecreate创建.

  参数:handle:要读取的句柄;buffer:存放读取的数据的缓冲区;count:想读取的字节数

  例:char str[400];fileread(hnd1,str,400);

⑷fileseek()

  原型:extern package int __fastcall fileseek(int handle, int offset, int origin);

 

  功能:移动文件读取指针,成功返回文件指针的位置,失败返回-1

  参数:handle:相关联的句柄;offset:移动的量;orgin:移动的基准,0=文件头,1=当前位置,2=文件尾.

  例:showmessage(fileseek(hnd1,0,2));//取得文件的长度

⑸filewrite()

  原型:extern package int __fastcall filewrite(int handle, const void *buffer, int count);

 

  功能:写文件,返回实际写入的字节数,句柄必须首先由fileopen或filecreate创建.

  参数:handle:要写入的句柄;buffer:存放写入数据的缓冲区;count:想写入的字节数

  例:char str[]="i love you";filewrite(hnd1,str,strlen(str));

⑹filecreate()

  原型:extern package int __fastcall filecreate(const ansistring filename);

 

  功能:创建文件.成功返回其句柄,否则返回-1

  参数:filename:要创建的文件名

  例:if(!fileexists("kc.c"))hnd1=filecreate("kc.c");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值