Difference between revisions of "Text"

From Lazarus wiki
Jump to navigationJump to search
m (Text replace - "delphi>" to "syntaxhighlight>")
(Review and fix errors)
Line 1: Line 1:
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 type '''TextFile''' (or, equivalent and older, just '''Text''') is used in a Pascal program to read from and write to a text file.
  
 
<syntaxhighlight>{$mode objfpc}{$H+}
 
<syntaxhighlight>{$mode objfpc}{$H+}
 
var  
 
var  
 
   MyFile: TextFile;
 
   MyFile: TextFile;
   S: string;
+
   s: string;
 
begin
 
begin
 
   AssignFile(MyFile, 'a.txt');
 
   AssignFile(MyFile, 'a.txt');
  Reset(MyFile);
 
 
   try
 
   try
     Readln(MyFile, S);
+
     reset(MyFile);
 +
    readln(MyFile, s);
 +
    writeln('Text read from file: ', s)
 +
 
 
   finally
 
   finally
     CloseFile(MyFile);
+
     CloseFile(MyFile)
   end;
+
   end
end.
+
end.</syntaxhighlight>
</syntaxhighlight>
 
  
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]]).
+
The [[Variable|variable]] representing the text file (''MyFile'' in the example above) may be used to read from, write to, or both to the actual file. It must be tied to the actual file by a [[RTL|run-time library]] routine [[AssignFile]]. 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 by closing the file calling [[CloseFile]].
  
 
Note that '''TextFile''' type is very different than the '''file of char''' type:
 
Note that '''TextFile''' type is very different than the '''file of char''' type:
  
* '''file of char''' is just a simple sequence of chars (single-byte characters), and you can only read or write a 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.
+
* '''file of char''' is just a simple sequence of single byte characters and you can only read or write a single character at a time. That is, you can only call '''Read(F, C)''' or '''Write(F, C)''' where C is variable of type '''char'''.
  
* '''TextFile''' offers much more functions, and represents the usual concept 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.
+
* '''TextFile''' offers much more functions, and represents the usual concept 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. [[End_of_Line|Line endings]] 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:48, 7 March 2015

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

{$mode objfpc}{$H+}
var 
  MyFile: TextFile;
  s: string;
begin
  AssignFile(MyFile, 'a.txt');
  try
    reset(MyFile); 
    readln(MyFile, s);
    writeln('Text read from file: ', s)

  finally
    CloseFile(MyFile)
  end
end.

The variable representing the text file (MyFile in the example above) may be used to read from, write to, or both to the actual file. It must be tied to the actual file by a run-time library routine AssignFile. 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 by closing the file calling CloseFile.

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

  • file of char is just a simple sequence of single byte characters and you can only read or write a single character at a time. That is, you can only call Read(F, C) or Write(F, C) where C is variable of type char.
  • TextFile offers much more functions, and represents the usual concept 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 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