Results 1 to 4 of 4

Thread: Writing structure to a text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    24

    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?

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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:
    1. Imports System.Runtime.Serialization.Formatters.Binary
    2.  
    3. <Serializable()> _    'must put this line for it to work
    4.     Public Structure class1
    5.         Public num1 As Integer
    6.         Public str As String
    7.         Public num2 As Integer
    8.     End Structure
    9.  
    10. Public Sub Serialize()
    11.         Dim obj As New class1
    12.         obj.num1 = 9
    13.         obj.str = "b"
    14.         obj.num2 = 8
    15.  
    16.         Dim fs As System.IO.FileStream
    17.         fs = New System.IO.FileStream("c:\serialize.dat", IO.FileMode.Create) 'it will be written to serialize.dat
    18.         Dim bf As New BinaryFormatter
    19.         bf.Serialize(fs, obj)
    20.         fs.Close()
    21.     End Sub
    22.     Public Sub Deserialize()
    23.         Dim obj As New class1
    24.         Dim fs As System.IO.FileStream
    25.         fs = New System.IO.FileStream("c:\serialize.dat", IO.FileMode.Open) 'it will read from serialize.dat
    26.         Dim bf As New BinaryFormatter
    27.         obj = CType(bf.Deserialize(fs), class1)
    28.         fs.Close()
    29.     End Sub

  3. #3
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    24

    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
  •  



Click Here to Expand Forum to Full Width