Reading/Writing Flexible Arrays
Author |
Message |
Cervantes
|
Posted: Sat May 21, 2005 10:26 pm Post subject: Reading/Writing Flexible Arrays |
|
|
I'm back.
I've followed Delos' advice and switched from using tell, seek, mod, put, and get to only read and write. The thing is, I've got a flexible array of a record to store data on users. As far as I can tell, I can write the whole flexible array record into the text file just fine; the problem comes when I want to read it back. The flexible array starts at 1 .. 0, but there's probably more data than that in my text file. Doing a read : fileNo, user won't set the upper bounds of my user array to what it should be, based on the amount of data in the text file.
So, how do I get around this? Once again, I can think of a solution (actually, three, this time), but I don't really want to use them because they aren't so orderly.
Solution 1) I start the record off at some obscenely large number. For obvious reasons, I reject this plan of action.
Solution 2) I split the data up into many different files, one for each element of the array.
Writing would work like this:
Turing: |
var fileNo : int
for i : 1 .. upper (user )
open : fileNo, "user" + intstr (i ) + ".txt", write
write : fileNo, user (i )
close : fileNo
end for
|
Reading would work like this:
Turing: |
var fileNo : int
var count := 1
loop
exit when ~ File.Exists ("user" + intstr (count ) + ".txt"
new user, count
open : fileNo, "user" + intstr (count ) + ".txt", read
read : fileNo, user (count )
close : fileNo
count + = 1
end loop
|
I don't really want to resort to this, however, because that would mean having lots and lots of text files. That's never fun.
Solution 3) I put flags in my file that tell me where a new element is starting. I would have to use read and get in the same loop. The question here is after I use read once, where is the effective cursor? That is, after reading once, where would the next get start from? If one read would go to the end of the first element and then the next get would get data just one line after that, things would be swell.
Thanks again,
Cervantes |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Delos
|
Posted: Sat May 21, 2005 10:36 pm Post subject: (No subject) |
|
|
Hmm...I'm sure you've thought of this already, but here's the approach that I've used several times when dealing with flexible arrays and record writing. (I actually prefer to use put: and get:, but that's just me...)
Turing: |
var file : int
var temp : string
var arr : flexible array 1.. 0 of string
if not File.Exist ("input.txt") then
put "This file does not exist"
return
end if
open : file, "input.txt", get
loop
exit when eof(file )
new arr, upper(arr ) + 1
get : file, temp : *
arr (upper(arr )) := temp
end loop
close : file
for i : 1.. upper(arr )
put arr (i )
end for
|
Before you even ask me, yes, I had a reason for using the seemingly redundant 'temp' variable. Just think about the possibilities if instead of string, arr was an array of some custom-made type. You could put each element of the type on a seperate line, then create a function to extract them. Or, as you noted, you could set up flags. Neither of these options are insanely difficult.
And since you're using flexible arrays, you can have a file as long as you need, just so long as no single line has more than 255 chars! [sigh], the joys of Turing.
Just to make sure, you do know why I used ': *' in my get: call? Of course you do.
BTW, check out the Tutorials section for a new addition. |
|
|
|
|
|
Cervantes
|
Posted: Sat May 21, 2005 11:09 pm Post subject: (No subject) |
|
|
Thanks again Delos.
I guess I'll have to weigh one option against the other. Using lots of files and read/write or using one file and put/get. I'd have to do more typing with put/get, since I've got lots of data to store. But I suppose that's irrelevent.
I did not follow this:
Delos wrote: You could put each element of the type on a seperate line, then create a function to extract them.
Did you mean to say "element"? Or "field"?
If field, then why the temp variable? It could be like such:
code: |
var user : flexible array 1 .. 0 of
record
name, password, otherData : string
end record
var file : int
open : file, "input.txt", get
loop
exit when eof (file)
new user, upper (user) + 1
get : file, user (upper (user)).name : *
get : file, user (upper (user)).password : *
get : file, user (upper (user)).otherData : *
end loop
close : file
|
Delos wrote:
BTW, check out the Tutorials section for a new addition.
Yeah, awesome work! 3000 bits
Thanks again for the help.
I'm going to sleep now, see you tomorrow. :p |
|
|
|
|
|
|
|