Approaches to File I/O

From RAD Studio
Jump to: navigation, search

Go Up to Working with Files


There are several approaches you can take when reading from and writing to files:

  • The recommended approach for working with files is to use file streams:
    • File streams are instances of the TFileStream class used to access information in disk files.
    • File streams are a portable and high-level approach to file I/O.
    • Because file streams make the file handle available, this approach can be combined with the next one. Using File Streams discusses TFileStream in detail.
  • You can work with files using a handle-based approach.
    • File handles are provided by the operating system when you create or open a file to work with its contents.
    • The SysUtils unit defines a number of file-handling routines that work with files using file handles.
    • On Windows, these are typically wrappers around Windows API functions.
    • Because the RTL functions can use the Delphi language syntax, and occasionally provide default parameter values, they are a convenient interface to the Windows API.
    • To use a handle-based approach, you first open a file using the FileOpen function or create a new file using the FileCreate function. Once you have the handle, use handle-based routines to work with its contents (write a line, read text, and so on).
  • The System unit defines a number of file I/O routines that work with file variables, usually of the format "F: Text:" or "F: File:"
    • File variables can have one of three types: typed, text, and untyped. A number of file-handling routines, such as AssignPrn and Writeln, use them.
    • The use of file variables is deprecated, and these file types are supported only for backward compatibility. They are incompatible with Windows file handles.

See Also