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

From Free Pascal wiki
Jump to navigationJump to search
m (bypass language bar/categorization template redirect [cf. discussion])
 
(9 intermediate revisions by 7 users not shown)
Line 1: Line 1:
2D - Files (author: Tao Yue, state: unchanged)
+
{{Basic Pascal Tutorial/Chapter 2/Files}}
 +
{{TYNavigator|Chapter 2/Formatting output|Chapter 2/EOLN and EOF}}
 +
 
 +
2D - Files (author: Tao Yue, state: changed)
  
 
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:
<delphi>
+
<syntaxhighlight lang="pascal">
read (file_variable, argument_list);
+
read(File_variable, Argument_list);
write (file_variable, argument_list);
+
write(File_variable, Argument_list);
</delphi>
+
</syntaxhighlight>
  
Similarly with <tt>readln</tt> and <tt>writeln</tt>. file_variable is declared as follows:
+
Similarly with <tt>readln</tt> and <tt>writeln</tt>.
<delphi>
+
The output is stored into the variables named in <tt>Argument_list</tt>. <tt>File_variable</tt> is declared as follows:
 +
<syntaxhighlight lang="pascal">
 
var
 
var
 
   ...
 
   ...
   filein, fileout : text;
+
   Filein, Fileout : text;
</delphi>  
+
</syntaxhighlight>  
  
 
The <tt>text</tt> data type indicates that the file is just plain text.
 
The <tt>text</tt> 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:
 
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');
+
  reset(File_variable, 'filename.extension');
  rewrite (file_variable, 'filename.extension');
+
  rewrite(File_variable, 'filename.extension');
 
<tt>reset</tt> opens a file for reading, and rewrite opens a file for writing. A file opened with <tt>reset</tt> can only be used with <tt>read</tt> and <tt>readln</tt>. A file opened with <tt>rewrite</tt> can only be used with <tt>write</tt> and <tt>writeln</tt>.
 
<tt>reset</tt> opens a file for reading, and rewrite opens a file for writing. A file opened with <tt>reset</tt> can only be used with <tt>read</tt> and <tt>readln</tt>. A file opened with <tt>rewrite</tt> can only be used with <tt>write</tt> and <tt>writeln</tt>.
  
 
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.
<delphi>
+
<syntaxhighlight lang="pascal">
assign (file_variable, 'filename.extension');
+
assign(File_variable, 'filename.extension');
reset (file_variable);
+
reset(File_variable);
</delphi>
+
</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:
<delphi>
+
<syntaxhighlight lang="pascal">
close (File_Identifier);
+
close (File_variable);
</delphi>
+
</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:
<delphi>
+
<syntaxhighlight lang="pascal">
 
program CopyOneByteFile;
 
program CopyOneByteFile;
  
 
var
 
var
   mychar : char;
+
   Mychar : char;
   filein, fileout : text;
+
   Filein, Fileout : text;
  
 
begin
 
begin
   assign (filein, 'c:\file1.txt');
+
   assign(Filein, 'c:\file1.txt');
   reset (filein);
+
   reset(Filein);
   assign (fileout, 'c:\file2.txt');
+
   assign(Fileout, 'c:\file2.txt');
   rewrite (fileout);
+
   rewrite(Fileout);
   read (filein, mychar);
+
   read(Filein, Mychar);
   write (fileout, mychar);
+
   write(Fileout, Mychar);
   close(filein);
+
   close(Filein);
   close(fileout)
+
   close(Fileout)
 
end.  
 
end.  
</delphi>
+
</syntaxhighlight>
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Chapter 2/Formatting output|Chapter 2/EOLN and EOF}}
|[[Formatting_output|previous]] 
 
|[[Contents|contents]]
 
|[[EOLN_and_EOF|next]]
 
|}
 

Latest revision as of 15:18, 20 August 2022

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

 ◄   ▲   ► 

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

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. The output is stored into the variables named in Argument_list. 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_variable);

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.
 ◄   ▲   ►