Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET File I/O and Registry (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/)
-   -   Visual Basic.NET Binary File Access (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/326951-visual-basic-net-binary-file-access.html)

ahroonwa 03-28-2014 03:12 PM

Visual Basic.NET Binary File Access
 
I am going absolutely nuts moving from VB.6 to VB.net.

I am trying to read a binary file without success.

The VB6 code was:

strStimulusFile = "000000.BIN"
Open strStimulusFile For Binary Access Read As #1 │
Get #1, , intSignal │
Close #1

[The file is from a speech corpus developed at the Air Force Research Laboratory and described in “A speech corpus for multitalker communications research” by Robert S. Bolia, W. Todd Nelson, Mark A. Ericson and Brian D. Simpson, J. Acoust. Soc. Am. 107, 1065 (2000); http://dx.doi.org/10.1121/1.428288)]

I want to read the file and then play it through my audio system (using Tucker-Davis Technologies (www.tdt.com) gear. I can e-mail a copy of the file if needed.

Can anyone assist?

PlausiblyDamp 03-29-2014 04:27 PM

Is the problem in reading the file or getting it to work with your audio system?

Gruff 03-30-2014 02:26 AM

ahroonwam.

As the brilliant PD stated we are uncertain as to your question.

If you are only needing help reading the file then the following should shed some light.

Under VB6 you could read and write to a binary file pretty much as you posted though
I would have used Option Explicit and defined my variables first.
Code:
Option Explicit Private Sub Command1_Click() Dim strStimulusFile As String Dim IntSignal As Integer strStimulusFile = "c:\000000.BIN" IntSignal = 32000 Open strStimulusFile For Binary Access Write As #1 Put #1, , IntSignal Close #1 End Sub Private Sub Command2_Click() Dim strStimulusFile As String Dim IntSignal As Integer strStimulusFile = "c:\000000.BIN" Open strStimulusFile For Binary Access Read As #1 Get #1, , IntSignal Close #1 MsgBox IntSignal End Sub

Under VB.NET you have more options.
Miicrosoft provided the VisualBasic namesspace to make VB6 users more comfortable.
The namespace provides many VB6 Tools you are used to seeing. Some look somewhat different
as they have been rewritten as objects. You can use them in a pinch, but it is recommended
you learn to use the VB.NET File methods instead.

Note: I am assuming your binary file contains an older 16 bit Integer datatype so the Integer
variable i show in the VB.NET examples use that type.
Code:
Imports Microsoft.VisualBasic Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim strStimulusFile As String = "c:\000000.BIN" Dim IntSignal As Int16 = IntSignal = 25 FileOpen(1, strStimulusFile, OpenMode.Binary, OpenAccess.Write) FilePut(1, IntSignal) FileClose(1) End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim strStimulusFile As String = "c:\000000.BIN" Dim IntSignal As Int16 = 0 FileOpen(1, strStimulusFile, OpenMode.Binary, OpenAccess.Read) FileGet(1, IntSignal) FileClose(1) MessageBox.Show(IntSignal) End Sub

The proper way to read and write binary files in VB.NET is to import the System.IO namespace
instead and use its BinaryReader and BinaryWriter objects.
Code:
Imports System.IO Public Class Form1 Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click Dim strStimulusFile As String = "c:\000000.BIN" Dim IntSignal As Int16 = 77 Using Writer As BinaryWriter = New BinaryWriter(File.Open(strStimulusFile, FileMode.OpenOrCreate)) Writer.Write(IntSignal) Writer.Close() End Using End Sub Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click Dim strStimulusFile As String = "c:\000000.BIN" Dim IntSignal As Integer = 0 If File.Exists(strStimulusFile) Then Using reader As BinaryReader = New BinaryReader(File.Open(strStimulusFile, FileMode.Open)) IntSignal = reader.ReadInt16() reader.Close() End Using End If MessageBox.Show(IntSignal) End Sub End Class

passel 03-30-2014 06:20 PM

In .Net there is often quite a few ways to do something, and while Gruff has given a more complete example than I will, I'll show my first thoughts and approach.
The VB6 code seems like it would only make sense if intSignal was an array of (vb6) integer, pre-sized to hold the file being read.
So, along those lines, my example reads all the bytes of the file, then transfers those bytes into an array of 16bit integers.
Code:

Public Class Form1
  Dim intSignal() As Short 'or int16 or uint16 or ushort)

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim b() As Byte = IO.File.ReadAllBytes("c:\000000.bin") 'Read the file into an array of bytes
    Dim hs As Integer = b.Length \ 2                        'how many 16 bit ints
    ReDim intSignal(hs)                                    'redim the intSignal array to hold the data
    'just in case the number of bytes was odd, \2 will round down so use hs*2 dropping the last odd byte
    Buffer.BlockCopy(b, 0, intSignal, 0, hs * 2)            'copy the bytes into the intSignal array
  End Sub
End Class


ahroonwa 04-16-2014 01:04 PM

Got it! Thanks.


All times are GMT -6. The time now is 07:44 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.