How to read from a file into a structure?

  • Thread starter Thread starter Alejandro Lapeyre
  • Start date Start date
A

Alejandro Lapeyre

I am writing a class to wrap a RIFF file, so i have to read and decode some
structured data on the file.

For example, in the begining of file is a header:

Public Structure RiffHeader
Public id As Int32 ' (a FOURCC actually)
Public fileSize As Int32
Public fileType As Int32
End Structure

How can a I read from the file into a RiffHeader structure?



Regards, Alejandro Lapeyre
 
Public Sub GetHeaderInfo(ByRef myHeader As RiffHeader)
Dim myFile As Integer
Dim i As Integer

myFile = FreeFile
Open "aFileName.riff" For Random Access Read _
As myFile Len = len(RiffHeader) // or is it sizeOf()?
Get myFile, 1, myHeader
Close myFile
End Sub
 
* said:
Public Sub GetHeaderInfo(ByRef myHeader As RiffHeader)
Dim myFile As Integer
Dim i As Integer

myFile = FreeFile
Open "aFileName.riff" For Random Access Read _
As myFile Len = len(RiffHeader) // or is it sizeOf()?
Get myFile, 1, myHeader
Close myFile
End Sub

That's VB6 code, isn't it? In VB.NET you will have to use 'FileOpen',
'FileGet', 'FileClose' and so on.
 
Thank you for your time.
I am looking for the ".NET way" to do it. (no VB specifics libraries).

I am using IO.FileStream and it reads into a Bytes() as byte buffer. How can
i move this bytes into a structure?
Do I have to look in serialization?

Thank you, again. :-)

Regards,
Alejandro Lapeyre
 
Thanks for your time!

I was looking for a "standard .NET way" to do it.

Finaly, i have a working class using a FileStream and a BinaryReader, I fill
the structure using the BinaryReader.ReadInt32 and ReadChar methods.

Is there any other way to fill a structure from a some Bytes()?

Regards,
Alejandro Lapeyre
 
* "Alejandro Lapeyre said:
I was looking for a "standard .NET way" to do it.

Finaly, i have a working class using a FileStream and a BinaryReader, I fill
the structure using the BinaryReader.ReadInt32 and ReadChar methods.

Is there any other way to fill a structure from a some Bytes()?

I don't think so. But why not use VB.NET's methods for doing that?
 
Back
Top