Writing strings/arrays to files in FORTRAN 77

  • Context: Fortran 
  • Thread starter Thread starter 1994Bhaskar
  • Start date Start date
  • Tags Tags
    files Fortran Writing
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 4K views
1994Bhaskar
Messages
134
Reaction score
0
Hey guys i am a little confused in how to write arrays to a output file in FORTRAN 77. Here is the piece of code:
Please assume that rri and eei arrays have been defined correctly above

open (16,file='output.out')
write (16,6100) rri eei
format (5e16.8)

However in the output file i am getting pretty jumbled up data:
Something like this:
"
0.12000000E+03 0.20742290E-09 0.41478867E-09 0.82941372E-09 0.16583648E-08
0.33154214E-08 0.66271448E-08 0.13243838E-07 0.26458146E-07 0.52832895E-07
0.10543041E-06 0.21019709E-06 0.41852488E-06 0.83179448E-06 0.16488514E-05
0.32565192E-05 0.63985245E-05 0.12480863E-04 0.24097285E-04 0.45863812E-04
0.85564378E-04 0.15526781E-03 0.27122637E-03 0.44993835E-03 0.69682466E-03
0.98711330E-03 0.12499488E-02 0.13803427E-02 0.12944919E-02 0.99909339E-03
0.60891981E-03 0.27673235E-03 0.86567616E-04 0.16818065E-04 0.18057130E-05
0.95199966E-07 0.22493871E-08 0.23612230E-10 0.12315050E-12 0.37242291E-15
0.72817779E-18 0.99201003E-21 0.99685794E-24 0.77166103E-27 0.47569850E-30
0.23964260E-33 0.10068188E-36 0.35851855E-40 0.10959223E-43 0.29017715E-47
0.66667887E-51 0.13035555E-54 0.19643613E-58 0.59575854E-88 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00
"
As you guys can see that i can't make out from the final output file that what is rri and what was part of eei array.

My preferred format would be:
"
rri eei
0.12000000E+03 0.12000000E+03
0.10543041E- 060.32565192E-05
0.33154214E-08 0.33154214E-08
0.10543041E-06
0.32565192E-05
"
The values are just a representation not the actual. It's just to represent the format i want the contents to be written in the output file.
Now as much as i have understood till now the format line: format (5e16.8) decides this arrangement. So any known format which would help this problem ?
I am unfamiliar with fortran 77.
 
Physics news on Phys.org
you could write a textline with "rri" followed by the rri array of numbers
then write a textline with "eei" followed by the eei array of numbers.

Alternatively, you could write out your arrays using a do..loop and index thru the arrays:

Code:
write(16,6101) "rri"
do i=1,100
   write(16,6100) rri(i)
loop

write(16,6101) "eei"
do i=1,100
    write (16,6100) eei(i)
end do

Some general Fortran examples:

www.esm.psu.edu/~ajm138/fortranexamples.html

Lastly, you could write out your lines as a CSV file that can be imported into a spreadsheet or other programs if needed.

http://en.wikipedia.org/wiki/Comma-separated_values
 
Code:
write (16,6100) rri, eei
writes al the elements of rii, and then all the elements of eei. Your format statement puts 5 numbers on each line. You don't get a new line at the start of the eeii data.

If you want a table of numbers like
rri(1) eei(1)
rri(2) eei(2)
rri(3) eei(3)
etc

the easiest way to do write a loop, something like
Code:
do i = 1,n
   write(16,6100) rri(i),eei(i)
end do
 
It's not clear to me what logic determines your desired output format. Which lines have 2 numbers and which have 1? Are they pairing the values from the two arrays or one array completed before the other begins?

1) Logic to determine which lines have 2 numbers and which have 1 will require some logic statements in the loops that others have indicated. Ant the apropriate write statements for each.
2) If you just want to pair-up the two arrays, you can use an implied do loop: WRITE (16, 6100) (rri(i),eei(i), i=1,n)
3) If you just want to separate the arrays, you can use a separate write statements and write a blank line or label before each one.
4) If you just want easy outputs, you should look at the NAMELIST capability of FORTRAN. That feature is very useful for engineering work.