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:
- Opening a File for Writing
- Writing Data to a File
- Closing a File
- Practical Examples
- Exercises
- 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:
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:
- 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:
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:
- 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:
unit_number
: The unit number associated with the file.
Example:
- 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
- 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.
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