Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Dynamic array data gets trashed when I increase size with Setlength

Avatar of shawn857
shawn857 asked on
Delphi
5 Comments1 Solution325 ViewsLast Modified:
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[7]... 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);

                TRY
                  SetLength(HBuffer, Length(holdrec)+2);
                  StrPCopy(PChar(HBuffer), holdrec+#13#10);
                  bytes_Wrote:=OutFilesStream[numfields].Write(HBuffer[0], StrLen(PChar(HBuffer)));
                EXCEPT
                  If numfields+1 > Length(OutFilesStream) then  // This file doesn't yet exist, increase the size of our dynamic files array
                     SetLength(OutFilesStream, numfields+1);
                  OutFilesStream[numfields]:=TGpHugeFileStream.Create(BuildFileName, accWrite);
                  bytes_Wrote:=OutFilesStream[numfields].Write(HBuffer[0], StrLen(PChar(HBuffer)));
                END;


(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,nil,nil,nil
- after the file "create" for OutFileStream[8], the array looked like : nil,nil,nil,nil,nil,nil,nil,nil,$1297078
- 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,(),(),(),(),(),(),(),(),nil

  ...It lost my file handle info for element [8]. Any thoughts anyone?

Thanks!
   Shawn
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert GFlag of Belgium imageOracle dba
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers