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

  1. 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.
  2. 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.
  3. 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 RewindExample

Example 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 BackspaceExample

Example 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 DirectAccessExample

Exercises

Exercise 1: Rewind and Read

  1. Create a file named data.txt with the following content:

    Line 1
    Line 2
    Line 3
    
  2. 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 Exercise1

Exercise 2: Direct Access Read/Write

  1. Create a file named records.txt with at least 10 lines of text.
  2. 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 Exercise2

Common 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, and ENDFILE appropriately 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.

© Copyright 2024. All rights reserved