DELPHI文件操作建议(reset打开只读文件处理)

本文详细探讨了在使用Delphi进行文件操作时遇到的问题,特别是使用reset打开只读文件导致的数据不准确现象。文章指出,文件模式(FileMode)作为一个全局变量并不支持线程安全,可能导致在多线程环境下出现问题。为了防止此类问题,作者提供了几种推荐的文件操作函数,包括fileOpen/FileCreate用于打开或创建文件,fileSeek用于文件定位,fileRead/fileWrite用于文件读写,以及fileClose用于关闭文件。这些替代方法有助于避免直接使用reset函数带来的风险。

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

 使用reset打开只读文件,发现会出错,导致数据不准,查了下帮助,发现

In Delphi code, Reset opens the existing external file with the name assigned to F using the mode specified by the global FileMode variable. An error results if no existing external file of the given name exists or if the file can't be opened with the current file mode. If F is already open, it is first closed and then reopened. The current file position is set to the beginning of the file.

Warning: The default value of FileMode is 2 ( Read/ Write access). If this is not changed to a read-only file mode before calling Reset, attempts to open read-only files will fail.

结果发现fileMode居然是sysutils的全局变量,悲剧呀;想修改fileMode的值再改回去,查帮助发现FileMode 居然不支持线程;

Warning: FileMode is declared as a variable, not as a threadvar. Therefore this variable is not thread-safe.

 

因此,建议delphi的用户们使用以下为文件函数操作:

1)fileOpen/FileCreate:打开/创建文件

2)fileSeek:移动文件

3)fileRead/fileWrite:读文件

4)fileClose:关闭文件

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值