Difference between revisions of "File"

From Free Pascal wiki
Jump to navigationJump to search
(This page was largely a copy of (invalid and messy) old Text page. "file of char" is NOT the same as Text/TextFile. Also, no need for unnecessary upper case. "file" is a type, not variable.)
(List three types of files - file, file of xxx, and link to Text)
Line 1: Line 1:
 
In a [[Pascal]] program, a variable of type '''file''' may be used to read or write (or both) to the file. A file is (usually) associated with a file on a disk, so it's contents are obviously stored on the disk even after the program [[terminate]]s.
 
In a [[Pascal]] program, a variable of type '''file''' may be used to read or write (or both) to the file. A file is (usually) associated with a file on a disk, so it's contents are obviously stored on the disk even after the program [[terminate]]s.
  
Example:
+
There are three types of files:
  
<delphi>var F: file;</delphi>
+
# The pure type '''file''' is an ''untyped'' binary file. ''Untyped'' means it's just a sequence of data (bytes). It may be used to store essentially anything. It can be read by '''BlockRead''' and written to by '''BlockWrite''' procedures. For example source code, see [[http://www.freepascal.org/docs-html/rtl/system/blockread.html|BlockRead documentation]]. The word ''file'' itself is a [[Reserved word|reserved word]] in Pascal.
  
Where '''F''' is the file variable used to access the file. The word ''file'' itself is a [[Reserved word|reserved word]] in Pascal.  
+
# The type '''file of Xxx''', where Xxx is any simple (without references / pointers) type, is a binary file representing a sequence of Xxx values. For example, you can have files that are a sequence of integers, floating-point values, or records (as long as all record fields are still a simple type).
  
When a file is defined in this fashion, it is called an ''[[untyped]]'' file; it may be used to store essentially anything.
+
# The type [[Text]] (more modern name: TextFile) represents a text file. This is something different than just '''file of char'''. There are various comfortable read and write operations available for text files, that parse integers, floating-point values and other types. Also, line endings are properly dealt with when you use text files.
 
 
To read and write text files, use the [[Text]] (more modern name: TextFile) type.
 
  
 
A file variable is used either for input, for output, or for both input and output (I/O).  A file may be a screen and/or keyboard, a modem, a light pen, a touch screen, a mouse, an area of storage on disk, a socket for connecting to the Internet, or it could be some other device depending on how the run-time library ties the file.  In some cases, actual memory can be defined as a file; this was used in the case of a ram disk.
 
A file variable is used either for input, for output, or for both input and output (I/O).  A file may be a screen and/or keyboard, a modem, a light pen, a touch screen, a mouse, an area of storage on disk, a socket for connecting to the Internet, or it could be some other device depending on how the run-time library ties the file.  In some cases, actual memory can be defined as a file; this was used in the case of a ram disk.

Revision as of 17:25, 13 March 2012

In a Pascal program, a variable of type file may be used to read or write (or both) to the file. A file is (usually) associated with a file on a disk, so it's contents are obviously stored on the disk even after the program terminates.

There are three types of files:

  1. The pure type file is an untyped binary file. Untyped means it's just a sequence of data (bytes). It may be used to store essentially anything. It can be read by BlockRead and written to by BlockWrite procedures. For example source code, see [documentation]. The word file itself is a reserved word in Pascal.
  1. The type file of Xxx, where Xxx is any simple (without references / pointers) type, is a binary file representing a sequence of Xxx values. For example, you can have files that are a sequence of integers, floating-point values, or records (as long as all record fields are still a simple type).
  1. The type Text (more modern name: TextFile) represents a text file. This is something different than just file of char. There are various comfortable read and write operations available for text files, that parse integers, floating-point values and other types. Also, line endings are properly dealt with when you use text files.

A file variable is used either for input, for output, or for both input and output (I/O). A file may be a screen and/or keyboard, a modem, a light pen, a touch screen, a mouse, an area of storage on disk, a socket for connecting to the Internet, or it could be some other device depending on how the run-time library ties the file. In some cases, actual memory can be defined as a file; this was used in the case of a ram disk.

Some of these devices are connected through various specialized libraries instead of treating them as actual files. For example, databases are not normally accessed directly as a simple file which is read or written, they are usually accessed either through a set of database handling procedures and functions in an external unit or library, or are accessed via SQL commands sent to a procedure or function. Programs that communicate with the Internet usually use a set of routines called sockets under Unix-based operating systems, or using the Winsock routines on Microsoft Windows, which essentially provide the same functions as the Unix sockets routines.

Generally, the standard Pascal language does not distinguish between types of files, treating all files the same, allowing them to be accessed via the read, readln, write and writeln standard procedures. Some Pascal Compilers also used the Get and Put routines for I/O.

Enhanced versions of Pascal such as UCSD Pascal, Turbo Pascal, and, of course, FPC Pascal have added features to indicate that a file is specifically a screen or a disk file, and to allow for such functions as naming of disk files, random access, appending to the end and deleting of files as needed.

File-related types, procedures and functions:

File - Text - AssignFile - CloseFile - Reset - Rewrite - Get - Put - Read - Readln - Write - Writeln