Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open a file Programmatically in Fortran.

Posted on 2006-11-01
5
Medium Priority
?
331 Views
Last Modified: 2013-11-08
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
Comment
Question by:Aiysha
  • 2
3 Comments
 
LVL 11

Expert Comment

by:cup
ID: 17853927
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
 

Author Comment

by:Aiysha
ID: 17853979
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
 
LVL 11

Accepted Solution

by:
cup earned 2000 total points
ID: 17872978
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question