在vbscript中逐字节读取文件

Read File byte after byte in vbscript

我正在寻找一种使用 VBScript(大 - 1 GB)读取大二进制文件的方法。我不能用 ReadAll 函数直接读取它,因为文件太大,所以我正在寻找一种循环读取它的方法,就像在 C 中一样。所以我想读取 X 字节,处理它们(我不需要完整的文件来完成我的工作),然后再阅读接下来的 10 个。

问题是我找不到办法做到这一点,我知道如何从偏移量开始读取,但找不到读取 X 字节的方法,只有 ReadAllReadLine 个函数。

有没有办法读取 X 字节?

如有疑问,请阅读 documentation:

Read Method

Reads a specified number of characters from a TextStream file and returns the resulting string.

Syntax

object.Read(characters)

Arguments

  • object
    Required. Always the name of a TextStream object.
  • characters
    Required. Number of characters you want to read from the file.
filename = "C:\path\to\your.file"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f   = fso.OpenTextFile(filename)

Do Until f.AtEndOfStream
    buf = f.Read(10)
    '...
Loop

f.Close

但是请注意,Read() 方法本身并不读取字节,而是读取字符。只要用ANSI模式(默认)打开文件就差不多了。