File handling is an essential aspect of programming, allowing programs to read from and write to files. In ALGOL, file handling involves opening files, reading data, writing data, and closing files. This section will cover the basics of file handling in ALGOL, including practical examples and exercises to reinforce the concepts.
Key Concepts
-
File Operations:
- Opening a file
- Reading from a file
- Writing to a file
- Closing a file
-
File Modes:
- Read mode
- Write mode
- Append mode
-
Error Handling:
- Checking for file existence
- Handling read/write errors
File Operations
Opening a File
To perform any operation on a file, it must first be opened. In ALGOL, this is done using the open
statement.
file_name
: The name of the file to be opened.mode
: The mode in which the file is to be opened (read, write, append).
Reading from a File
To read data from a file, use the read
statement.
file_name
: The name of the file to read from.variable
: The variable where the read data will be stored.
Writing to a File
To write data to a file, use the write
statement.
file_name
: The name of the file to write to.data
: The data to be written to the file.
Closing a File
After performing the necessary operations, the file should be closed using the close
statement.
file_name
: The name of the file to be closed.
Practical Example
Let's look at a practical example that demonstrates how to open a file, write data to it, read data from it, and then close the file.
Example: Writing and Reading a File
begin string file_name; string data_to_write; string data_read; file_name := "example.txt"; data_to_write := "Hello, ALGOL!"; % Open the file in write mode open(file_name, "write"); % Write data to the file write(file_name, data_to_write); % Close the file close(file_name); % Open the file in read mode open(file_name, "read"); % Read data from the file read(file_name, data_read); % Close the file close(file_name); % Output the read data print(data_read); end;
Explanation
- Opening the File: The file
example.txt
is opened in write mode. - Writing Data: The string "Hello, ALGOL!" is written to the file.
- Closing the File: The file is closed after writing.
- Reopening the File: The file is reopened in read mode.
- Reading Data: The data is read from the file into the variable
data_read
. - Closing the File: The file is closed after reading.
- Outputting Data: The read data is printed to the console.
Exercises
Exercise 1: Write and Read Multiple Lines
Task: Modify the example to write multiple lines to the file and then read and print each line.
Solution:
begin string file_name; string data_to_write[3]; string data_read; integer i; file_name := "example.txt"; data_to_write[1] := "Line 1: Hello, ALGOL!"; data_to_write[2] := "Line 2: Learning file handling."; data_to_write[3] := "Line 3: This is fun!"; % Open the file in write mode open(file_name, "write"); % Write multiple lines to the file for i := 1 step 1 until 3 do write(file_name, data_to_write[i]); end for; % Close the file close(file_name); % Open the file in read mode open(file_name, "read"); % Read and print each line from the file for i := 1 step 1 until 3 do read(file_name, data_read); print(data_read); end for; % Close the file close(file_name); end;
Exercise 2: Error Handling
Task: Implement error handling to check if the file exists before reading from it.
Solution:
begin string file_name; string data_read; boolean file_exists; file_name := "example.txt"; % Check if the file exists file_exists := file_exists(file_name); if file_exists then % Open the file in read mode open(file_name, "read"); % Read data from the file read(file_name, data_read); % Close the file close(file_name); % Output the read data print(data_read); else print("Error: File does not exist."); end if; end;
Summary
In this section, we covered the basics of file handling in ALGOL, including opening, reading, writing, and closing files. We also looked at practical examples and exercises to reinforce these concepts. Understanding file handling is crucial for managing data in real-world applications, and mastering these skills will enable you to handle files efficiently in your ALGOL programs.