Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29276 Discussions

How to write a format list with character variables?

olle_onkel
Beginner
1,071 Views
Hello,

I want to write a matrix to a file. Therefore I want to use a formated WRITE to determine the numbers of columns and rows the matrix should appear within the file.
I have the integer variable: maxCol which holds the value for the number of columns.
My initial idea was it to write this value in a character variable strMaxCol:

write(strMaxCol,'(I4)')maxCol

to create a format list like:

write(2, 'strMaxCol//F7.3') MATRIX

Unfortunatly, without success.
Does anybody know how to implement this correctly?

Kindly, Rico.
0 Kudos
3 Replies
TimP
Honored Contributor III
1,071 Views
Quoting - olle.onkel
write(strMaxCol,'(I4)')maxCol

write(2, 'strMaxCol//F7.3') MATRIX

Close. Maybe you should build the entire format string and display it, so as to see and fix the bug. How about
write(strMaxCol,'("(7F",I4.0,".3)")')maxCol
write(*,*)strMaxCol
write(2, strMaxCol) MATRIX

Didn't the results give you a clue about the error in the last line? You've gone a bit out of your way to hide your intentions; you might have meant something somewhat different.
There have been known to exist systems where unit 2 is pre-assigned to some special meaning. You would then require an OPEN to take it over for normal usage, with no ability to recover the original system usage. Hence the standard expert advice not to use units 0 through 9 for your own purposes.
0 Kudos
olle_onkel
Beginner
1,071 Views
Quoting - tim18
Close. Maybe you should build the entire format string and display it, so as to see and fix the bug. How about
write(strMaxCol,'("7F",I4.0,".3")')maxCol
write(*,*)strMaxCol
write(2, strMaxCol) MATRIX

Didn't the results give you a clue about the error in the last line? You've gone a bit out of your way to hide your intentions; you might have meant something somewhat different.
There have been known to exist systems where unit 2 is pre-assigned to some special meaning. You would then require an OPEN to take it over for normal usage, with no ability to recover the original system usage. Hence the standard expert advice not to use units 0 through 9 for your own purposes.

Hello Tim,

thanx for replying immediatly. The way you described to build format list works! No problem to compile, BUT:
The program crashed at the statement:
write(2, strMaxCol) MATRIX

with the error:

1377F7.3
forrtl: info (58): format syntax error at or near 1377F7.3
forrtl: severe (62): syntax error in format, unit 2, file D:bjava1.txt
Image PC Routine Line Source
rreadwrite.exe 004AE08A Unknown Unknown Unknown
rreadwrite.exe 004AA669 Unknown Unknown Unknown
rreadwrite.exe 0041F68B Unknown Unknown Unknown
rreadwrite.exe 0041E84B Unknown Unknown Unknown
rreadwrite.exe 00414360 Unknown Unknown Unknown
rreadwrite.exe 00403E27 _MAIN__ 86 ReadInZwei2.F90
rreadwrite.exe 004B4A73 Unknown Unknown Unknown
rreadwrite.exe 00457A67 Unknown Unknown Unknown
rreadwrite.exe 0045793F Unknown Unknown Unknown
kernel32.dll 7C817067 Unknown Unknown Unknown

strMaxCol is '1377F7.3'

Any ideas why? I#m clueless. I tried a lot of tests in a small testprogram with oher and more simple formats. It is always compilable, so the syntax should be allright. But it crashes during runtime when attempting to WRITE.

Gradeful for ideas,
Rico
0 Kudos
Steven_L_Intel1
Employee
1,071 Views
You forgot the parentheses. They are a required part of the format string. Tim had them in his example.
0 Kudos
Reply