Skip to content

Commit fbd7417

Browse files
committed
Allow to append to CSV files
1 parent d496ee4 commit fbd7417

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/csv_module.F90

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,22 +323,35 @@ end subroutine read_csv_file
323323
!
324324
! Use `initialize` to set options for the CSV file.
325325

326-
subroutine open_csv_file(me,filename,n_cols,status_ok)
326+
subroutine open_csv_file(me,filename,n_cols,status_ok,append)
327327

328328
implicit none
329329

330330
class(csv_file),intent(inout) :: me
331331
character(len=*),intent(in) :: filename !! the CSV file to open
332332
integer,intent(in) :: n_cols !! number of columns in the file
333333
logical,intent(out) :: status_ok !! status flag
334+
logical,intent(in),optional :: append !! Append if file exists
334335

335336
integer :: istat !! open `iostat` flag
337+
logical :: append_flag = .false.
338+
logical :: file_exists
336339

337340
call me%destroy()
338341

339342
me%n_cols = n_cols
340343

341-
open(newunit=me%iunit,file=filename,status='REPLACE',iostat=istat)
344+
if (present(append)) append_flag = append
345+
if (append_flag) then
346+
inquire (file=filename, exist=file_exists)
347+
if (file_exists) then
348+
open(newunit=me%iunit,file=filename,status='OLD',position='APPEND',iostat=istat)
349+
else
350+
open(newunit=me%iunit,file=filename,status='REPLACE',iostat=istat)
351+
end if
352+
else
353+
open(newunit=me%iunit,file=filename,status='REPLACE',iostat=istat)
354+
end if
342355
if (istat==0) then
343356
status_ok = .true.
344357
else

0 commit comments

Comments
 (0)