From 9ac942dec743f18ad70508145e0fa87096aa6848 Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Fri, 27 Sep 2024 12:12:37 +0200 Subject: [PATCH] `loadtxt/savetxt`: do not require space after last entry --- src/stdlib_io.fypp | 47 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/src/stdlib_io.fypp b/src/stdlib_io.fypp index 7aceae2e2..542c31321 100644 --- a/src/stdlib_io.fypp +++ b/src/stdlib_io.fypp @@ -144,53 +144,28 @@ contains do i = 1, skiprows_ read(s, *) end do - - #:if 'real' in t1 + ! Default to format used for savetxt if fmt not specified. - fmt_ = optval(fmt, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") - - if ( fmt_ == '*' ) then - ! Use list directed read if user has specified fmt='*' - do i = 1, max_rows_ - read (s,*) d(i, :) - enddo - else - ! Otherwise pass default or user specified fmt string. - do i = 1, max_rows_ - read (s,fmt_) d(i, :) - enddo - endif + #:if 'real' in t1 + fmt_ = optval(fmt, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",:,1x))") #:elif 'complex' in t1 - ! Default to format used for savetxt if fmt not specified. - fmt_ = optval(fmt, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))") - if ( fmt_ == '*' ) then - ! Use list directed read if user has specified fmt='*' - do i = 1, max_rows_ - read (s,*) d(i, :) - enddo - else - ! Otherwise pass default or user specified fmt string. - do i = 1, max_rows_ - read (s,fmt_) d(i, :) - enddo - endif + fmt_ = optval(fmt, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",:,1x))") #:else - ! Default to list directed for integer fmt_ = optval(fmt, "*") - ! Use list directed read if user has specified fmt='*' + #:endif + if ( fmt_ == '*' ) then + ! Use list directed read if user has specified fmt='*' do i = 1, max_rows_ read (s,*) d(i, :) enddo else - ! Otherwise pass default user specified fmt string. + ! Otherwise pass default or user specified fmt string. do i = 1, max_rows_ read (s,fmt_) d(i, :) enddo endif - #:endif - close(s) end subroutine loadtxt_${t1[0]}$${k1}$ @@ -222,11 +197,11 @@ contains s = open(filename, "w") do i = 1, size(d, 1) #:if 'real' in t1 - write(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") d(i, :) + write(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",:,1x))") d(i, :) #:elif 'complex' in t1 - write(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))") d(i, :) + write(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",:,1x))") d(i, :) #:elif 'integer' in t1 - write(s, "(*"//FMT_INT(1:len(FMT_INT)-1)//",1x))") d(i, :) + write(s, "(*"//FMT_INT(1:len(FMT_INT)-1)//",:,1x))") d(i, :) #:else write(s, *) d(i, :) #:endif