How to read structured data from a file.

G

Guest

Hi,

I need to send data from a file into a structure. In Vb6 this was done like
so.

public Type Struct1
v1 as string
v2 as string
end type

dim s1 as Struct1

Open TxtPath For Random Access Read Shared As #1 Len = Len(s1)
Get #1, , s1
Close #1


How can this be done in VB.NET?

I tried something like this, however I cant seem to be able to convert my
buffer to a structure.

Public Structure Struct1
<VBFixedString(4)>public v1 as string
<VBFixedString(4)>public v2 as string
End Structure

dim s1 as Struct1

dim oFs as new filestream....

Dim oBr As New BinaryReader(oFs)
Dim oBuf() As Char
ReDim oBuf(Len(s1))
oBr.Read(oBuf, 0, Len(s1))

At this point i don't know how to convert my buffer to a Struct1 object ...


Thx for any input.
 
M

Mythran

Eric said:
Hi,

I need to send data from a file into a structure. In Vb6 this was done
like
so.

public Type Struct1
v1 as string
v2 as string
end type

dim s1 as Struct1

Open TxtPath For Random Access Read Shared As #1 Len = Len(s1)
Get #1, , s1
Close #1


How can this be done in VB.NET?

I tried something like this, however I cant seem to be able to convert my
buffer to a structure.

Public Structure Struct1
<VBFixedString(4)>public v1 as string
<VBFixedString(4)>public v2 as string
End Structure

dim s1 as Struct1

dim oFs as new filestream....

Dim oBr As New BinaryReader(oFs)
Dim oBuf() As Char
ReDim oBuf(Len(s1))
oBr.Read(oBuf, 0, Len(s1))

At this point i don't know how to convert my buffer to a Struct1 object
...


Thx for any input.

' Create a temporary structure array.
Dim structs As MyStruct() = New MyStruct() { _
New MyStruct("Name1", "Pass1"), _
New MyStruct("Name2", "Pass2"), _
New MyStruct("Name3", "Pass3") _
}
Dim st As MyStruct

' Write the structure array to a file.
Dim fileNo As Integer = FreeFile()
FileOpen( _
fileNo, _
"info.dat", _
OpenMode.Random _
)

Try
For i As Integer = 0 To structs.Length - 1
FilePut(fileNo, structs(i), i + 1)
Next

Array.Clear(structs, 0, structs.Length)

' Read the first record in the file.
FileGet(fileNo, st, 1)
Finally
FileClose(New Integer() { fileNo })
End Try

HTH,
Mythran
 
G

Guest

Ok thx this helped a lot. It is a much better idea to use the fileGet function.

I can read most of my structs now. But there is still one that can't be read.

When I use your method with an element with the <VBFixedString(x),
VBFixedArray(x)> attribute in the structure I receive the following error
when executing the fileget function : "Invalid record length. "

However I am absolutely certain that the record length used in the fileopen
function is correct. I even tried a loop to give my struct a record length
from 0 to 2000 and none of the lengths were correct.

Where is the problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top