Difference between revisions of "File"

From Free Pascal wiki
Jump to navigationJump to search
(List three types of files - file, file of xxx, and link to Text)
(Remove useless notes that file is usually a file on disk (duh), use modern XxxFile identifiers, mention FileMode, FileRec, TextRec...)
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 variable is (usually) associated with a normal file on a disk using the [[AssignFile]] procedure (older, equivalent name: just [[Assign]]).
  
 
There are three types of files:
 
There are three types of files:
  
# 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.  
+
# The 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.  
  
 
# 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).
 
# 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).
  
# 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.
+
# The type [[Text|TextFile]] (older, equivalent name: Text) represents a text file. Note that this is 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.
+
Working with all file types is similar:
 
+
# Associate a file with data. ''Usually'' by calling [[AssignFile]] (older, equivalent name: [[Assign]]).
Some of these devices are connected through various specialized [[library|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 [[Glossary#S|SQL]] commands sent to a procedure or function.  Programs that communicate with the Internet usually use a set of routines called [[Sockets|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.
+
# Open the file by [[Reset]] (read existing file, and maybe write --- see below), [[Rewrite]] (clear contents and write), or [[Append]] (keep contents and write). In case of [[Reset]] on an untyped binary file, the global [[FileMode]] variable determines if you can only read (fmOpenRead), or both read and write (fmOpenReadWrite), to the file.
 
+
# Then read/write file data by [[Read|read]], [[readln]], [[Write|write]] and [[writeln]] standard procedures.  Some Pascal Compilers also used the [[Get]] and [[Put]] routines for I/O. For untyped binary files, use [[BlockRead]] and [[BlockWrite]].
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|read]], [[readln]], [[Write|write]] and [[writeln]] standard procedures.  Some Pascal Compilers also used the [[Get]] and [[Put]] routines for I/O.  
+
# At the end, release the necessary file resources (and possibly flush the data to be written) by calling [[CloseFile]] (older, equivalent name: just [[Close]]).
 
 
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.
 
  
 +
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. You can customize where the file data is by initializing [http://www.freepascal.org/docs-html/rtl/sysutils/filerec.html FileRec record] (for untyped binary files) or [http://www.freepascal.org/docs-html/rtl/sysutils/textrec.html TextRec record] (for text files), instead of calling standard [[AssignFile]] ([[Assign]]) routines.
  
 
[[category:Pascal]]
 
[[category:Pascal]]
  
 
{{File}}
 
{{File}}

Revision as of 04:29, 15 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 variable is (usually) associated with a normal file on a disk using the AssignFile procedure (older, equivalent name: just Assign).

There are three types of files:

  1. The 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 BlockRead 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 TextFile (older, equivalent name: Text) represents a text file. Note that this is 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.

Working with all file types is similar:

  1. Associate a file with data. Usually by calling AssignFile (older, equivalent name: Assign).
  2. Open the file by Reset (read existing file, and maybe write --- see below), Rewrite (clear contents and write), or Append (keep contents and write). In case of Reset on an untyped binary file, the global FileMode variable determines if you can only read (fmOpenRead), or both read and write (fmOpenReadWrite), to the file.
  3. Then read/write file data by read, readln, write and writeln standard procedures. Some Pascal Compilers also used the Get and Put routines for I/O. For untyped binary files, use BlockRead and BlockWrite.
  4. At the end, release the necessary file resources (and possibly flush the data to be written) by calling CloseFile (older, equivalent name: just Close).

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. You can customize where the file data is by initializing FileRec record (for untyped binary files) or TextRec record (for text files), instead of calling standard AssignFile (Assign) routines.

File-related types, procedures and functions:

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