Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Go Back  Xtreme Visual Basic Talk > > > Visual Basic.NET Binary File Access


Reply
 
Thread Tools Display Modes
  #1  
Old 03-28-2014, 03:12 PM
ahroonwa ahroonwa is offline
Newcomer
 
Join Date: Mar 2014
Posts: 2
Question 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?
Reply With Quote
  #2  
Old 03-29-2014, 04:27 PM
PlausiblyDamp's Avatar
PlausiblyDampVisual Basic.NET Binary File Access PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Is the problem in reading the file or getting it to work with your audio system?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 03-30-2014, 02:26 AM
Gruff's Avatar
GruffVisual Basic.NET Binary File Access Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

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
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 03-30-2014 at 02:32 AM.
Reply With Quote
  #4  
Old 03-30-2014, 06:20 PM
passel's Avatar
passelVisual Basic.NET Binary File Access passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

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
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 04-16-2014, 01:04 PM
ahroonwa ahroonwa is offline
Newcomer
 
Join Date: Mar 2014
Posts: 2
Default

Got it! Thanks.
Reply With Quote
Reply

Tags
vb file i/o, vb6 to .net


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
 
Visual Basic.NET Binary File Access
Visual Basic.NET Binary File Access
 
-->