saving udts to a file?

New to FreeBASIC? Post your questions here.
Post Reply
Vance
Posts: 20
Joined: Feb 26, 2006 3:23

saving udts to a file?

Post by Vance »

Is there are a way to save all the variables stored in a user-defined type with a single WRITE command? Kinda like

Code: Select all

type playmate
    fname as string
    lname as string
    month as string
    year as integer
end type

dim shared girls(3) as playmate

girls(1).fname="Lisa"
girls(1).lname="Matthews"
girls(1).month="April"
girls(1).year=1990
girls(2).fname="Kara"
girls(2).lname="Monaco"
girls(2).month="June"
girls(2).year=2005
girls(3).fname="Karen"
girls(3).lname="McDougal"
girls(3).month="December"
girls(3).year=1998

open "playmate.txt" for output as #1
    for i=1 to 3
        write #1,girls(i)      ' *** Part of the program that doesn't work. ***
    next i
close #1
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Post by yetifoot »

You can save a type to a file, using PUT and binary mode, however the strings will have to be a fixed length (ie defined like 'month As String * 256')

If you want variable length strings then you can't use PUT in this way.

here are the changes.

Code: Select all

type playmate
    fname as zstring * 100
    lname as zstring * 100
    month as zstring * 20
    year as integer
end type

Code: Select all

open "playmate.txt" for binary as #1
    for i=1 to 3
        Put #1, , girls(i)      ' *** Part of the program that doesn't work. ***
    next i
close #1
To read back in again use GET in the same manner
Vance
Posts: 20
Joined: Feb 26, 2006 3:23

Post by Vance »

I see. Thanks for the info, yetifoot! :)
Post Reply