Hello Experts... I have a dynamic array of FileStream's that can get bigger only... never smaller. Reason is I need to have (possibly) many filestreams open at once as I need to write a record to the appropriate stream based on the number of fields the record has. For instance, if I read a record and it has 7 fields, I need to write it to FileStream... if the next record I read has 22 fields and my Filestream array is only 0..7 in length, I then need to do a Setlength on my FileStream array to increase the length to 23 Filestream elements (ie. 0..22). My code basically looks like this:
// check if an output file already has been opened for this particular fieldcount
BuildFileName := FNnoext+'-'+IntToStr(numfields);
If numfields+1 > Length(OutFilesStream) then // This file doesn't yet exist, increase the size of our dynamic files array
(I'm using "TGpHugeFileStream" component from Primoz Gabrijelcic instead of a regular TFileStream. Shouldn't make any difference).
Somehow, my dynamic array data is getting trashed when I try to increase the size of it with the SetLength(OutFilesStream, numfields+1); statement. Stepping through it in debug mode revealed this:
- after the declaration and initial Setlength of the array to 0 (not shown), the array contains simply: ()
- after reading the first data record with 8 fields (numfields=8) and doing a Setlength(numfields+1), the array looked like: nil,nil,nil,nil,nil,nil,ni
- after the file "create" for OutFileStream, the array looked like : nil,nil,nil,nil,nil,nil,ni
- okay, so far so good, but when the next record is read... one with 9 fields this time, the trouble happens. Upon doing the Setlength(numfields+1), this is what it did to the OutFileStream array: nil,(),(),(),(),(),(),(),(
...It lost my file handle info for element . Any thoughts anyone?