...

View Full Version : VB: Opening a binary file?



bobleny
08-08-2009, 08:54 AM
Hey, I have another question!

I have a binary file with a .rnd extension. I need to read the records. In QBASIC, it could be done like so:


OPEN fileLocation$ FOR BINARY AS #1
GET #1, recordNumber%, aDataStructure
CLOSE 1


From what I can tell, in VB this is how you would open a file:


Dim fileStream As New IO.FileStream(fileLocation, IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.None)

Dim file As New IO.StreamReader(fileStream)

Dim records As String = file.ReadToEnd()

fileStream.Close()


Though this opens the file, I can only seem to read the first line... Maybe it's just me, but this seems to also be a bit more complicated than in QBASIC.

When I look up how to open a binary file in VB online, everyone says the same thing,


OPEN fileLocation$ FOR BINARY AS #1
GET #1, recordNumber%, aDataStructure
CLOSE 1


However, when I type that in, "Open" just doesn't exist...

Is anyone able to help me out? I don't understand.

Thanks!

RJackson
08-08-2009, 06:06 PM
It sounds like you're using VB.NET?
What you're getting from searching is VB, the two are different languages.


Dim fileToOpen as New FileStream("file.txt",FileMode.Open,FileAccess.Read)
Dim fileToRead as new StreamReader(FileToOpen)
FileToRead.BaseStream.Seek(0,SeekOrigin.Begin)
while FileToRead.peek()>-1
outputobject.text &= FileToRead.ReadLine()
End While

That should do the job.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum