Writing strings/arrays to files in FORTRAN 77

In summary, there are multiple ways to format and output your arrays to an output file in FORTRAN 77. You can use loops and logic statements, implied do loops, separate write statements, or the NAMELIST feature. Consider your desired output format and use the appropriate method to achieve it.
  • #1
1994Bhaskar
134
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.
 
Technology news on Phys.org
  • #2
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
 
  • #3
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
 
  • #4
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.
 
  • #5


As a scientist, my recommendation would be to consult the FORTRAN 77 documentation or seek help from a more experienced FORTRAN programmer. The format line does indeed determine the arrangement of the data in the output file, but it is important to understand how the format specifications work in FORTRAN 77 in order to achieve the desired output. It may also be helpful to use a debugger or print statements to check the values of the arrays before writing them to the file. Additionally, there may be built-in functions or libraries in FORTRAN 77 that can assist with formatting and writing arrays to files. It is important to thoroughly understand the language and its capabilities in order to effectively utilize it for scientific purposes.
 

1. How do I write a string to a file in FORTRAN 77?

To write a string to a file in FORTRAN 77, you can use the WRITE statement, specifying the string variable and the file unit number. For example: WRITE(10,*) 'Hello World!' will write the string Hello World! to the file with unit number 10.

2. How do I write an array to a file in FORTRAN 77?

To write an array to a file in FORTRAN 77, you can use the WRITE statement, specifying the array variable and the file unit number. For example: WRITE(10,*) array will write the entire array to the file with unit number 10.

3. Can I specify the format of the string or array when writing to a file in FORTRAN 77?

Yes, you can specify the format of the string or array using the FMT option in the WRITE statement. For example: WRITE(10, '(A10)') string will write the string with a maximum length of 10 characters to the file with unit number 10.

4. How do I open a file for writing in FORTRAN 77?

To open a file for writing in FORTRAN 77, you can use the OPEN statement, specifying the file unit number, file name, and STATUS='NEW' to create a new file. For example: OPEN(10, FILE='output.txt', STATUS='NEW') will open a new file named output.txt for writing with the file unit number 10.

5. How do I close a file after writing in FORTRAN 77?

To close a file after writing in FORTRAN 77, you can use the CLOSE statement, specifying the file unit number. For example: CLOSE(10) will close the file with unit number 10.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
Replies
58
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
7K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Astronomy and Astrophysics
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top