Thread View: comp.lang.pascal.delphi.misc
3 messages
3 total messages
Started by Shay
Mon, 23 Jul 2001 15:43
file of record with dynamic array
Author: Shay
Date: Mon, 23 Jul 2001 15:43
Date: Mon, 23 Jul 2001 15:43
55 lines
2675 bytes
2675 bytes
hi,
i'm trying to save data (long string with unknown size) to a file.
i need help - i tried to read the posts here about the subject, but i
still don't know what to do.
what i want to do -
save a record to a file , the record has strings (with known size) &
strings (with UNKNOWN size) & integers.
i tried to declared the unknown sized string as "array of char" or
"string" or "^ ArrayOfCharType", but each time i got in the saved file
a pointer to this vaiable, and i want the data of it.
i had to declared the file variable as "file" and not as "file of
EntryType" due to the problem of finalization.
here is an example :
type
EntryType = record
str1 : string[255];
str2 : string[255];
Num1 : integer;
largeStr : array of char;
end;
procedure ...
var
MyFile : file;
MyRec : EntryType ;
sourceStr : string; // a large string that i have from some other
place with unknown size (until i get it)
begin
sourceStr := getSourceString();
SetLength(MyRec.largeStr, length(sourceStr));
// the next line also give me an error, thats why i did a loop
// MyRec.largeStr := sourceStr;
for i := 1 to length(sourceStr) do
begin
MyRec.largeStr[i-1] := Char(sourceStr[i]);
end;
MyRec.str1 := 'line1';
MyRec.str2 := 'line2';
MyRec.num1 := 123record;
AssignFile(MyFile,'data1.dat');
rewrite(MyFile,1);
BlockWrite(MyFile, MyRec, sizeof(MyRec));
closeFile(MyFile);
end;
thanks,
Shay
Re: file of record with dynamic array
Author: Andy
Date: Tue, 24 Jul 2001 01:01
Date: Tue, 24 Jul 2001 01:01
67 lines
3485 bytes
3485 bytes
the problem is that the array is not array but a pointer to the array. So for example you can use // write first fields BlockWrite(file,record,integer(@rec.largeStr)-integer(@rec)); // write your string leng := high(rec.largeStr)-low(rec.largeStr)+1 // array size; BlockWrite(file,leng,sizeof(leng)); // you will need it to read BlockWrite(file, largeStr[low(largestr)],leng); // array itself "Shay" <shayt@inter.net.il> wrote in message news:d44d2b3f.0107230743.1051539d@posting.google.com... > hi, > i'm trying to save data (long string with unknown size) to a file. > i need help - i tried to read the posts here about the subject, but i > still don't know what to do. > > what i want to do - > save a record to a file , the record has strings (with known size) & > strings (with UNKNOWN size) & integers. > i tried to declared the unknown sized string as "array of char" or > "string" or "^ ArrayOfCharType", but each time i got in the saved file > a pointer to this vaiable, and i want the data of it. > > i had to declared the file variable as "file" and not as "file of > EntryType" due to the problem of finalization. > > here is an example : > > type > EntryType = record > str1 : string[255]; > str2 : string[255]; > Num1 : integer; > largeStr : array of char; > end; > > procedure ... > var > MyFile : file; > MyRec : EntryType ; > sourceStr : string; // a large string that i have from some other > place with unknown size (until i get it) > begin > sourceStr := getSourceString(); > SetLength(MyRec.largeStr, length(sourceStr)); > > // the next line also give me an error, thats why i did a loop > // MyRec.largeStr := sourceStr; > > for i := 1 to length(sourceStr) do > begin > MyRec.largeStr[i-1] := Char(sourceStr[i]); > end; > > MyRec.str1 := 'line1'; > MyRec.str2 := 'line2'; > MyRec.num1 := 123record; > > AssignFile(MyFile,'data1.dat'); > rewrite(MyFile,1); > BlockWrite(MyFile, MyRec, sizeof(MyRec)); > closeFile(MyFile); > end; > > thanks, > Shay
Re: file of record with dynamic array
Author: Shay
Date: Wed, 25 Jul 2001 13:55
Date: Wed, 25 Jul 2001 13:55
71 lines
3620 bytes
3620 bytes
Thank you very much. shay. "Andy" <agribanov@paradise.net.nz> wrote in message news:<vI377.211$ww1.12339@news02.tsnz.net>... > the problem is that the array is not array but a pointer to the array. So > for example you can use > > // write first fields > BlockWrite(file,record,integer(@rec.largeStr)-integer(@rec)); > // write your string > leng := high(rec.largeStr)-low(rec.largeStr)+1 // array size; > BlockWrite(file,leng,sizeof(leng)); // you will need it to read > BlockWrite(file, largeStr[low(largestr)],leng); // array itself > > "Shay" <shayt@inter.net.il> wrote in message > news:d44d2b3f.0107230743.1051539d@posting.google.com... > > hi, > > i'm trying to save data (long string with unknown size) to a file. > > i need help - i tried to read the posts here about the subject, but i > > still don't know what to do. > > > > what i want to do - > > save a record to a file , the record has strings (with known size) & > > strings (with UNKNOWN size) & integers. > > i tried to declared the unknown sized string as "array of char" or > > "string" or "^ ArrayOfCharType", but each time i got in the saved file > > a pointer to this vaiable, and i want the data of it. > > > > i had to declared the file variable as "file" and not as "file of > > EntryType" due to the problem of finalization. > > > > here is an example : > > > > type > > EntryType = record > > str1 : string[255]; > > str2 : string[255]; > > Num1 : integer; > > largeStr : array of char; > > end; > > > > procedure ... > > var > > MyFile : file; > > MyRec : EntryType ; > > sourceStr : string; // a large string that i have from some other > > place with unknown size (until i get it) > > begin > > sourceStr := getSourceString(); > > SetLength(MyRec.largeStr, length(sourceStr)); > > > > // the next line also give me an error, thats why i did a loop > > // MyRec.largeStr := sourceStr; > > > > for i := 1 to length(sourceStr) do > > begin > > MyRec.largeStr[i-1] := Char(sourceStr[i]); > > end; > > > > MyRec.str1 := 'line1'; > > MyRec.str2 := 'line2'; > > MyRec.num1 := 123record; > > > > AssignFile(MyFile,'data1.dat'); > > rewrite(MyFile,1); > > BlockWrite(MyFile, MyRec, sizeof(MyRec)); > > closeFile(MyFile); > > end; > > > > thanks, > > Shay
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads