View previous topic :: View next topic |
Author |
Message |
simon
Joined: 05 Jul 2006 Posts: 268
|
Posted: Wed Jul 05, 2006 11:20 am Post subject: Problem using long character strings |
|
|
There seems to be a problem using character strings with LEN exceeding 32768. Consider the following program as an example.
PROGRAM p
IMPLICIT NONE
INTEGER, PARAMETER :: lc=32769
CHARACTER(LEN=lc) :: c
!
c=REPEAT(' ',lc)
!
END PROGRAM p
This program crashes.
While there are obvious work-arounds for the above, the reason I want to use long character strings is to find the number of
arguments on the first line of a formatted input text file. The way I do this is to read in the first line as a long character, and
then see how many arguments I can read from this line. Perhaps there is a better way to do this.
|
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7938 Location: Salford, UK
|
Posted: Thu Jul 06, 2006 12:26 am Post subject: Problem using long character strings |
|
|
Simon
The problem in this code concerns the use of REPEAT with a large value for the second argument.
I presume that we could fix this bug but the limitation does not seem unreasonable.
Otherwise you should have no difficulty with long character variables.
If you do have problems, please send an example.
I assume that you know that in this case you can simply write
c = ' '
i.e. a single space, to get the same result, because character variables are always padded out with spaces. |
|
Back to top |
|
|
simon
Joined: 05 Jul 2006 Posts: 268
|
Posted: Thu Jul 06, 2006 9:06 am Post subject: Problem using long character strings |
|
|
Paul,
Many thanks for your response. Your suggested solutions are useful, but here
is another problem where I am experiencing difficulties with large strings:
PROGRAM p
IMPLICIT NONE
INTEGER, PARAMETER :: iin=11
INTEGER, PARAMETER :: lc=32769
CHARACTER(LEN=lc) :: c
!
c(1:lc)='a'
!
OPEN (UNIT=iin,FILE='tmp.txt',ACTION='write',FORM='formatted',STATUS='unknown')
WRITE (UNIT=iin,FMT='(A)') c(1:lc)
CLOSE (UNIT=iin)
!
OPEN (UNIT=iin,FILE='tmp.txt',ACTION='read',FORM='formatted',STATUS='old')
READ (UNIT=iin,FMT='(A)') c(1:lc)
CLOSE (UNIT=iin)
!
END PROGRAM p
The program will create the file, but will not read the character back in.
Simon
|
|
Back to top |
|
|
simon
Joined: 05 Jul 2006 Posts: 268
|
Posted: Thu Jul 06, 2006 9:12 am Post subject: Problem using long character strings |
|
|
Paul,
Sorry, I see that I can fix this by an ANSI extension: using RECL on sequential access files.
Simon |
|
Back to top |
|
|
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Thu Jul 06, 2006 9:15 am Post subject: Problem using long character strings |
|
|
Simon
You are opening the files as unformatted, but then using a format to write and read character data. _________________ John Horspool
Roshaz Software Ltd.
Gloucestershire |
|
Back to top |
|
|
simon
Joined: 05 Jul 2006 Posts: 268
|
Posted: Thu Jul 06, 2006 4:53 pm Post subject: Problem using long character strings |
|
|
John,
Thanks for responding. The file was explicitly opened as a formatted file for both the write and the read (see OPEN statements
on the earlier message). The solution is to use the RECL specifier even though the file is sequential. This is allowed in FTN95
even though it's not part of the ANSI standard. So the fixed code looks like this:
PROGRAM p
IMPLICIT NONE
INTEGER, PARAMETER :: iin=11
INTEGER, PARAMETER :: lc=32769
CHARACTER(LEN=lc) :: c
!
c(1:lc)='a'
!
OPEN (UNIT=iin,FILE='tmp.txt',ACTION='write',FORM='formatted',STATUS='unknown')
WRITE (UNIT=iin,FMT='(A)') c(1:lc)
CLOSE (UNIT=iin)
!
OPEN (UNIT=iin,FILE='tmp.txt',ACCESS='sequentual',ACTION='read',FORM='formatted',STATUS='old',RECL=lc)
READ (UNIT=iin,FMT='(A)') c(1:lc)
CLOSE (UNIT=iin)
!
END PROGRAM p |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7938 Location: Salford, UK
|
Posted: Fri Jul 07, 2006 12:56 am Post subject: Problem using long character strings |
|
|
Simon
An alternative may be to consider using READF@. |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7938 Location: Salford, UK
|
Posted: Mon Jul 10, 2006 1:50 am Post subject: Problem using long character strings |
|
|
Simon
This bug has now been fixed for the next release. |
|
Back to top |
|
|
|