Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read from scratch file

84 views
Skip to first unread message

Beliavsky

unread,
Mar 12, 2022, 12:59:24 PM3/12/22
to
How do you read from a scratch file you have created? Running the program

program scratch_file
implicit none
integer, parameter :: inu = 10, outu = 11
integer :: i
open (unit=inu,status="scratch",action="write")
write (inu,*) 42
! close (inu) ! activating this line does not help
open (unit=outu,status="scratch",action="read",position="rewind")
read (outu,*) i
print*,"i =",i
end program scratch_file

I get a run-time error with gfortran of

At line 9 of file xscratch.f90 (unit = 11, file = 'C:\Users\myname\AppData\Local\Temp\gfortrantmpsHeKcN')
Fortran runtime error: End of file

and something similar for Intel Fortran.

gah4

unread,
Mar 12, 2022, 1:05:12 PM3/12/22
to
On Saturday, March 12, 2022 at 9:59:24 AM UTC-8, Beliavsky wrote:
> How do you read from a scratch file you have created? Running the program
>
> program scratch_file
> implicit none
> integer, parameter :: inu = 10, outu = 11
> integer :: i
> open (unit=inu,status="scratch",action="write")
> write (inu,*) 42
> ! close (inu) ! activating this line does not help
> open (unit=outu,status="scratch",action="read",position="rewind")
> read (outu,*) i
> print*,"i =",i
> end program scratch_file

Tradition is that when you close a scratch file, it goes away.
You get a new one next time you open one.

Otherwise:

WRITE, REWIND, READ


Beliavsky

unread,
Mar 12, 2022, 1:14:04 PM3/12/22
to
On Saturday, March 12, 2022 at 1:05:12 PM UTC-5, gah4 wrote:

> Tradition is that when you close a scratch file, it goes away.
> You get a new one next time you open one.
>
> Otherwise:
>
> WRITE, REWIND, READ

Got it, thanks. The following program works:

program scratch_file
implicit none
integer, parameter :: iu = 10, n = 25
integer :: i,isq(n)
open (unit=iu,status="scratch")
write (iu,"(i0)") (10*i**2,i=1,n)
rewind (iu)
read (iu,*) isq
print*,isq([1,n]) ! 10 6250
end program scratch_file

John

unread,
Mar 12, 2022, 2:25:49 PM3/12/22
to

John

unread,
Mar 12, 2022, 2:29:00 PM3/12/22
to
If you are planning on making extensive use of scratch files, there are some additional details at

https://fortranwiki.org/fortran/show/Scratch+Files




0 new messages