|
-
Feb 6th, 2007, 12:11 PM
#1
Thread Starter
Junior Member
Writing structure to a text file
I am a long-time vb6 programmer, but new to VB2005.
I have the following declarations and code:
Private Structure IndividualType
Dim Inum As Integer ' GEDCOM number assigned to this individual
Dim Inam As String ' name
Dim Isex As String ' sex M or F
Dim Ibdt As String ' birth date Jan 12, 1789
Dim Ibpl As Integer ' pointer to birth place in .STR file
Dim Iddt As String ' death date
Dim Idpl As Integer ' pointer to death place in .STR file
Dim Irdt As String ' burial date
Dim Irpl As Integer ' pointer to burial place in .STR file
Dim Inot As Integer ' pointer to note in .STR file
Dim Ifamsp() As Short ' numbers of family in which he is a spouse
Dim Ifamch As Integer ' number of in which he/she is a child
Dim Ifamch2 As Integer ' sometimes in GED file, points to family with no HUSB or WIFE
Public Sub Initialize()
ReDim Ifamsp(maxSpouse)
End Sub
End Structure
Dim Ind() As IndividualType
Dim Ind2() As IndividualType
Dim nInd, IndBiggest As Integer
Private Structure FamilyType
Dim Fnum As Integer ' GEDCOM number assigned to this family
Dim Fhus As Integer ' number of the husband, 0 if none
Dim Fwif As Integer ' number of the wife
Dim Fmdt As String ' marriage date
Dim Fmpl As Integer ' pointer to marriage place in .STR file
Dim Fnot As Integer ' pointer to note about family in .STR file
Dim Fnch As Integer ' count of children
Dim Fchl() As Integer ' numbers assigned to children
Public Sub Initialize()
ReDim Fchl(maxChildren)
End Sub
End Structure
Ffile = FreeFile()
FileOpen(Ffile, txtFLXpath.Text & txtFLXfile.Text, OpenMode.Output)
For I = 1 To IndBiggest
If Ind(I).Inum > 0 Then
With Ind(I)
WriteLine(Ffile, "I", .Inum, RTrim(.Inam), .Isex, RTrim(.Ibdt), .Ibpl, RTrim(.Iddt), .Idpl, RTrim(.Irdt), .Irpl, .Inot, .Ifamsp(1), .Ifamsp(2), .Ifamsp(3), .Ifamsp(4), .Ifamsp(5), .Ifamsp(6), .Ifamsp(7), .Ifamsp(8), .Ifamch, .Ifamch2)
End With
End If
Next I
For I = 1 To FamBiggest
With Fam(I)
If .Fnch = 0 Then
WriteLine(Ffile, "F", .Fnum, .Fhus, .Fwif, .Fmdt, .Fmpl, .Fnot, .Fnch)
Else
Write(Ffile, "F", .Fnum, .Fhus, .Fwif, .Fmdt, .Fmpl, .Fnot, .Fnch)
If .Fnch = 1 Then
WriteLine(Ffile, .Fchl(1))
Else
For J = 1 To .Fnch - 1
Write(Ffile, .Fchl(J))
Next J
WriteLine(Ffile, .Fchl(.Fnch))
End If
End If
End With
Next I
FileClose(Ffile)
The problem:
The first loop writes absolutely NOTHING to the file
but the second loop write the data to the file correctly.
I have verified that the Ind() array contains the correct data by writing it into a textbox.
Why is Ind() not getting to the Ffile?
-
Feb 6th, 2007, 03:40 PM
#2
Frenzied Member
Re: Writing structure to a text file
You may want to try using Serialization if you want to write your structure objects to a file. This basically writes your objects to binary file.
Serialize would be the writing part, and deserialize would be the reading part
VB Code:
Imports System.Runtime.Serialization.Formatters.Binary
<Serializable()> _ 'must put this line for it to work
Public Structure class1
Public num1 As Integer
Public str As String
Public num2 As Integer
End Structure
Public Sub Serialize()
Dim obj As New class1
obj.num1 = 9
obj.str = "b"
obj.num2 = 8
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream("c:\serialize.dat", IO.FileMode.Create) 'it will be written to serialize.dat
Dim bf As New BinaryFormatter
bf.Serialize(fs, obj)
fs.Close()
End Sub
Public Sub Deserialize()
Dim obj As New class1
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream("c:\serialize.dat", IO.FileMode.Open) 'it will read from serialize.dat
Dim bf As New BinaryFormatter
obj = CType(bf.Deserialize(fs), class1)
fs.Close()
End Sub
-
Feb 6th, 2007, 03:50 PM
#3
Frenzied Member
Re: Writing structure to a text file
You could also substitute "System.Runtime.Serialization.Formatters.Soap.SoapFormatter" for BinaryFormatter. This would write it in xml form, instead of binary.
-
Feb 9th, 2007, 01:07 PM
#4
Thread Starter
Junior Member
Re: Writing structure to a text file
I finally got the program to work using Writeline.
The problem was that I was never setting the dimension of the array Ifamsp() which is an element in the structure.
Apparently this was allowing the data to be corrupted.
But I was never able to get the Serialization to write anything to the file. No error messages - just didn't put anything in the file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|