By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

How to read structured data from a file.

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Eric" <Er**@discussions.microsoft.com> wrote in message
news:B4**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
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?

"Mythran" wrote:

"Eric" <Er**@discussions.microsoft.com> wrote in message
news:B4**********************************@microsof t.com...
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.