Flexible arrays and input and output
Author |
Message |
jondmml
|
Posted: Thu Mar 08, 2007 4:55 pm Post subject: Flexible arrays and input and output |
|
|
I have a program that creates a flexible array and then outputs it to a file. I wish to retrieve this flexible array and am unsure how to approach this problem since I do not know how large the array is. Is there any way to open the array or should I just write a variable stating the array size? Thanks,
Jon |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Thu Mar 08, 2007 6:46 pm Post subject: RE:Flexible arrays and input and output |
|
|
Use a flexible array and keep increasing the bounds of the array until you hit the eof (end of file). |
|
|
|
|
![](images/spacer.gif) |
jondmml
|
Posted: Fri Mar 09, 2007 1:19 pm Post subject: RE:Flexible arrays and input and output |
|
|
Thanks for the quick response. The only thing is, I think you misinterpreted my question. The file consists of a flexible array. I don't think I can read out line after line of an array using the method you suggested. For the time being, I just store a variable with the length of the flexible array, however, there must be a better way. |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Thu Mar 15, 2007 4:35 am Post subject: Re: Flexible arrays and input and output |
|
|
I'm not sure exactly what problem with reading in the data you're describing, but based on what you've said so far just follow this pseudo-ish code and you'll be fine:
code: | open file
var foo : flexible array 1 .. 0 of element
while not eof
increase size of foo by 1
set top element of foo to value in file
move down one line or across one word |
The key is to start the array at size 0, and increase the size as long as the file contains more elements. |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Thu Mar 15, 2007 11:05 am Post subject: Re: RE:Flexible arrays and input and output |
|
|
jondmml @ Fri Mar 09, 2007 1:19 pm wrote: Thanks for the quick response. The only thing is, I think you misinterpreted my question. The file consists of a flexible array. I don't think I can read out line after line of an array using the method you suggested. For the time being, I just store a variable with the length of the flexible array, however, there must be a better way.
You say the file contains a flexible array. What exactly does that mean? It probably means it contains a bunch of data, one line for each element in the array, or something like that. That accounts for most of the data stored in the flexible array. There are two other pieces of data stored by the flexible array, though: the upper and lower bounds. Thus, you might want to include that data in your file, probably at the very start. However, think about the problem you're working on in a broader sense and determine if this is really necessary. At the very least, writing the size of the array would make loading the array from the file faster. It takes a fair bit of work to resize a flexible array.
(There's actually another piece of information stored by the flexible array. That's the type. However, you probably won't have to worry about writing that to the file.) |
|
|
|
|
![](images/spacer.gif) |
jrblast
|
Posted: Fri Mar 16, 2007 12:00 am Post subject: RE:Flexible arrays and input and output |
|
|
I think he means he creates a flexible array and then just uses the write command to store it all in the datafile with just one line of code. You can write to the file this way with no problems but when you want to read it you dont know the upper bound. The easiest solution I can think of is just write the flexible array to the file:
code: |
stuff
write : fileStream, flexibleArray
more stuff
|
and then have a loop to read it, include exit when eof (fileStream), increase the upper bound, read to a seperate, non array, variable and make the upper bound of the flexible array equal to that. Heres some sample code I made to show you how this works.
Writing to the file: code: | var flex : flexible array 1 .. 0 of int
for i : 1 .. 10 %give values to the array, in this case we are using some perfect squares
new flex, i
flex (i) := i ** 2
put flex (i)
end for
var dataStream : int := 0
open : dataStream, "test.dat", write
write : dataStream, flex %write the array to the file
close : dataStream
|
Reading the file: code: | var flex : flexible array 1 .. 0 of int
var input : int
var up : string
var dataStream : int := 0
open : dataStream, "test.dat", read
loop
exit when eof (dataStream)
new flex, upper (flex) + 1
read : dataStream, input
flex (upper (flex)) := input
put flex (upper (flex))
end loop
close : dataStream
|
|
|
|
|
|
![](images/spacer.gif) |
|
|