写入随机文件只为第一个记录写入不正确的文件长度

发布于 2024-12-20 04:40:10 字数 1045 浏览 2 评论 0原文

注意 - 我从头开始重写了这个问题以简化问题...

以下函数将记录写入二进制文件:

Public Type TestRecord
    Available As Boolean
    Inidcator As String
End Type

Private Sub WriteTestRecord(ByVal RecCount As Integer)

    Dim Fn As Integer, CN As Integer
    Dim filename As String
    Dim EmpRec As TestRecord
    Dim clk() As TestRecord
    ReDim clk(1 To RecCount)

    Debug.Print Len(EmpRec)

    filename = "C:\TestRecFile.bin"

    If Len(Dir(filename)) > 0 Then Kill filename

    Fn = FreeFile
    Open filename For Random As #Fn Len = Len(EmpRec)
    For CN = 1 To RecCount
        EmpRec = clk(CN)
        Put #Fn, , EmpRec
    Next CN
    Close #Fn

End Sub

请注意 Len(EmpRec) = 6 code> 单步执行此代码时

如果我调用该函数并写出一条记录 - 它仅写出 4 个字节:

Call WriteTestRecord(1) '文件长度为 4 个字节

如果我调用该函数并写入输出不止一条记录 - 它写道out (RecCount*6) - 2 bytes:

Call WriteTestRecord(10) ' file length is 58 bytes

这是因为我有一个非固定长度的字符串在我的类型中,但为什么第一个记录的长度与所有其他记录的长度不同?

Note - I have re-written this question from scratch to simplify the problem...

The following function writes out a record to a binary file:

Public Type TestRecord
    Available As Boolean
    Inidcator As String
End Type

Private Sub WriteTestRecord(ByVal RecCount As Integer)

    Dim Fn As Integer, CN As Integer
    Dim filename As String
    Dim EmpRec As TestRecord
    Dim clk() As TestRecord
    ReDim clk(1 To RecCount)

    Debug.Print Len(EmpRec)

    filename = "C:\TestRecFile.bin"

    If Len(Dir(filename)) > 0 Then Kill filename

    Fn = FreeFile
    Open filename For Random As #Fn Len = Len(EmpRec)
    For CN = 1 To RecCount
        EmpRec = clk(CN)
        Put #Fn, , EmpRec
    Next CN
    Close #Fn

End Sub

Note that Len(EmpRec) = 6 when stepping through this code

If I call the function and write out one record - it writes out only 4 bytes:

Call WriteTestRecord(1) 'file length is 4 bytes

If I call the function and write out more than one record - it writes out (RecCount*6) - 2 bytes:

Call WriteTestRecord(10) ' file length is 58 bytes

This is caused by the fact that I have a non-fixed length string in my Type but why is the the first record a different length than all the others?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2024-12-27 04:40:10

不是第一条记录错误,而是最后一条记录错误。

如果在每次写入并查看文件之前将“可用”设置为“真”,则可以看到这一点。

您的 UDT 只有 4 个字节,因此第一次写入时文件中有 4 个字节。但是,当您写入第二条记录时,VB 首先将文件中的 Rec#1 用空值填充到您在 Open 中提供的 6 字节长度。然后写入 4 字节记录 (rec #2)。每次写入时都会重复此过程。最后的记录总是很短。

有趣的是,Close 并没有将最后一条记录的 4 字节填充到 6 字节。

It is not that thr first record is wrong, it is that the last record is wrong.

You can see this if you set Available to True just before each write and look at the file.

Your UDT is only 4 bytes, so on the first write you have 4 bytes in the file. But then when you write the 2nd record, VB is first padding Rec#1 in the file with nulls to the 6 byte length you supplied in the Open. It then writes the 4 byte record (rec #2). This process is repeated on every write. It is always the last record that is short.

What is interesting is that the Close does not also pad the last record of 4 byte upto 6 bytes.

巾帼英雄 2024-12-27 04:40:10

VB6 可变长度字符串是指向 BSTR 结构的指针。根据语言规范,它们可以为 NULL。这意味着类型定义中的指针值将为零。我可以看到 VB6 做了一些事情,比如在写出这些字节时跳过这些字节。

正如您在编辑的问题中提到的,如果您要将结构写入随机访问文件,正确的答案是使用固定长度的字符串。

VB6 variable length strings are pointers to BSTR structures. According to the language spec, they can be NULL. That means the pointer value in the type definition would be zero. I can see VB6 doing something like skipping those bytes when writing it out.

As you mention in your edited question, the correct answer is to use fixed length strings if you're going to write the structure out to a random access file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文