Terminates the association between file variable and an external disk file.
procedure CloseFile(var F);
CloseFile( F);
Due to naming conflicts, CloseFile replaces the Close procedure. Use the CloseFile procedure instead of Close to terminate the association between a file variable and an external disk file.
F is a file variable of any file type opened using Reset, Rewrite, or Append. The external file associated with F is completely updated and then closed, freeing the file handle for reuse.
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;
{ This example reads a file one character at a time and writes it into a file of your chosing. } procedure TForm1.Button1Click(Sender: TObject); var F1, F2: TextFile; Ch: AnsiChar; begin if OpenDialog1.Execute then begin AssignFile(F1, OpenDialog1.Filename); Reset(F1); if SaveDialog1.Execute then begin AssignFile(F2, SaveDialog1.Filename); Rewrite(F2); while not Eof(F1) do begin Read(F1, Ch); Write(F2, Ch); end; CloseFile(F2); end; CloseFile(F1); end; end;
{ Use a TOpenDialog to select a file. Place the first line of the file into a TEdit. Click on the text edit to open the open dialog box. } procedure TForm1.Edit1Click(Sender: TObject); var F: TextFile; S: string; begin if OpenDialog1.Execute then { Display Open dialog box } begin AssignFile(F, OpenDialog1.FileName); { File selected in dialog } Reset(F); Readln(F, S); { Read first line of file } Edit1.Text := S; { Put string in a TEdit control } CloseFile(F); end; end;
{ Click the button to open a TOpenDialog, then select a file to delete. } procedure TForm1.Button1Click(Sender: TObject); var F: Textfile; begin OpenDialog1.Title := 'Delete File'; if OpenDialog1.Execute then begin AssignFile(F, OpenDialog1.FileName); try Reset(F); if MessageDlg('Erase ' + OpenDialog1.FileName + '?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin CloseFile(F); Erase(F); end; except on EInOutError do MessageDlg('File I/O error.', mtError, [mbOk], 0); end; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|