In this section, we will explore how to write data to files in Fortran. File handling is a crucial aspect of programming, especially when dealing with large datasets or when you need to save the output of your programs for later use. We will cover the following topics:

  1. Opening a File for Writing
  2. Writing Data to a File
  3. Closing a File
  4. Practical Examples
  5. Exercises

  1. Opening a File for Writing

To write data to a file in Fortran, you first need to open the file using the OPEN statement. The OPEN statement associates a file with a unit number, which is used to refer to the file in subsequent operations.

Syntax:

OPEN(unit=unit_number, file='filename', status='status', action='action')
  • unit_number: An integer that uniquely identifies the file.
  • filename: The name of the file.
  • status: Specifies the status of the file. Common values are 'new' (create a new file) and 'replace' (overwrite an existing file).
  • action: Specifies the action to be performed. For writing, use 'write'.

Example:

OPEN(unit=10, file='output.txt', status='replace', action='write')

  1. Writing Data to a File

Once the file is opened, you can write data to it using the WRITE statement. The WRITE statement sends data to the file associated with the specified unit number.

Syntax:

WRITE(unit_number, format) variable_list
  • unit_number: The unit number associated with the file.
  • format: Specifies the format of the output. It can be a format label or an asterisk (*) for default formatting.
  • variable_list: A list of variables to be written to the file.

Example:

INTEGER :: i
REAL :: x

i = 10
x = 3.14

WRITE(10, *) 'Integer:', i, 'Real:', x

  1. Closing a File

After writing data to a file, it is important to close the file using the CLOSE statement. This ensures that all data is properly saved and resources are released.

Syntax:

CLOSE(unit=unit_number)
  • unit_number: The unit number associated with the file.

Example:

CLOSE(unit=10)

  1. Practical Examples

Example 1: Writing Simple Data to a File

PROGRAM WriteSimpleData
  INTEGER :: unit_number
  INTEGER :: i
  REAL :: x

  unit_number = 10
  i = 42
  x = 2.718

  ! Open the file for writing
  OPEN(unit=unit_number, file='simple_data.txt', status='replace', action='write')

  ! Write data to the file
  WRITE(unit_number, *) 'Integer:', i
  WRITE(unit_number, *) 'Real:', x

  ! Close the file
  CLOSE(unit=unit_number)
END PROGRAM WriteSimpleData

Example 2: Writing an Array to a File

PROGRAM WriteArrayData
  INTEGER :: unit_number
  INTEGER, DIMENSION(5) :: array = (/1, 2, 3, 4, 5/)
  INTEGER :: i

  unit_number = 20

  ! Open the file for writing
  OPEN(unit=unit_number, file='array_data.txt', status='replace', action='write')

  ! Write array data to the file
  DO i = 1, 5
    WRITE(unit_number, *) 'Element', i, ':', array(i)
  END DO

  ! Close the file
  CLOSE(unit=unit_number)
END PROGRAM WriteArrayData

  1. Exercises

Exercise 1: Write a Program to Save User Input to a File

Write a Fortran program that prompts the user to enter their name and age, and then saves this information to a file named user_info.txt.

Solution:

PROGRAM SaveUserInfo
  INTEGER :: unit_number
  CHARACTER(LEN=50) :: name
  INTEGER :: age

  unit_number = 30

  ! Prompt the user for input
  PRINT *, 'Enter your name:'
  READ *, name
  PRINT *, 'Enter your age:'
  READ *, age

  ! Open the file for writing
  OPEN(unit=unit_number, file='user_info.txt', status='replace', action='write')

  ! Write user information to the file
  WRITE(unit_number, *) 'Name:', name
  WRITE(unit_number, *) 'Age:', age

  ! Close the file
  CLOSE(unit=unit_number)
END PROGRAM SaveUserInfo

Exercise 2: Write a Program to Save a Matrix to a File

Write a Fortran program that creates a 3x3 matrix and saves its elements to a file named matrix_data.txt.

Solution:

PROGRAM SaveMatrixData
  INTEGER :: unit_number
  REAL, DIMENSION(3,3) :: matrix
  INTEGER :: i, j

  unit_number = 40

  ! Initialize the matrix
  matrix = RESHAPE((/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/), (/3, 3/))

  ! Open the file for writing
  OPEN(unit=unit_number, file='matrix_data.txt', status='replace', action='write')

  ! Write matrix data to the file
  DO i = 1, 3
    DO j = 1, 3
      WRITE(unit_number, '(F6.2)', ADVANCE='NO') matrix(i, j)
      IF (j < 3) WRITE(unit_number, '(A)', ADVANCE='NO') ', '
    END DO
    WRITE(unit_number, *)
  END DO

  ! Close the file
  CLOSE(unit=unit_number)
END PROGRAM SaveMatrixData

Conclusion

In this section, we learned how to write data to files in Fortran. We covered the basics of opening a file for writing, writing data to the file, and closing the file. We also provided practical examples and exercises to reinforce the concepts. In the next section, we will explore file positioning and how to manipulate the file pointer for more advanced file operations.

© Copyright 2024. All rights reserved