Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran File output of Array

Status
Not open for further replies.

wronski123

Technical User
Mar 14, 2012
3
DE
Dear forum members

I have recently encountered a problem when trying to write arrays on a fortran formatted output file. I have arrays which are either integer or real, with the following descriptors F10.11 I20. If I try to print them with formatted print statement I obtain blanks, if I run out of array elements. Do you have idea how to overcome this. I have figured out a way how to do it with implied do loop and counting the number of remaining elements in the array, but I hope to find some more elegant solution here.
I am including small fortran source pls look at it’s output in order to see what my problem is.
Thank you in advance
Wronski

program main

implicit none

integer :: a(10)=99999,i,sb(10)=88888
real :: b(15) = 9.12345678911E+03


open(unit=111,file='test_fortran_print.txt',status='replace',action='write')

write(111,'(4I20)') a
write(111,'(4ES20.12)') b
write(111,'(4I20)') sb


close(111)




end program



 
When I run your program on Silverfrost, I get
Code:
               99999               99999               99999               99999
               99999               99999               99999               99999
               99999               99999
  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03
  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03
  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03
  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03
               88888               88888               88888               88888
               88888               88888               88888               88888
               88888               88888
You haven't got something strange like white text on a white background have you? Try selecting everything in an editor. Are your numbers there or do you get blanks in the inverted colour?
 
I am not sure what else you were expecting...the behavior that you are witnessing is the correct one.

By the way...what exactly do you mean when you say that you get blanks? Do you actually get blank spaces? Or does the last line actually ends after the last value written and then nothing else?...in which case you are not obtaining blanks...you are obtaining nothing...which is the correct thing since the array has no more values to be written.

The way I say, fortran is doing you a favor, here...after all, you just said to write 4 values; yet, fortran wants to help since you passed an array and, so, it goes a ahead and write ALL the elements in the array obeying your initial request of 4 items per line.



 
Hi and thank you all for your replies.

My problem is that I would like to print the arrays without the blanks. I would like to have the first array and when it runs out of elements the second one. Because I am using write(unit,’4FS20.11’) when it runs out of array elements it just prints blanks.
My question is how to print the arrays without the trailing blanks. What I am currently doing is counting the number of remaining elements and using implied do loop, where the loop bounds are properly chosen on the basis of the number of remaining elements.
 
I would like to have :
Code:
               99999               99999               99999               99999
               99999               99999               99999               99999
               99999               99999  9.123457031250E+03  9.123457031250E+03
  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03
  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03
  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03  9.123457031250E+03
  9.123457031250E+03               88888               88888               88888
               88888               88888               88888               88888
               88888               88888               88888

The question is how to do that in a reasonable manner
 
Problem is, you want to merge to half output records into one. I guess this would only be possible, if you frist write your records into a string and then print the string.

Something like

Code:
character*(600)   ! number of characters big enough to hold all (!) your output)

...

write (cBuffer, '(10ii20, 10ES20.12, 10i20)') a, b, sb
write (111, '(a80)') (cBuffer ((j-1) * 80 + 1 : j * 80), j = 1, 9)

...

Of course you may introduce variables to compute the different constants applied here, like number of elemts in arrays, number of characters per element and number of columns in output.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
... too fast in typing.

Code:
character*(640) cBuffer ! number of characters big enough to hold all (!) your output, must be a multiple of the length of one line of output

...

write (cBuffer, '(10ii20, 10ES20.12, 10i20)') a, b, sb
write (111, '(a80)') (cBuffer ((j-1) * 80 + 1 : j * 80), j = 1, 8)

...

You must make sure, that the implied do-loop does not reference more characters of cBuffer than allocated in type declaration.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top