Question

I have an existing file written by a Fortran program (formatted) and I would like to add few lines at the beginning of the file. The idea is to do so without making a copy of the original file.

I could add a line at the end of the file with:

open(21,file=myfile.dat,status='old',action='write',
        form='formatted',position="append")
write(21,*) "a new line"

but when I tried:

open(21,file=myfile.dat,status='old',action='write',
        form='formatted',position="rewind")
write(21,*) "a new line"

it overwrites the whole file.

It might be impossible. At least, I would be glad to have a confirmation that it is effectively impossible.

Was it helpful?

Solution

Yes, it is impossible. With position= you only set the position for writing. Normally you just delete everything past the current record by a write in a sequential file. You may be able to adjust a record at the beginning in a direct access file, but also not just add something at the beginning. You have to make a copy first.

OTHER TIPS

If you are using un-formatted data and know how many lines to expect, try using the direct access file read/write method. This has the potential to store information for each line in a 'record' which can be accessed later much like an array.

In order to append to the beginning, simply create as many empty records as you will have lines in your 'header' at the beginning of the file then go back and change their values to the actual lines you want them to be later.

Example of Direct Access file io:

CHARACTER (20) NAME
INTEGER I
INQUIRE (IOLENGTH = LEN) NAME
OPEN( 1, FILE = 'LIST', STATUS = 'REPLACE', ACCESS = 'DIRECT', &
         RECL = LEN )

DO I = 1, 6
  READ*, NAME
  WRITE (1, REC = I) NAME             ! write to the file
END DO

DO I = 1, 6
  READ( 1, REC = I ) NAME             ! read them back
  PRINT*, NAME
END DO

WRITE (1, REC = 3) 'JOKER'            ! change the third record

DO I = 1, 6
  READ( 1, REC = I ) NAME             ! read them back again
  PRINT*, NAME
END DO

CLOSE (1)
END

code source, see section on "Direct Access Files": http://oregonstate.edu/instruct/ch590/lessons/lesson7.html

It is possible !!! Here is a sample program which could accomplish the task.

   ! Program to write after the end line of a an existing data file  
   ! Written in fortran 90
   ! Packed with an example

  program write_end
  implicit none
  integer :: length=0,i

  ! Uncomment the below loop to check example 
  ! A file.dat is created for EXAMPLE defined to have some 10 number of lines
  ! 'file.dat may be the file under your concern'.


  !      open (unit = 100, file = 'file.dat')
  !      do i = 1,10
  !      write(100,'(i3,a)')i,'th line'
  !      end do
  !      close(100) 

  ! The below loop calculates the number of lines in the file 'file.dat'.

  open(unit = 110, file = 'file.dat' )
   do  
       read(110,*,end=10)
       length= length + 1 
   end do
   10   close(110)

  ! The number of lines are stored in length and printed.

   write(6,'(a,i3)')'number of lines= ', length

  ! Loop to reach the end of the file.

   open (unit= 120,file = 'file.dat')
   do i = 1,length
       read(120,*)
   end do

  ! Data is being written at the end of the file...

   write(120,*)'Written in the last line,:)'
   close(120)
   end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top