Difference between revisions of "Text"

From Free Pascal wiki
(Fixed/removed misinformation and errors: Text is absolutely different than "file of char", use clear "MyFile" instead of "filname", suggest modern TextFile instead of Text, larger example, removed "sp)
Line 1: Line 1:
In this article, for the sake of ease of understanding, [[reserved word]]s in Pascal are shown in UPPER CASE even though the Pascal lanugage is not case sensitive.
+
The [[Type|type]] '''TextFile''' (or, equivalent and older, just '''Text''') is used in a Pascal program to read and write from a text file.
  
The term '''text''', when used in a Pascal program, is a shortcut for the [[Type|type]] ''[[File|file]] of [[Char|char]]''. The following Pascal definitions should be equivalent:
+
<delphi>{$mode objfpc}{$H+}
 +
var
 +
  MyFile: TextFile;
 +
  S: string;
 +
begin
 +
  AssignFile(MyFile, 'a.txt');
 +
  Reset(MyFile);
 +
  try
 +
    Readln(MyFile, S);
 +
  finally
 +
    CloseFile(MyFile);
 +
  end;
 +
end.
 +
</delphi>
  
<delphi>
+
The [[Variable|variable]] representing the text file (''MyFile'' in the example above) may be used to input, output, or both, to the actual file. It must be tied to the actual file  by a [[RTL|run-time library]] routine [[AssignFile]] (older name: just [[Assign]]). Then the file must be opened by the [[Reset]], [[Rewrite]] or [[Append]] procedure. You can read and write to file using Read, Readln, Write, Writeln. After you have finished processing the file, you should release the necessary file resources (and possibly flush the data to be written) by closing the file, calling [[CloseFile]] (older name: just [[Close]]).
  VAR filname: FILE OF char;
 
  VAR filname: text;
 
</delphi>
 
  
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.
+
Note that '''TextFile''' type is very different than the '''file of char''' type.  
  
<HR>
+
* '''file of char''' is just a simple sequence of bytes, and you can only read or write single character at a time. That is, you can only call '''Read(F, C)''' or '''Write(F, C)''' where C is variable of '''char''' type.
When the term '''Text''' is used for [[File|files]] in general, it refers to a type of file which consists of ordinary information which is (usually) readable by a person. This includes documents, [[Source code|source code]] of Pascal Programs, web pages, and other such items.  This differs from a [[Binary|binary]] file, which is generally not human readable, and is used to store machine-readable data.
 
  
 +
* '''TextFile''' offers much more functions, and represents the usual meaning of a text file. You can use Read, Readln, Write, Writeln to read/write from a text file a number of standard types, like strings, integers and floating-point values. Line endings (Unix #10, DOS/Windows #13#10, etc.) are also automatically handled: when reading, various line endings are recognized; when writing, the current OS line endings are used.
  
 
[[category:Pascal]]
 
[[category:Pascal]]
  
 
{{File}}
 
{{File}}

Revision as of 03:38, 13 March 2012

The type TextFile (or, equivalent and older, just Text) is used in a Pascal program to read and write from a text file.

<delphi>{$mode objfpc}{$H+} var

 MyFile: TextFile;
 S: string;

begin

 AssignFile(MyFile, 'a.txt');
 Reset(MyFile); 
 try
   Readln(MyFile, S);
 finally
   CloseFile(MyFile);
 end;

end. </delphi>

The variable representing the text file (MyFile in the example above) may be used to input, output, or both, to the actual file. It must be tied to the actual file by a run-time library routine AssignFile (older name: just Assign). Then the file must be opened by the Reset, Rewrite or Append procedure. You can read and write to file using Read, Readln, Write, Writeln. After you have finished processing the file, you should release the necessary file resources (and possibly flush the data to be written) by closing the file, calling CloseFile (older name: just Close).

Note that TextFile type is very different than the file of char type.

  • file of char is just a simple sequence of bytes, and you can only read or write single character at a time. That is, you can only call Read(F, C) or Write(F, C) where C is variable of char type.
  • TextFile offers much more functions, and represents the usual meaning of a text file. You can use Read, Readln, Write, Writeln to read/write from a text file a number of standard types, like strings, integers and floating-point values. Line endings (Unix #10, DOS/Windows #13#10, etc.) are also automatically handled: when reading, various line endings are recognized; when writing, the current OS line endings are used.

File-related types, procedures and functions:

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