Given the length of a string in a file, how do I allocate a F90
character variable to hold it?
Thanks,
Ed
> Given the length of a string in a file, how do I allocate a F90
> character variable to hold it?
Allocatable string length is an f2003 feature (and one awfully slow to
get implemented in actual compilers). You can't do it in f90 (or f95),
at least not simply.
I think I might have seen a horribly ugly hack that involved allocating
a character array and using argument sequence association to map a
character string onto the allocated memory. It might have been by James
Buskirk, but I wouldn't swear to that. (It being a TRANSFER-like trick,
he comes to mind). It isn't something I'd do.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
> horribly ugly hack
> James
> Buskirk TRANSFER
;)
--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
> "Richard Maine" <nos...@see.signature> wrote in message
> news:1j5d8ns.1gwqs5i1afydt4N%nos...@see.signature...
>
> > horribly ugly hack
> > James
> > Buskirk TRANSFER
>
> ;)
Oh dear! I tried real hard to both say what I thought about it, but not
sound insulting. It occurred to me that there was a potential danger of
that. :-(
> Oh dear! I tried real hard to both say what I thought about it, but not
> sound insulting. It occurred to me that there was a potential danger of
> that. :-(
I wasn't insulted, just amused at being equated with Demon Rum in
your sermon. :)
You mean using fortran 90 rules, right? Under F90 you can't do exactly
what you are thinking. I wish you could. However there are several
useful approximations. Following from memory, untested...
Method 1. Over allocate and trim.
character line*200 ! longer than longest expected string
read (infile, '(a)') line
print *, '[' // trim (line) // ']'
Method 2. Over allocate and substring.
character line*200 ! longer than longest expected string
integer len1
read (infile, '(a)') line
len1 = len_trim (line) ! get len by any method desired
print *, '[' // line(1:len1) // ']'
Method 3. Character*1 allocated array. Cumbersome...
character*1, allocatable :: chars(:)
integer len1
len1 = ... predetermined, you said "given the length'...
allocate (chars(len1))
read (infile, '(9999a1)') chars ! 9999 longer than max possible
! must be expected length on one line within file
print *, '[', chars, ']' ! unsure, may require further handling
Method 4. Use subroutine interface to make conformance.
Within subroutine only, string will be expected length.
integer len1
len1 = ... predetermined, you said "given the length'...
call read_string (len1)
subroutine read_string (len2)
integer len2
character(len2) str
read (infile, '(a)') str
print *, '[' // str // ']'
end subroutine read_string
I use a mixture of 1 and 2 all the time to get real work done. I do not
recommend 3 at all, 4 only when really necessary. HTH.
--Dave
As others have already said you can't do this in F90. You could try
the ISO Varying String module:
http://www.fortran.com/iso_varying_string.f95
which may be suitable for your purpose.
Regards,
Simon
An alternative is to use the following:
length = determine_string_length( filename )
call handle_file( filename, length)
subroutine handl_file( filename, length )
character(len=*) :: filename
integer :: length
character(len=length) :: buffer
... use the local variable buffer to read the file
... line by line
end subroutine
Regards,
Arjen
It's something that's been available in PL/I since 1966.
Read N and then call SUB:
SUBROUTINE SUB(N)
IMPLICIT NONE
INTEGER :: N
CHARACTER (LEN=N) :: S
READ *, S
PRINT *, S
END SUBROUTINE SUB