Just as an excercise for working with streams: If there were not a "string" variable in the TDataFile record declaration you could simply do a very fast single write:
Stream.Write(vDataFile, SizeOf(vDataFile))
The problem is, however, that a "string" is a pointer, and this routine would only write the address of the string data to the stream, not the data themselves. However, when you know that BackupFolderName will never be longer than, say, 60 characters, then you can redeclare the BackupFolderName as a short string, String[60], and this trick will work because it writes the string data directly (of course, this will write unnecessary characters when the string is shorter):
type
TDataFile = Record
Version : Byte;
BackupNumber : Word;
BackupFolderName : String[60];
StartDateTime : TDateTime;
EndDateTime : TDateTime;
Completed : Byte;
end;
procedure Tbackup.WriteDataFile(DFPath:String; DFName:String);
var
fsOut : TFileStream;
vDataFile : TDataFile;
begin
with vDataFile do
begin
Version := 1;
BackupNumber := 0;
BackupFolderName := DFName;
StartDateTime := Now;
EndDateTime := 0;
Completed := 0;
end;
fsOut := TFileStream.Create(DFPath+DFName+'.bck', fmCreate);
try
fsOut.Write(vData, SizeOf(FDataFile));
finally
fs.Out.Free;
end;
end;
function Tbackup.ReadDataFile(DFPath:String; DFName:String):TDataFile;
var
fsOut: TFileStream;
n: Integer;
begin
fsOut := TFileStream.Create(DFPath+DFName+'.bck', fmOpenRead);
try
n := fsOut.Read(fsOut, SizeOf(TDataFile));
if n <> SizeOf(TDataFile) then
raise Exception.Create('TDataFile record could not read correctly');
finally
vsOut.Free;
end;
end;
In the reading function you'll notice that the return value of the stream's Read method is checked -- this is the number of bytes actually read. If this value is different from the value expected, the SizeOf() value, then something is wrong (file damaged, somebody fiddled around with a hex editor, etc.).
Another remark: Directly writing the record to the file stream, makes the file very dependent on the version, you cannot add other fields to the record later because reading and writing may run out of sync. If you must provide new record fields you must provide a Version flag (or maybe the Version field of the record is just for this purpose?), read the version info first and then decide which record type actually will be read. Or you select a more flexible, descriptive file format such as ini, or xml.