Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Dec 11, 2017 8:02 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: File I/O and Classes
PostPosted: Wed Nov 28, 2012 8:50 am 
Offline

Joined: Sat Aug 18, 2012 11:23 am
Posts: 21
Hello all,
In VB6 I would have a User-Defined Type (UDT) that created a single record of a determinable length.

'In RB I've created a Class:
Class TheClass
Text() as String
SomeData() as Integer
MoreData() As Integer
End Class

' I create an array of TheClass
tc() as TheClass

'Later I dim it to 11 records
ReDim tc(10)
For i = 0 to 10
tc(i) = new NewClass
redim tc(i).Text(4)
redim tc(i).SomeData(4)
redim tc(i).MoreData(4)
Next i

All of this works beautifully.

The problem is ... I know that there's 11 records. But the user will be able to add records.

In VB6 I used to get the length of the file, determine the length of each record, and divide them to get the number of records.

Writing to disk seems simple enough. But how do I determine the number of records in a serialized file when I want to read the data?

I've wondered if .Append is the way to go by deserializing the file in a loop, but how do I know when to start and stop each "record" since the Text string length can vary?

Can anyone point me in the right direction?

Thanks,
RBnoob


Top
 Profile  
Reply with quote  
 Post subject: Re: File I/O and Classes
PostPosted: Wed Nov 28, 2012 9:08 am 
Offline

Joined: Mon May 30, 2011 12:56 am
Posts: 702
If you are bound by a file structure (such as ISAM), I find the best method is to create a memory block of the right size, and write lumps of that to the file.

The fixed length allows you to determine the number of records by getting the length of the file and dividing, as you say.

The CLASS you have created does not have fixed length strings, so this approach would fail with a class.


If you are starting from scratch, you are better off creating a simple database, with fields for each member of the class. There are loads of examples of that in the database area.


Top
 Profile  
Reply with quote  
 Post subject: Re: File I/O and Classes
PostPosted: Wed Nov 28, 2012 9:13 am 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
I recommend switching to a database as well. Use the very, very fast and lightweight REALSQLDatabase class.

A few years ago we rewrote a VB6 app that was manually storing data in various text files. They had a few relatively complex calculations that took minutes to complete. We switched to using a database and let the db do what it's really good at and that calculation is now done nearly instantaneously. Now instead of a calculate button we have adjust a full chart on the fly when the user changes a value. Obviously not all of that was file I/O but the database is really really good at some operations (like summing and finding related data).

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: File I/O and Classes
PostPosted: Wed Nov 28, 2012 9:30 am 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
I also recommend a database if possible. But to address your question as asked:

Use a structure instead of a class, which is what the UDT is. You'd set up the structure with fixed lengths for the strings and fixed dimensions for the arrays. For example:
Structure TestStructure
Text(4) As String * 100
SomeData(4) As Integer
MoreData(4) As Integer

You'd use it in a similar way that you use the class except you can't change the dimensions of the arrays, and you can't add more text to each string than your upper limit. (In the example above, that's 100 bytes.) Since every structure is a known number of bytes, reading and writing to a file is easy through the Structure's StringValue method.

If you need to alter the size on a per-instance basis, you will have to create your own format for reading and writing the data. For example, the first 4 bytes contains the total length of the record, the next 4 bytes contains the Ubound of the text array, the next four contain the length of the first text element, the next X bytes are text element 1, etc. You can encapsulate this into methods in your class so you can later do:
myClassInstance.ReadFromBinaryStream( bs )
bs.Write myClassInstance.StringValue

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: File I/O and Classes
PostPosted: Wed Nov 28, 2012 9:34 am 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
Actually, the "ReadFromBinaryStream" should be more generalized, something like:
Sub ReadFrom(src As Readable)

That way you could read from any Readable stream.

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: File I/O and Classes
PostPosted: Wed Nov 28, 2012 6:04 pm 
Offline

Joined: Sat Aug 18, 2012 11:23 am
Posts: 21
Thank you for the replies.

In all the years I've programmed I've never once done a database program. I guess it's time.

My concern with the STRUCTURE method, which would be closer to what I know, is that some of the structures would be 20K, and I don't think that's the way to go anymore.

I'm off to search for database tutorials and examples.

Thanks for your time and input.

RBnoob


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group