- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear forum,
I have a subroutine which reads a text input file. In this subroutine I need to read (repetitively)a character string S which has a length which is not known prior to the call to this subroutine (in essence, thelengthNis determined by the content of the file).
One way of dealing with this is of course to declare a string S of length much greater than the anticipated possible values of N, and then later on perform operations on S(1:N) only. But this is not very efficient when N can vary in a wide range of values, and when these character operations need to be repeated many, many times. It would be much better to *allocate* S to the right size (once it is known) - but I am not sure that what I am looking for corresponds to allocatable character variables.
Note: once the size of S (i.e., N) is determined by the subroutine, it doesn't change - and then millions of read/processing operations are performed with S.
Is there any convenient way to do this?
Olivier
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No, once the size of the string is determined (early on in the file) it does not change.
I looked at allocatable character variables... this is going to make me look real dumb I know... I did the following test case:
PROGRAM MAIN
IMPLICIT NONE
CHARACTER,ALLOCATABLE :: S(:)
ALLOCATE(S(10))
S = '0123456789'
WRITE(*,*) S
WRITE(*,*) S(1:10)
END PROGRAM MAIN
The output is
'0000000000'
'0000000000'
which is not that surprising, of course. But this is not what I want (I want to be able to use S just like a normal character variable, I guess, so that I can use S = 'xxxx' statements, as well as use regular character subroutines (TRIM, ADJUSTL, LEN_TRIM, SCAN etc).
I must be missing something very obvious!
Olivier
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
http://www.fortran.com/iso_varying_string.f95
helps.
Jrg Kuthe
www.qtsoftware.de
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[plain]PROGRAM MAIN IMPLICIT NONE CHARACTER(:),ALLOCATABLE :: S ALLOCATE(CHARACTER(10) :: S) S = '0123456789' WRITE(*,*) S WRITE(*,*) S(1:10) END PROGRAM MAIN[/plain]Do not use ISO_VARYING_STRING.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It looks like this is exactly what I want... But I can't get it now... I am going to have to kick and scream to get my hands on IVF 11 (still stuck with IVF 10).
So I suppose I'll have to stick with fixed length string variables.
I made a good note of this however. Thanks for the corrected sample program!
Olivier
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page