Difference between revisions of "File"

From Free Pascal wiki
m
(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.)
Line 1: Line 1:
In this article, for the sake of ease of understanding, [[Reserved words|reserved word]]s in Pascal are shown in UPPER CASE even though the Pascal lanugage is not case sensitive.
+
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.
  
When used in a [[Pascal]] program, a '''file''' is a variable which may be used to retrieve data from by reading the file [[Variable|variable]], or to send data to the file variable, or for both reading and writing.  A file differs from normal variables in a program in that the information within the file is usually available even after the program [[terminate]]s.
+
Example:
  
The Pascal source format is
+
<delphi>var F: file;</delphi>
:VAR filname : FILE;
 
  
Where '''filname''' is the file variable used to access the file.  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.  
  
 
When a file is defined in this fashion, it is called an ''[[untyped]]'' file; it may be used to store essentially anything.
 
When a file is defined in this fashion, it is called an ''[[untyped]]'' file; it may be used to store essentially anything.
  
A typical type of file used for plain documents is a '''file [[Of|of]] [[Char|char]]''' and is defined by the compiler using the shorthand name [[Text|text]].  As such, the following two declarations should be equivalent:
+
To read and write text files, use the [[Text]] (more modern name: TextFile) type.
 
 
:VAR filname : FILE OF char;
 
:VAR filname : text;
 
 
 
In the above example, the [[Identifier|identifier]] used (''filname'') is the file [[Variable|variable]] which is used to do input, output, or both, to the actual file.  The file variable, however, is not the actual file; the file variable must be tied to the actual file  by a [[RTL|run-time library]] routine.  In most cases, this is done via the [[assign]] procedure followed by use of the [[reset]] or [[rewrite]] procedure.  In the case of specialized files such as databases, it is done via some other method than the standard ones, in order to allow a file variable to actually read from and/or write to the actual [[File|file]] itself using specialized routines.
 
  
 
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:15, 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.

Example:

<delphi>var F: file;</delphi>

Where F is the file variable used to access the file. The word file itself is a reserved word in Pascal.

When a file is defined in this fashion, it is called an untyped file; it may be used to store essentially anything.

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.

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