Author Topic: FilePut Problem  (Read 3543 times)

0 Members and 1 Guest are viewing this topic.

dukesys

  • Guest
FilePut Problem
« on: April 29, 2007, 11:42:52 PM »
Hello All,

I have a very annoying little problem with VB .NET and was wondering if anyone could cast some light on it.

I am opening and reading a file using random access, I have defined the structure I am reading in.  I can read the data in, but when I write out the data the final 40 bytes or so do not get updated until I close the file.  This is annoying as I update the file as part of a design process and need to read the data after I have written it.  the data consists of an array of 50 items, i can update the second last entry no problem but the majority of the data in the last entry is not filed to disk until the file is closed.  Here is a cut down version of the code in a test example.

Public Class Form1
    Dim filRanR As Short
    Dim filRanW As Short
    Dim filBinR As Short
    Dim filBinW As Short
    Dim modelfilename As String = "c:\conference\model1.fil"
    Dim RecordSize As Integer = 2016


    Public Structure StringIdx
        <VBFixedString(4)> Public Label As String
        <VBFixedString(4)> Public SubRef As String
        Dim StringType As Integer
        Dim NumPoints As Integer
        <VBFixedArray(3)> Dim enBox() As Integer
        Dim RecordNum As Integer
        Dim Location As Integer

        Public Sub Initialize()
            ReDim enBox(3)
        End Sub
    End Structure

    Private Structure stringIdxRec
        <VBFixedArray(49)> Dim stringEntry() As StringIdx
        Dim nextIdxRecNum As Integer
        Dim nextFileRecNum As Integer
        Dim fluff1 As Integer
        Dim fluff2 As Integer

        Public Sub Initialize()
            Dim i0 As Integer

            ReDim stringEntry(49)
            For i0 = 0 To 49
                ReDim stringEntry(i0).enBox(3)
            Next
        End Sub
    End Structure

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tempStringRec As New stringIdxRec
        Dim t1 As New stringIdxRec

        tempStringRec.Initialize()
        t1.Initialize()

        openRead()
        openWrite()
        FilePut(filRanW, t1, 1)
        FileGet(filRanW, tempStringRec, 1)
        tempStringRec.stringEntry(48).NumPoints = 1
        tempStringRec.stringEntry(48).RecordNum = 1
        tempStringRec.stringEntry(48).SubRef = "wwww"

        tempStringRec.stringEntry(49).NumPoints = 5
        tempStringRec.stringEntry(49).RecordNum = 5
        tempStringRec.stringEntry(49).SubRef = "gggg"
        tempStringRec.stringEntry(49).StringType = 777712
        tempStringRec.stringEntry(49).enBox(0) = 10
        tempStringRec.stringEntry(49).enBox(1) = 20
        tempStringRec.stringEntry(49).enBox(2) = 30
        tempStringRec.stringEntry(49).enBox(3) = 40
        tempStringRec.fluff1 = 777

        FilePut(filRanW, tempStringRec, 1)
        FileGet(filRanR, t1, 1)
        closeWrite()
        closeRead()

    End Sub
    Public Function openRead() As Integer
        Dim retval As Integer

        On Error GoTo badopen
        filRanR = FreeFile()
        FileOpen(filRanR, modelfilename, OpenMode.Random, OpenAccess.Read, _
            OpenShare.Shared, RecordSize)
        filBinR = FreeFile()
        FileOpen(filBinR, modelfilename, OpenMode.Binary, OpenAccess.Read, _
            OpenShare.Shared)
badopen:
        On Error GoTo 0
        openRead = retval
    End Function
    Public Sub closeRead()

        On Error Resume Next
        FileClose(filRanR)
        FileClose(filBinR)
        filRanR = 0
        filBinR = 0
        On Error GoTo 0
    End Sub

    Public Function openWrite() As Integer
        Dim retval As Integer

        On Error GoTo badopen
        filRanW = FreeFile()
        FileOpen(filRanW, modelfilename, OpenMode.Random, , _
            OpenShare.Shared, RecordSize)
        filBinW = FreeFile()
        FileOpen(filBinW, modelfilename, OpenMode.Binary, , OpenShare.Shared)
badopen:
        On Error GoTo 0
        openWrite = retval
    End Function
    Public Sub closeWrite()

        On Error Resume Next
        FileClose(filRanW)
        FileClose(filBinW)
        filRanW = 0
        filBinW = 0
        On Error GoTo 0
    End Sub
End Class


The test example defines a form with a button,  when the button is pressed a blank record is written to disk and then read back into a different variable.  I then change a bunch of values and write the data back to disk.  I then read the values back into a variable (t1).  If execution is halted at this point and the variable t1 is inspected it can be seen that the info that should have been written out is not present.  In fact the only way to fully update the record is to close the file which I do not want to do, yet.  and yes I know some of the code is a little dodgy.


Any ideas would be greatly appreciated.


Kind Regards

Martin Duke.

Glenn R

  • Guest
Re: FilePut Problem
« Reply #1 on: April 29, 2007, 11:56:37 PM »
Call Flush on the stream or wherever it is...can't remember...otherwise it sits in the buffer.

dukesys

  • Guest
Re: FilePut Problem
« Reply #2 on: April 30, 2007, 12:00:10 AM »
Call Flush on the stream or wherever it is...can't remember...otherwise it sits in the buffer.

Hi Glenn,

I don't think you can flush as it is not a conventional filestream, it's an old skool random access file.


Martin.

Glenn R

  • Guest
Re: FilePut Problem
« Reply #3 on: April 30, 2007, 12:16:19 AM »
Have you tried using the .net file stream classes ie. BinaryReader and BinaryWriter?

dukesys

  • Guest
Re: FilePut Problem
« Reply #4 on: April 30, 2007, 12:22:26 AM »
Have you tried using the .net file stream classes ie. BinaryReader and BinaryWriter?

Hello Glenn,

Because of the nature of the existing file format it doesn't suit the binary reader methods, I wish it did as it would solve a great deal of problems.


Cheers

Martin.

TonyT

  • Guest
Re: FilePut Problem
« Reply #5 on: May 03, 2007, 01:56:51 PM »
BinaryReader on a FileStream supports random file/IO via the
Seek() method, which is really all you need to do random
file IO.

Have you tried using the .net file stream classes ie. BinaryReader and BinaryWriter?

Hello Glenn,

Because of the nature of the existing file format it doesn't suit the binary reader methods, I wish it did as it would solve a great deal of problems.


Cheers

Martin.