• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Open a file Programmatically in Fortran.

I want a small code in Fortran that opens a .txt file when executed. I have Lahey90 V4.5 to compile this program.

Thank you.
0
Aiysha
Asked:
Aiysha
  • 2
1 Solution
 
cupCommented:
You didn't say whether you wanted to read from it or to write to it.

To open a file for reading on channel nnnn

open (nnnn, 'afile.txt', status='old')

To open for writing

open (nnnn, 'afile.txt', status='new')
0
 
AiyshaAuthor Commented:
cup,
I might be asking for too much, but can you please send me a entire program, so I can just compile it..I am very new to fortran..

Thank you.
0
 
cupCommented:
Sure

program main
    ! SRC is the input channel
    ! DST is the output channel
    integer SRC, DST
    parameter (SRC = 20, DST = 30)  ! any number will do
    integer i, isq

    ! Output a table of squares
    ! use unknown so you can run the program over and  over again
    open (DST, file='squares.txt', status='unknown')
    do i = 1, 10, 1
       isq = i * i
       write (DST, '(I2, I4)') i, isq
    end do
    close (DST)

    ! Read the file and print it
    open (SRC, file='squares.txt', status='old')
    do while (.true.)
       read (SRC, '(I2, I4)', end = 100) i, isq
       print *, i, isq
    end do
!   end of file reached
100 continue
    close (SRC)
    stop
end program
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now