Skip to main content
Engineering LibreTexts

12.8: Example

  • Page ID
    54331
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this example, we will write a Fortran program to read an input file and write a line number and the original line to an output file.

    Understand the Problem

    For this problem, we will read the file names from the user and open the files. Then we will read a line from the input file, and write the line number and line to the output file. When done, we will close the file.

    Create the Algorithm

    For this problem, first, we will need to prompt for and read the file names from the user and ensure that they open correctly. If the file cannot be opened, an error message will be displayed and the file names will be re-read.

    ! declare variables
    !     integer -> i, rdopst, wropst
    !     character -> line
    ! display initial header
    ! loop
    !     prompt for input file name
    !     read input file name
    !     open input file (read access)
    !     if open unsuccessful, display error message
    !     otherwise, end loop
    ! loop
    !     prompt for output file name
    !     read output file name
    !     open output file (write access)
    !     if open unsuccessful, display error message
    !     otherwise, end loop
    

    Once the file is open, a line will be read from the input file, and the line number and the line will be written to the output file. For this example, we will assume that a line will have 132 or less characters.

    This process will continue until there are no more lines in the input file.

    ! loop
    !     read line from input file
    !     if end of file, exit loop
    !     write line number and line to output file
    ! close files
    

    For convenience, the steps are written as program comments.

    Implement the Program

    Based on the algorithm, the below program could be created.

    program linenumbers
    
    ! declare variables
    implicit none
    integer :: i, rdopst, wropst, rdst
    character(30) :: rdfile, wrfile
    character(132) :: line
    
    ! display initial header
        write (*,*) "Line Number Example"
    
    ! ----------
    ! prompt for input file name
        do
            write (*,'(a)', advance="no") "Input File Name: "
    
            ! read input file name
            read (*,*) rdfile
    
            ! open input file (read access)
            ! if open unsuccessful, display error message
            !     otherwise, end loop
            open(12, file=rdfile, status="old",              &
                        action="read", position="rewind",    &
                        iostat=rdopst )
            if (rdopst==0) exit
    
            write (*,'(a/,a)') "Unable to open input file.", &
                            "Please re-enter"
        end do
    
    ! ----------
    ! prompt for output file name
        do
            write (*,'(a)', advance="no") "Output File Name: "
    
            ! read output file name
            read (*,*) wrfile
    
            ! open output file (read access)
            ! if open unsuccessful, display error message
            !     otherwise, end loop
    
            open(14, file=wrfile, status="replace",          &
                        action="write", position="rewind",   &
                        iostat=wropst )
            if (wropst==0) exit
    
            write (*,'(a, a/,a)') "Unable to open ",         &
                            "output file.", "Please re-enter"
        end do
    
    ! ----------
    
        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 line number and line to output file
            write (14, '(i10,2x,a)') i, line
            i = i + 1
        end do
    
    ! close files
        close(12)
        close(14)
    
    end program linenumbers
    

    The spacing and indentation are not required, but help to make the program more readable.

    Test/Debug the Program

    For this problem, testing would involve executing the program with various input files and verifying that the line numbers are correctly added.


    This page titled 12.8: Example is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?