Difference between revisions of "Basic Pascal Tutorial/Chapter 2/Files"

From Lazarus wiki
Jump to navigationJump to search
m (OSX->macOS; fixed syntax highlighting)
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{{Files}}
 
{{Files}}
 +
{{TYNavigator|Formatting_output|EOLN_and_EOF}}
  
 
2D - Files (author: Tao Yue, state: unchanged)
 
2D - Files (author: Tao Yue, state: unchanged)
  
 
Reading from a file instead of the console (keyboard) can be done by:
 
Reading from a file instead of the console (keyboard) can be done by:
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
read (file_variable, argument_list);
 
read (file_variable, argument_list);
 
write (file_variable, argument_list);
 
write (file_variable, argument_list);
Line 10: Line 11:
  
 
Similarly with <tt>readln</tt> and <tt>writeln</tt>. file_variable is declared as follows:
 
Similarly with <tt>readln</tt> and <tt>writeln</tt>. file_variable is declared as follows:
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
var
 
var
 
   ...
 
   ...
Line 24: Line 25:
  
 
Turbo Pascal introduced the assign notation. First you assign a filename to a variable, then you call <tt>reset</tt> or <tt>rewrite</tt> using only the variable.
 
Turbo Pascal introduced the assign notation. First you assign a filename to a variable, then you call <tt>reset</tt> or <tt>rewrite</tt> using only the variable.
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
assign (file_variable, 'filename.extension');
 
assign (file_variable, 'filename.extension');
 
reset (file_variable);
 
reset (file_variable);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The method of representing the path differs depending on your operating system. Windows uses backslashes and drive letters due to its DOS heritage (e.g. <tt>c:\directory\name.pas</tt>), while MacOS X and Linux use forward slashes due to their UNIX heritage.
+
The method of representing the path differs depending on your operating system. Windows uses backslashes and drive letters due to its DOS heritage (e.g. <tt>c:\directory\name.pas</tt>), while FreeBSD, macOS and Linux use forward slashes due to their UNIX heritage.
  
 
After you're done with the file, you can close it with:
 
After you're done with the file, you can close it with:
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
close (File_Identifier);
 
close (File_Identifier);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
Here's an example of a program that uses files. This program was written for Turbo Pascal and DOS, and will create file2.txt with the first character from file1.txt:
 
Here's an example of a program that uses files. This program was written for Turbo Pascal and DOS, and will create file2.txt with the first character from file1.txt:
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
program CopyOneByteFile;
 
program CopyOneByteFile;
  
Line 56: Line 57:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Formatting_output|EOLN_and_EOF}}
|[[Formatting_output|previous]] 
 
|[[Contents|contents]]
 
|[[EOLN_and_EOF|next]]
 
|}
 

Revision as of 11:48, 23 December 2019

български (bg) Deutsch (de) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

2D - Files (author: Tao Yue, state: unchanged)

Reading from a file instead of the console (keyboard) can be done by:

read (file_variable, argument_list);
write (file_variable, argument_list);

Similarly with readln and writeln. file_variable is declared as follows:

var
  ...
  filein, fileout : text;

The text data type indicates that the file is just plain text.

After declaring a variable for the file, and before reading from or writing to it, we need to associate the variable with the filename on the disk and open the file. This can be done in one of two ways. Typically:

reset (file_variable, 'filename.extension');
rewrite (file_variable, 'filename.extension');

reset opens a file for reading, and rewrite opens a file for writing. A file opened with reset can only be used with read and readln. A file opened with rewrite can only be used with write and writeln.

Turbo Pascal introduced the assign notation. First you assign a filename to a variable, then you call reset or rewrite using only the variable.

assign (file_variable, 'filename.extension');
reset (file_variable);

The method of representing the path differs depending on your operating system. Windows uses backslashes and drive letters due to its DOS heritage (e.g. c:\directory\name.pas), while FreeBSD, macOS and Linux use forward slashes due to their UNIX heritage.

After you're done with the file, you can close it with:

close (File_Identifier);

Here's an example of a program that uses files. This program was written for Turbo Pascal and DOS, and will create file2.txt with the first character from file1.txt:

program CopyOneByteFile;

var
   mychar : char;
   filein, fileout : text;

begin
   assign (filein, 'c:\file1.txt');
   reset (filein);
   assign (fileout, 'c:\file2.txt');
   rewrite (fileout);
   read (filein, mychar);
   write (fileout, mychar);
   close(filein);
   close(fileout)
end.
 ◄   ▲   ►