In this section, we will explore how to read data from files in Fortran. File handling is a crucial aspect of programming, especially when dealing with large datasets or when you need to persist data between program executions. We will cover the following topics:
- Opening a File
- Reading Data from a File
- Closing a File
- Practical Examples
- Exercises
- Opening a File
To read from a file in Fortran, you first need to open it. This is done using the OPEN statement. The OPEN statement associates a file with a unit number, which is used to reference the file in subsequent operations.
Syntax
unit: An integer that uniquely identifies the file.file: The name of the file to be opened.status: Specifies the status of the file. Use'old'for existing files.action: Specifies the action to be performed. Use'read'for reading.
Example
- Reading Data from a File
Once the file is opened, you can read data from it using the READ statement. The READ statement reads data from the file associated with the specified unit number.
Syntax
unit: The unit number associated with the file.fmt: The format in which the data is read. Use*for default formatting.variable_list: A list of variables where the read data will be stored.
Example
- Closing a File
After you have finished reading from the file, it is good practice to close it using the CLOSE statement. This ensures that all resources associated with the file are properly released.
Syntax
Example
- Practical Examples
Example 1: Reading Integers from a File
Suppose you have a file named integers.txt with the following content:
The following Fortran program reads these integers and prints them to the console.
PROGRAM ReadIntegers
IMPLICIT NONE
INTEGER :: num, i
OPEN(unit=10, file='integers.txt', status='old', action='read')
DO i = 1, 5
READ(unit=10, fmt=*) num
PRINT *, 'Read number:', num
END DO
CLOSE(unit=10)
END PROGRAM ReadIntegersExample 2: Reading Mixed Data Types
Suppose you have a file named data.txt with the following content:
The following Fortran program reads these pairs of integers and real numbers and prints them to the console.
PROGRAM ReadMixedData
IMPLICIT NONE
INTEGER :: num
REAL :: value
INTEGER :: i
OPEN(unit=10, file='data.txt', status='old', action='read')
DO i = 1, 3
READ(unit=10, fmt=*) num, value
PRINT *, 'Read number:', num, 'and value:', value
END DO
CLOSE(unit=10)
END PROGRAM ReadMixedData
- Exercises
Exercise 1: Reading Strings from a File
Create a file named names.txt with the following content:
Write a Fortran program to read these names and print them to the console.
Solution
PROGRAM ReadNames
IMPLICIT NONE
CHARACTER(LEN=20) :: name
INTEGER :: i
OPEN(unit=10, file='names.txt', status='old', action='read')
DO i = 1, 3
READ(unit=10, fmt=*) name
PRINT *, 'Read name:', name
END DO
CLOSE(unit=10)
END PROGRAM ReadNamesExercise 2: Reading Data into Arrays
Create a file named array_data.txt with the following content:
Write a Fortran program to read these numbers into a 2x5 array and print the array.
Solution
PROGRAM ReadArrayData
IMPLICIT NONE
INTEGER, DIMENSION(2, 5) :: array
INTEGER :: i, j
OPEN(unit=10, file='array_data.txt', status='old', action='read')
DO i = 1, 2
READ(unit=10, fmt=*) (array(i, j), j = 1, 5)
END DO
PRINT *, 'Array data:'
DO i = 1, 2
PRINT *, (array(i, j), j = 1, 5)
END DO
CLOSE(unit=10)
END PROGRAM ReadArrayDataConclusion
In this section, we have learned how to read data from files in Fortran. We covered the basics of opening, reading, and closing files, and provided practical examples and exercises to reinforce the concepts. Understanding file handling is essential for working with real-world data and building robust Fortran applications. In the next section, we will explore writing data to files, which complements the skills learned here.
Fortran Programming Course
Module 1: Introduction to Fortran
- Introduction to Fortran
- Setting Up the Development Environment
- Basic Syntax and Structure
- Writing Your First Fortran Program
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Input and Output
- Control Structures: If Statements
- Control Structures: Loops
Module 3: Arrays and Strings
Module 4: Procedures and Functions
Module 5: Advanced Data Structures
Module 6: File Handling
Module 7: Advanced Topics
Module 8: Best Practices and Optimization
- Code Optimization Techniques
- Debugging and Profiling
- Writing Maintainable Code
- Fortran Standards and Portability
