Silverfrost Forums

Welcome to our forums

Problem using long character strings

5 Jul 2006 10:20 #830

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.

5 Jul 2006 11:26 #834

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.

6 Jul 2006 8:06 #840

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

6 Jul 2006 8:12 #841

Paul,

Sorry, I see that I can fix this by an ANSI extension: using RECL on sequential access files.

Simon

6 Jul 2006 8:15 #842

Simon

You are opening the files as unformatted, but then using a format to write and read character data.

6 Jul 2006 3:53 #843

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

6 Jul 2006 11:56 #844

Simon

An alternative may be to consider using READF@.

10 Jul 2006 12:50 #853

Simon

This bug has now been fixed for the next release.

Please login to reply.