首页 /编程语言和算法/VB6/VBA/ASP
 VB6 奇怪的LOF,实验结果
2025年3月5日 14:54

新建From1(窗体),新建Command1(按钮CommandButton),代码:

Private Sub Command1_Click()
    Dim strFN As String
    strFN = "C:\Windows\notepad.exe"
    
    Open strFN For Input As #1
        MsgBox "Input方式得到文件大小:" & LOF(1)
    Close #1
    
    Dim byteArray() As Byte
    
    Open strFN For Binary As #1
        MsgBox "Binary方式得到文件大小:" & LOF(1)
        Seek #1, 1    '移动到指定位置,文件位置从 1 开始
        Get #1, , byteArray    '读取 N 个字节到字节数组
    Close #1
End Sub

在VB6IDE中运行:

Input方式得到文件大小:202240
Binary方式得到文件大小:0

生成exe运行:

Input方式得到文件大小:202240
Binary方式得到文件大小:202240

而改成不是Windows自带的exe,比如:

strFN = "C:\Windows\py.exe"

在IDE中就能得到正确的大小。

另外,Seek语句可以不读取全部文档,直接定位到指定位置读取,速度超快。

 
全部回复(0)
首页 | 电脑版 |