Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Writing strings/arrays to files in FORTRAN 77

  1. Sep 11, 2014 #1
    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.
     
  2. jcsd
  3. Sep 11, 2014 #2

    jedishrfu

    Staff: Mentor

    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 (Text):

    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
     
  4. Sep 11, 2014 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Code (Text):
    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 (Text):

    do i = 1,n
       write(16,6100) rri(i),eei(i)
    end do
     
     
  5. Sep 21, 2014 #4

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Writing strings/arrays to files in FORTRAN 77
  1. Write File - Fortran (Replies: 1)

Loading...