From: Saga on
Hello all. First a quick sidenote thanking all who have helped me
embark down the .NET path.

At this time I need to write some data into a binary file, specifically:

Private Structure typBinFileHeader
Dim Hdr1 As Byte
Dim Hdr2 As Byte
Dim Option1 As Byte
Dim Option2 As Byte
Dim FileDate As Integer
Dim xFlags1 As Byte
Dim xFlags2 As Byte
End Structure

Dim hdrData as typBinFileHeader

Dim bytBuffer() As Byte 'File data
Dim lngSize As Integer 'Footer
Dim lngSize2 As Integer 'Footer
Dim strFileName As String

I started with the following code which works ok:
(Assume all data has been correctly placed in defined variables)

fh = FreeFile()
FileOpen(fh, strOutFile, OpenMode.Binary)

'Write out the header.
FilePut(fh, hdrData)

'Write out the file name, null terminated.
FilePut(fh, strFileName & Chr(0))

'The file data.
FilePut(fh, bytBuffer)

'The footer.
FilePut(fh, lngSize1)
FilePut(fh, lngSize2)

FileClose(fh)

After doing some research I found that the above technique has been (or
will be) deprecated and alternate methods are suggested. After doing
still more research I built the following routine:

Dim fsStream As System.IO.FileStream
Dim bwWriter As System.IO.BinaryWriter
Dim bytTemp() As Byte
Dim encoding As New System.Text.ASCIIEncoding()

fsStream = New System.IO.FileStream(strOutFile, _
IO.FileMode.Create, IO.FileAccess.Write, _
IO.FileShare.ReadWrite)

bwWriter = New System.IO.BinaryWriter(fsStream)

bwWriter.Write(hdrData)

'This out
' bwWriter.Write(strFileName & chr(0))

'This in
bytTemp = encoding.GetBytes(strFileName & Chr(0))
bwWriter.Write(bytTemp)

bwWriter.Write(bytBuffer)

bwWriter.Write(lngSize1)
bwWriter.Write(lngSize2)

bwWriter.Close()

fsStream.Close()
fsStream.Dispose()

The above did not work. It seems that the Write() method cannot
accept a structure as a parameter, so I broke it down to its
individual components:

With hdrData
bwWriter.Write(.Hdr1)
bwWriter.Write(.Hdr2)
bwWriter.Write(.Option1)
bwWriter.Write(.Option2)
bwWriter.Write(.FileDate)
bwWriter.Write(.xFlags1)
bwWriter.Write(.xFlags2)
End With

That worked ok, but the resulting file still had an error. After
generating a few samples and analyzing them with debug I
found out that when Write() is given a string it first writes
the string length and then the string, so I converted the
string into a byte array, wrote that out and that worked
as expected. The above code reflects the changes.

Is there a better way of doing the write to the binary file?
I liked the simplicity of using the fileput statements; however,
if it is advised to not use this technique I will heed said
advice, but if it works and is good enough then I will
leave that code in. Thanks again! Saga



From: Mr. Arnold on
Saga wrote:
> Hello all. First a quick sidenote thanking all who have helped me
> embark down the .NET path.
>
> At this time I need to write some data into a binary file, specifically:
>
> Private Structure typBinFileHeader
> Dim Hdr1 As Byte
> Dim Hdr2 As Byte
> Dim Option1 As Byte
> Dim Option2 As Byte
> Dim FileDate As Integer
> Dim xFlags1 As Byte
> Dim xFlags2 As Byte
> End Structure
>
> Dim hdrData as typBinFileHeader
>
> Dim bytBuffer() As Byte 'File data
> Dim lngSize As Integer 'Footer
> Dim lngSize2 As Integer 'Footer
> Dim strFileName As String
>
> I started with the following code which works ok:
> (Assume all data has been correctly placed in defined variables)
>
> fh = FreeFile()
> FileOpen(fh, strOutFile, OpenMode.Binary)
>
> 'Write out the header.
> FilePut(fh, hdrData)
>
> 'Write out the file name, null terminated.
> FilePut(fh, strFileName & Chr(0))
>
> 'The file data.
> FilePut(fh, bytBuffer)
>
> 'The footer.
> FilePut(fh, lngSize1)
> FilePut(fh, lngSize2)
>
> FileClose(fh)
>
> After doing some research I found that the above technique has been (or
> will be) deprecated and alternate methods are suggested. After doing
> still more research I built the following routine:
>
> Dim fsStream As System.IO.FileStream
> Dim bwWriter As System.IO.BinaryWriter
> Dim bytTemp() As Byte
> Dim encoding As New System.Text.ASCIIEncoding()
>
> fsStream = New System.IO.FileStream(strOutFile, _
> IO.FileMode.Create, IO.FileAccess.Write, _
> IO.FileShare.ReadWrite)
>
> bwWriter = New System.IO.BinaryWriter(fsStream)
>
> bwWriter.Write(hdrData)
>
> 'This out
> ' bwWriter.Write(strFileName & chr(0))
>
> 'This in
> bytTemp = encoding.GetBytes(strFileName & Chr(0))
> bwWriter.Write(bytTemp)
>
> bwWriter.Write(bytBuffer)
>
> bwWriter.Write(lngSize1)
> bwWriter.Write(lngSize2)
>
> bwWriter.Close()
>
> fsStream.Close()
> fsStream.Dispose()
>
> Is there a better way of doing the write to the binary file?
> I liked the simplicity of using the fileput statements; however,
> if it is advised to not use this technique I will heed said
> advice, but if it works and is good enough then I will
> leave that code in. Thanks again! Saga
>
>
>
http://msdn.microsoft.com/en-us/library/2bw4h516.aspx