From: ralf.schaa on 14 May 2010 23:35 I am struggling to get something like the following to work - is it possible at all? cheers -ralf program main character( : ), allocatable :: theStr call GetStr( theStr ) contains subroutine GetStr( theStr ) character( : ), allocatable, intent( out ) :: theStr ! open a file etc. ! read the string from the file ! dynamic allocation of 'theStr' ??? read( funit, * ) theStr end subroutine GetStr end program main
From: Jim Xia on 14 May 2010 23:45 > > program main > character( : ), allocatable :: theStr > call GetStr( theStr ) > > contains > > subroutine GetStr( theStr ) > character( : ), allocatable, intent( out ) :: theStr > > ! open a file etc. > > ! read the string from the file > ! dynamic allocation of 'theStr' ??? > read( funit, * ) theStr The answer is NO. READ statement never does an dynamic allocation automatically on its own. You have to allocate theStr before reading it. Cheers, Jim
From: Richard Maine on 14 May 2010 23:48 ralf.schaa <ralf.schaa(a)gmail.com> wrote: > I am struggling to get something like the following to work - is it > possible at all? Yes, but...... 1. It requires f2003 for multiple reasons. F95+TR isn't enough, as the TR doesn't include allocatable character length. 2. Yes, you can dynamically allocate theStr, but a read statement does *NOT* do so. People have asked for functionalities somewhat like that, but they don't exist. You can allocate with an ALLOCATE statement or with the f2003 allocate-on-assignment. You cannot do it with a READ statement. For example, one might modify the code shown as follows. > program main > character( : ), allocatable :: theStr > call GetStr( theStr ) > > contains > > subroutine GetStr( theStr ) > character( : ), allocatable, intent( out ) :: theStr character(1000) :: some_temp_str > > ! open a file etc. > > ! read the string from the file > ! dynamic allocation of 'theStr' ??? read( funit, * ) some_temp_str theStr = trim(some_temp_str) > > end subroutine GetStr > end program main This assumes that 1000 (or some other suitable size) is large enough for any string that will be encountered. I have no error checking. It also assumes that the dynamic allocation is just to get the string length "right" rather than to save space; if it is to save space then the some_temp_str defeats the purpose. My crude version also assumes that trim gets what is desired (in particular, that quoting was not used to get a value with significant trailing blanks). One could write more complicated versions with fewer restrictions by using non-advancing I/O to read the string in pieces, but that's significantly messier (and I'm not going to try to code it on the fly in a usenet posting). -- Richard Maine | Good judgment comes from experience; email: last name at domain . net | experience comes from bad judgment. domain: summertriangle | -- Mark Twain
From: ralf.schaa on 14 May 2010 23:59 Thanks Jim and Richard, I changed my code following Richard's advice and it works fine. Cheers -Ralf
From: robin on 15 May 2010 11:25 "ralf.schaa" <ralf.schaa(a)gmail.com> wrote in message news:9d0c8560-5f06-4ae5-af5b-57c78975f8f0(a)t14g2000prm.googlegroups.com... |I am struggling to get something like the following to work - is it | possible at all? | cheers | -ralf | | program main | character( : ), allocatable :: theStr | call GetStr( theStr ) | | contains | | subroutine GetStr( theStr ) | character( : ), allocatable, intent( out ) :: theStr | | ! open a file etc. | | ! read the string from the file | ! dynamic allocation of 'theStr' ??? | read( funit, * ) theStr You need to read the string into a temporary, Then you need to allocate "theStr", and then you can assign the temporary to it. | end subroutine GetStr | end program main
|
Next
|
Last
Pages: 1 2 Prev: abort on end-of-file Next: Need efficient Fortran equivalent to Matlab accumarray |