In this section, we will explore how to manage file positioning in Fortran. File positioning is crucial when you need to read from or write to specific locations within a file. This is particularly useful for large files where you want to avoid reading the entire file into memory.
Key Concepts
-
File Positioning Basics:
- Understanding the file pointer.
- Moving the file pointer to a specific location.
- Reading from and writing to specific positions in a file.
-
Fortran Intrinsic Functions for File Positioning:
REWIND: Moves the file pointer to the beginning of the file.BACKSPACE: Moves the file pointer one record back.ENDFILE: Marks the end of the file.
-
Direct Access Files:
- Using direct access to read/write specific records.
- Understanding record length and its importance in direct access.
Practical Examples
Example 1: Using REWIND
The REWIND statement moves the file pointer to the beginning of the file.
PROGRAM RewindExample
IMPLICIT NONE
INTEGER :: i
CHARACTER(LEN=20) :: line
OPEN(UNIT=10, FILE='example.txt', STATUS='OLD')
! Read the first line
READ(10, '(A)') line
PRINT *, 'First read: ', line
! Rewind the file
REWIND(10)
! Read the first line again
READ(10, '(A)') line
PRINT *, 'Second read after rewind: ', line
CLOSE(10)
END PROGRAM RewindExampleExample 2: Using BACKSPACE
The BACKSPACE statement moves the file pointer one record back.
PROGRAM BackspaceExample
IMPLICIT NONE
INTEGER :: i
CHARACTER(LEN=20) :: line
OPEN(UNIT=10, FILE='example.txt', STATUS='OLD')
! Read the first line
READ(10, '(A)') line
PRINT *, 'First read: ', line
! Read the second line
READ(10, '(A)') line
PRINT *, 'Second read: ', line
! Backspace to the first line
BACKSPACE(10)
! Read the first line again
READ(10, '(A)') line
PRINT *, 'Read after backspace: ', line
CLOSE(10)
END PROGRAM BackspaceExampleExample 3: Using Direct Access
Direct access allows you to read/write specific records directly.
PROGRAM DirectAccessExample
IMPLICIT NONE
INTEGER :: i
CHARACTER(LEN=20) :: line
OPEN(UNIT=10, FILE='example.txt', STATUS='OLD', ACCESS='DIRECT', RECL=20)
! Write to the 5th record
WRITE(10, REC=5) 'This is record 5'
! Read the 5th record
READ(10, REC=5) line
PRINT *, 'Read from record 5: ', line
CLOSE(10)
END PROGRAM DirectAccessExampleExercises
Exercise 1: Rewind and Read
-
Create a file named
data.txtwith the following content:Line 1 Line 2 Line 3 -
Write a Fortran program that:
- Opens the file.
- Reads and prints the first line.
- Rewinds the file.
- Reads and prints the first line again.
Solution:
PROGRAM Exercise1
IMPLICIT NONE
CHARACTER(LEN=20) :: line
OPEN(UNIT=10, FILE='data.txt', STATUS='OLD')
! Read the first line
READ(10, '(A)') line
PRINT *, 'First read: ', line
! Rewind the file
REWIND(10)
! Read the first line again
READ(10, '(A)') line
PRINT *, 'Second read after rewind: ', line
CLOSE(10)
END PROGRAM Exercise1Exercise 2: Direct Access Read/Write
- Create a file named
records.txtwith at least 10 lines of text. - Write a Fortran program that:
- Opens the file in direct access mode.
- Writes a new line to the 3rd record.
- Reads and prints the 3rd record.
Solution:
PROGRAM Exercise2
IMPLICIT NONE
CHARACTER(LEN=20) :: line
OPEN(UNIT=10, FILE='records.txt', STATUS='OLD', ACCESS='DIRECT', RECL=20)
! Write to the 3rd record
WRITE(10, REC=3) 'New record 3'
! Read the 3rd record
READ(10, REC=3) line
PRINT *, 'Read from record 3: ', line
CLOSE(10)
END PROGRAM Exercise2Common Mistakes and Tips
- Incorrect Record Length: When using direct access, ensure the record length (
RECL) matches the length of the records in the file. - File Status: Always check the file status (
STATUS) when opening files to avoid overwriting or failing to open files. - File Positioning Commands: Use
REWIND,BACKSPACE, andENDFILEappropriately to manage file pointers effectively.
Conclusion
In this section, we covered the basics of file positioning in Fortran, including how to use REWIND, BACKSPACE, and direct access methods. These techniques are essential for efficient file handling, especially when dealing with large files. In the next section, we will delve into binary file operations, which will further enhance your file handling capabilities in Fortran.
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
