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:

  1. Opening a File
  2. Reading Data from a File
  3. Closing a File
  4. Practical Examples
  5. Exercises

  1. 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

OPEN(unit=unit_number, file='filename', status='old', action='read')
  • 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

OPEN(unit=10, file='data.txt', status='old', action='read')

  1. 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

READ(unit=unit_number, fmt=format) variable_list
  • 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

INTEGER :: num
REAL :: value

READ(unit=10, fmt=*) num, value

  1. 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

CLOSE(unit=unit_number)

Example

CLOSE(unit=10)

  1. Practical Examples

Example 1: Reading Integers from a File

Suppose you have a file named integers.txt with the following content:

1
2
3
4
5

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 ReadIntegers

Example 2: Reading Mixed Data Types

Suppose you have a file named data.txt with the following content:

1 2.5
2 3.6
3 4.7

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

  1. Exercises

Exercise 1: Reading Strings from a File

Create a file named names.txt with the following content:

Alice
Bob
Charlie

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 ReadNames

Exercise 2: Reading Data into Arrays

Create a file named array_data.txt with the following content:

1 2 3 4 5
6 7 8 9 10

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 ReadArrayData

Conclusion

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.

© Copyright 2024. All rights reserved