AuthorMessage
Mickey
Ametuar
Posts: 115

Since Delphi topic is empty, let me start it.
I have recognized that a lot of Delphi programmers use Turbo/Borland Pascal methods of working with files which are good in Delphi too but are n00bish and old.
There are much better ways in Delphi. Eazier to handle:
Code:
uses
  SysUtils; // file handling routines can be found here
procedure FileHandlingSample;
 var
  SomeFile: file;  // untyped file
  SomeTypedFile: file of AnyType;  // typed file
  SomeFileName : string;
begin
  SomeFileName := 'FileName.ext';
  AssignFile(SomeFile, SomeFileName);
  try
    Rewrite(SomeFile); // Creating a new file. You can write only
(*   or
    Reset(SomeFile); // Opening an existig file for reading/writting  *)
    try
      // Do something with file here: Read, Write etc.
      ...
      ...
      ...
    finally
      CloseFile(SomeFile);
    end; // try...finally
  except
    // Error handling here
    on E:EInOutError do
      MessageDlg(E.Message, mtError, [mbAbort], 0); // Default error message by system.
  end; // try...except
  // You may continue your work here
  ...
end; // procedure

In except...end statement you may handle error messages by yourself. You need to do this only if you want to customize error messages.
There is a sample:
Code:
...
  except
    on E:EInOutError do
      begin
        Error := True;
        case E.ErrorCode of
          2 : E.Message := 'I cannot find: ' + SomeFileName;
          3 : E.Message := 'I cannot find the path!';
          5 : E.Message := 'Access denied: ' + SomeFileName;
         32 : E.Message := 'Sharing violation!';
             // and so on with other customized messages...
             // see other E.ErrorCode-s in help.
        else
          E.Message := SomeFileName + E.Message;
        end; // case
        MessageDlg(E.Message, mtError, [mbAbort], 0);
      end; //on
  end; // try...except
...

If you want to read data from a textfile to a memo the eaziest and fastest way is:
Code:
...
   if FileExists(SomeFileName)
     then
       Memo.Lines.LoadFromFile(SomeFileName);  // text file to memo
...

Here we don't need checking because FileExists returns a boolean value that indicates whether the specified file exists and can be opened.
That's all folks.