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

Data I/O problems in FORTRAN 77

  1. May 23, 2012 #1
    Hello, every one:

    I have a piece of data like this:

    1999 1 1 0.0 1 1.0000000e+00 -9.9900000e+02 4.8646021e-01 0
    1999 1 1 0.5 1 1.0208334e+00 -9.9900000e+02 4.9180925e-01 0
    1999 1 1 1.0 1 1.0416666e+00 -9.9900000e+02 3.7898308e-01 0
    1999 1 1 1.5 1 1.0625000e+00 -9.9900000e+02 3.5607040e-01 0

    The first three column are integers, as well as the fifth (three digits) and the last one (one digit). I put them into a data file: x.dat and tried to read them in and output to another data file: y.dat, in which there is a comma between two columns.

    I used the following code but got error:
    INTEGER A(4), B(4), C(4), D(4), E(4)
    REAL F(4)
    DOUBLE PRECISION S1(4), S2(4), S3(4)

    OPEN (66, FILE = 'x.dat', STATUS = 'OLD')
    OPEN (9, FILE = 'y.dat', STATUS = 'NEW')

    READ (66, 20) A(1), B(1), C(1), F(1), D(1), S1(1), S2(1), S3(1), E(1)
    20 FORMAT(I4, I2, I2, F4.1, I3, 3E13.7, I1)

    WRITE(9, 30) S1(1), S2(1), S3(1)

    30 FORMAT(3(E13.7, ','))

    I didn't read all data in but the first row and I have error message:
    "writing sequential formatted external I0"
    "Aborted"

    Could some one here point out my mistakes? Thank you!

    Ashiley
     
  2. jcsd
  3. May 23, 2012 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    If the data you posted was an accurate cut-and-paste from the file, your input format is wrong - specifically "3E13.7" doesn't match the file. The second number is 14 characters long, and there are also blanks between the numbers.

    Assuming the data values are always separated by blanks, it's easier to use list-directed (or free format) input.
    READ (66, *) A(1), B(1), C(1), F(1), D(1), S1(1), S2(1), S3(1), E(1)
    No FORMAT statement required.

    Trying to open the output file with status='new' won't work if the file already exists, i.e. the program might work the first time you run it, but not second time. Unless you really want to protect yourself from accidentally overwriting an existing file, the default status='unknown' works fine. It either creates a new file, or overwrites an existing one.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Data I/O problems in FORTRAN 77
  1. Fortran 77 problem! (Replies: 7)

Loading...