Save and load types
Blitz3D Forums/Blitz3D Beginners Area/Save and load types
| ||
Hello i using Blitz3D for more then a year now. a first i was avoiding using types becaus they where hard to understand. Now i start understanding them and use them. For the most part everyting is working fine if i am using a type. But: I am trying to save and load data from a type to a file. Writing to a file works fine but when i want to load the file back it won't work. here a exampple of source code i tested. Type WorkMenuType Field ID Field Naam$ Field LinkID Field XPos Field YPos Field Width Field Height End Type Dim WorkMenu.WorkMenuType (100) For x = 0 To 100 WorkMenu(x) = New WorkMenuType Next If FileType ("MyTest.typ") = 1 DeleteFile "MuTest.typ" EndIf outfile = WriteFile ("MyTest.typ") For x = 0 To 100 WriteString Outfile,Str(WorkMenu (x)) Next CloseFile outfile ; All the above is working fine. when adding below an error comes up. infile = ReadFile ("MyTest.typ") For x = 0 To 100 WorkMenu(x) = ReadString (Infile) Next CloseFile infile Its generate a error "ILLIGAL TYPE CONVERSION" If i leave the part of reading the file away. The program is running and saving the data fine. why can i write to a file but not read the same way? |
| ||
Because your writing the type "handle" into the file. Not the data in the type. And using an array of types? why not just use types? Makes it MUCH simpler. for example. See, Each type has an "ID" a "Naam$" and the other things, then you just save each of those into the file, then when you load em, make a new type and put that data into it. |
| ||
Hi You are storing strings (not object handles) containing a comma separated list of the values of each field in your typed variable, enclosed between brackets [ ], because you convert your object to string before write it to the file. Extracted from the documentation of Type (see help, I didn't remember but it is there!) ... A cunning trick for debug purposes, or for saving data from types to a file, is to use the Str$ command. Print Str$() will print the values of each field of the type in turn, comma separated, within square brackets, e.g. [15,42,"Fluffy",500]. ... Late, when you read, you get a string and it's not possible to convert a string to an object. Sounds strange, that, blitz convert an object to a string and afterwards couldn't reverse that string to an object again, but, I'm sorry, blitz don't do it. So I agree with the solution given by Terry If you still want, you should write the file the way you do, but when you read the file back, you have to parse the fields one by one, analyzing the string, looking for commas, and padding the first and the last element of the string (they are square brackets). In your example, the string (second field) is null and all the integers are 0, so your strings look like: [0,"",0,0,0,0,0] If you inspect your file with (notepad) you recognize the strings part, but they are separated by a 4 byte (no sense in ascii) because is an integer (4 bytes) that blitz write with the length of the string that is at their side. When you read the file, that weird part, is done by blitz and you get the strings back. Extracted from the help of WriteString (again I didn't remember that!): ... Each string is stored in the file as a 4 byte (32bit) integer followed by the characters that form the string. The integer contains the number of characters in the string, i.e. its length. ... best regards Juan |
| ||
Thanks for the info folks. Its true what you said almeida. And the solution given by you terry, i already use it. But thanks anyway for comfurning my idea. The major problem was indeed the way Blitz is handeling this string format. And thanks for giving me the awnser why its not working. I was wondering about that. Anyway its stupid that Blitz permits it to write it to a file and converting it for you. but not converting it back. I try to program as mutch as possible using OOP. So the program is neet and tide. |