Writes one or more records from a variable to an open file.
procedure BlockWrite(var f: File; var Buf; Count: Integer); overload; procedure BlockWrite(var f: File; var Buf; Count: Integer; var AmtTransferred: Integer); overload;
BlockWrite(File f, Buf, int Count); BlockWrite(File f, Buf, int Count, int AmtTransferred);
F is an untyped file variable, Buf is any variable, Count is an expression of type Integer, and AmtTransferred is an optional variable of type Integer.
BlockWrite writes Count or fewer records to the file F from memory, starting at the first byte occupied by Buf. The actual number of complete records written (less than or equal to Count) is returned in AmtTransferred.
The entire block transferred occupies at most Count * RecSize bytes. RecSize is the record size specified when the file was opened (or 128 if the record size was unspecified).
If the entire block is transferred, AmtTransferred is equal to Count on return.
If AmtTransferred is less than Count, the disk became full before the transfer was complete. In this case, if the file's record size is greater than 1, AmtTransferred returns the number of complete records written.
BlockWrite advances the current file position by AmtTransferred records.
If AmtTransferred isn't specified, an I/O error occurs if the number written isn't equal to Count. If the $I+ compiler directive is in effect, errors raise an EInOutError exception.
Delphi Examples:
{ This example reads an entire file into a buffer with one command and then writes it into a saved file. Validate the saved file contents. } procedure TForm1.Button1Click(Sender: TObject); var FromF, ToF: file; NumRead, NumWritten: Integer; Buf: array[1..2048] of Char; begin if OpenDialog1.Execute then { Display Open dialog box } begin AssignFile(FromF, OpenDialog1.FileName); Reset(FromF, 1); { Record size = 1 } if SaveDialog1.Execute then { Display Save dialog box} begin AssignFile(ToF, SaveDialog1.FileName); { Open output file } Rewrite(ToF, 1); { Record size = 1 } Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromF)) + ' bytes...'); repeat System.BlockRead(FromF, Buf, SizeOf(Buf), NumRead); BlockWrite(ToF, Buf, NumRead, NumWritten); until (NumRead = 0) or (NumWritten <> NumRead); // Use CloseFile rather than Close; Close provided for backward compatibility CloseFile(FromF); CloseFile(ToF); Canvas.TextOut(120, 10, ' done.'); end; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|