Data I/O problems in FORTRAN 77

  • Context: Fortran 
  • Thread starter Thread starter Ashiley
  • Start date Start date
  • Tags Tags
    Data Fortran
Click For Summary
SUMMARY

The forum discussion addresses data input/output issues in FORTRAN 77, specifically related to reading and writing formatted data files. The user encountered errors when attempting to read data from 'x.dat' and write to 'y.dat' using a specific FORMAT statement. The solution provided includes using list-directed input with the command READ (66, *) instead of a FORMAT statement, and changing the output file status to STATUS='UNKNOWN' to avoid conflicts with existing files.

PREREQUISITES
  • Understanding of FORTRAN 77 syntax and data types
  • Familiarity with file handling in FORTRAN
  • Knowledge of formatted vs. list-directed input/output
  • Basic debugging skills in programming
NEXT STEPS
  • Learn about list-directed input in FORTRAN 77
  • Explore file handling techniques in FORTRAN, including file statuses
  • Investigate common data formatting issues in FORTRAN
  • Review error handling and debugging practices in FORTRAN 77
USEFUL FOR

FORTRAN developers, data analysts working with legacy systems, and programmers troubleshooting file I/O operations in scientific computing applications.

Ashiley
Messages
1
Reaction score
0
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
 
Technology news on Phys.org
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.
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K