Fortran Data I/O problems in FORTRAN 77

  • Thread starter Thread starter Ashiley
  • Start date Start date
  • Tags Tags
    Data Fortran
Click For Summary
The discussion revolves around a coding issue related to reading and writing data files in a programming environment. The user encountered an error while trying to read data from a file named 'x.dat' and write to 'y.dat'. The error message indicated a mismatch in the expected input format. A participant pointed out that the input format specified in the code was incorrect, particularly the use of "3E13.7," which did not align with the actual data structure. They recommended using list-directed input, which simplifies the reading process by not requiring a FORMAT statement. Additionally, it was noted that opening the output file with status='new' could lead to issues if the file already exists; using the default status='unknown' would be more effective as it allows for either creating a new file or overwriting an existing one.
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.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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