Section 12.8: Line Numbers#

Adapted from: “Introduction to Programming Using Fortran 95/2003/2008” by Ed Jorgensen (March 2018 / Version 3.0.51).

Program to add line numbers to each line in a text file#

program linenumbers

! declare variables

implicit none

integer             :: i, rdopst, wropst, rdst
character(30)       :: rdfile, wrfile
character(132)      :: line

! --------------------
! dsiplay initial header

print *
write (*,'(a)') "Line Number Example Program"

! --------------------
! Set rdfile to input_test_file.txt for Jupyter Notebook
rdfile = "input_test_file.txt"

open (  12, file = rdfile, status = "old",      &
        action = "read", position = "rewind",   &
        iostat = rdopst )

! Check if file exists
print * ! skip a line on screen
if (rdopst /= 0) then
    write (*, '(a,a)', advance = "no") "Unable to open input file -> ", rdfile
    stop
else
    write (*, '(a,a)', advance="no") "Opened input file for reading -> ", rdfile
end if
print * ! skip a line on screen

! --------------------
! Set wrfile to output_test_file for Jupyter Notebook

wrfile = "output_test_file.txt"

open (  14, file = wrfile, status = "replace",  &
        action = "write", position = "rewind",  &
        iostat = wropst )

print * ! skip a line on screen
if (wropst /= 0) then
    write (*, '(a,a)', advance="no") "Unable to open output file -> ", wrfile
    stop
else 
    write (*, '(a,a)', advance="no") "Opened output file for writing -> ", wrfile
end if
print * ! skip a line on screen

! --------------------
! Process input file and add line numbers.  Write result to output file
print * ! skip a line on screen
write (*, '(a)') "Processing input file and writing output file...."
print * ! skip a line on screen
i = 1
do
    ! read line from input file
    read (12, '(a)', iostat = rdst) line

    ! if end of file, exit loop
    if (rdst > 0) stop "read error"
    if (rdst < 0) exit

    write (*, '(a, a)', advance="no") "Read line from input file -> ", line

    ! write line number and line to output file
    write (14, '(i10, 2x, a)') i, line
    print * ! skip line on screen
    write (*, '(a, i10, 2x, a)', advance="no") "Wrote line to output file -> ", i, line
    print * ! skip line on sreen
    i = i + 1
        
end do

print * ! skip line on screen

! close files

close (12)
close (14)

end program linenumbers

The following explanation is given by “Code Copilot” within ChatGPT 4.

The Fortran program you’re inquiring about seems to involve reading data from an input file, manipulating or processing this data in some way, and then writing the output to another file, formatting it with line numbers. Based on the content of the input and output files, here’s a likely explanation:

Program Description#

  1. Input Handling: The program reads lines from an input file named input_test_file.txt, which contains the following text:

    this is a test 1
    this is another test line 345
    one more line if you please
    
  2. Processing: The main functionality of the program likely involves:

    • Reading each line from the input file.

    • Keeping track of the line numbers as it reads each line.

  3. Output Handling: The output of the program is written to another file named output_test_file.txt. Each line of the input file is prefixed with its line number in a formatted manner, shown as follows:

          1  this is a test 1
          2  this is another test line 345
          3  one more line if you please
    

    The line numbers are formatted to be right-aligned, possibly using formatted output capabilities in Fortran, which allows specifying field widths and alignments.

Likely Code Structure#

The program might include:

  • Opening and reading from the input file.

  • Looping through each line, incrementing a line counter.

  • Writing each line to the output file with the line number formatted to be right-aligned.

  • Closing both the input and output files after processing is complete.

This approach is commonly used in programs that need to display or log data with line numbers for easier tracking and referencing, particularly useful in contexts like debugging or data reviewing. If you need the actual Fortran code or further explanation on how it could be implemented, please let me know!

Program Compilation and Execution#

The above program is compiled and run using Fortran Package Manager (fpm):

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_12_8_Line_Numbers"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")
exec_status = os.system("fpm run 2>/dev/null")
Line Number Example Program

Opened input file for reading -> input_test_file.txt           

Opened output file for writing -> output_test_file.txt          

Processing input file and writing output file....

Read line from input file -> this is a test 1                                                                                                                    
Wrote line to output file ->          1  this is a test 1                                                                                                                    
Read line from input file -> this is another test line 345                                                                                                       
Wrote line to output file ->          2  this is another test line 345                                                                                                       
Read line from input file -> one more line if you please                                                                                                         
Wrote line to output file ->          3  one more line if you please