FileLib.PRG -- VFP 文件基础函数库

FileLib.PRG 是一个Visual FoxPro(VFP)的文件基础函数库,包含了文件的打开、关闭、拷贝、移动、获取长度、行数等常用操作。该库通过宏定义、文件处理函数、文件读写等功能,简化了对文件的操作。例如,fileOpen 函数用于打开文件,fileClose 用于关闭文件,copyFile 和 moveFile 分别实现文件的复制和移动,而 getFileSizes 和 getFileLines 则用于获取文件的大小和行数。
摘要由CSDN通过智能技术生成
* FileLib.PRG -- VFP 文件基础函数库
* 
* 代码编写: fireghost57
* 维护日期: 2013.11.05
* 
*----------------------------------------------------------------------------* 
* 
* 宏定义
* 
*----------------------------------------------------------------------------*
#Define 	LINE_MAX_LENGTH 	1024
*----------------------------------------------------------------------------* 
* 
* 文件处理函数
* 
*----------------------------------------------------------------------------*
* 打开文件函数
FUNCTION fileOpen(lcFileName)
	local lnHandle
	
	if file(lcFileName)
		lnHandle = fopen(lcFileName, 12)			&& 如果存在,以读写方式打开
	else
		lnHandle = fcreate(lcFileName)
	endif
	
	if lnHandle < 0
		wait "cannot open or create file" window nowait
	endif
	* modify file &lcFileName nowait				&& 在编辑窗口中打开文件
	
	return lnHandle
ENDFUNC

*----------------------------------------------------------------------------*
* 关闭文件函数
FUNCTION fileClose(lnHandle)
	fclose(lnHandle)								&& 关闭该文件 
ENDFUNC

*----------------------------------------------------------------------------*
* 文件拷贝
FUNCTION copyFile(lcFileName,lcCopyFileName)
	oFSO = CreateObject("Scripting.FileSystemObject")
	lbOverWrite = .T.
	
	TRY
		oFSO.CopyFile(lcFileName, lcCopyFileName, lbOverWrite)
		lbResult = .T.
	CATCH
		lbResult = .F.
	ENDTRY
	
	return lbResult
ENDFUNC

*----------------------------------------------------------------------------*
* 文件移动/改名
FUNCTION moveFile(lcFileName,lcNewFileName)
	oFSO = CreateObject("Scripting.FileSystemObject")
	
	TRY
		oFSO.MoveFile(lcFileName, lcNewFileName)
		lbResult = .T.
	CATCH
		lbResult = .F.
	ENDTRY
	
	return lbResult
ENDFUNC

*----------------------------------------------------------------------------*
* 获得文件长度
* 既可使用文件句柄又可使用文件名
FUNCTION getFileSizes(lvFileName)
	local	lbIsHandle,lbError,;
			lnHandle,;
			lnResult
			
	* 获取文件句柄
	lbError = .F.
	try
		* 处理数字
		lbIsHandle = .T.
		lnHandle = val(alltrim(str(lvFileName)))
	catch
		* 处理字符
		lbIsHandle = .F.
		if file(lvFileName)
			lnHandle = fopen(lvFileName, 12)
			if lnHandle < 0
				* 打开失败
				lbError = .T.
				lnResult = -2
			endif
		else
			* 文件不存在
			lbError = .T.
			lnResult = -1
		endif
	endtry
	
	* 处理内容
	if not lbError
		local lnftail,lnfhead
		
		if lbIsHandle
			* 处理数字
			local lnCurPos
			
			lnCurPos = fseek(lnHandle, 0, 1)		&& 保存当前指针位置
			lnftail = fseek(lnHandle, 0, 2)			&& 移动指针到文件末
			lnfhead = fseek(lnHandle, 0, 0)			&& 移动指针到文件头
			fseek(lnHandle, lnCurPos, 0)			&& 恢复当前指针位置
		else
			* 处理字符
			lnftail = fseek(lnHandle, 0, 2)			&& 移动指针到文件末
			lnfhead = fseek(lnHandle, 0, 0)			&& 移动指针到文件头
			fclose(lnHandle)
		endif
		
		lnResult = lnftail - lnfhead
	endif
	
	return lnResult
ENDFUNC

*----------------------------------------------------------------------------*
* 获得文件行数
* 既可使用文件句柄又可使用文件名
FUNCTION getFileLines(lvFileName)
	local	lbIsHandle,lbError,;
			lnHandle,;
			lnResult
			
	* 获取文件句柄
	lbError = .F.
	try
		* 处理数字
		lbIsHandle = .T.
		lnHandle = val(alltrim(str(lvFileName)))
	catch
		* 处理字符
		lbIsHandle = .F.
		if file(lvFileName)
			lnHandle = fopen(lvFileName, 12)
			if lnHandle < 0
				* 打开失败
				lbError = .T.
				lnResult = -2
			endif
		else
			* 文件不存在
			lbError = .T.
			lnResult = -1
		endif
	endtry
	
	* 处理内容
	if not lbError
		local lnCount
		
		lnCount = 0
		if lbIsHandle
			* 处理数字
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值