Fortran reading scientific form

Click For Summary

Discussion Overview

The discussion revolves around issues related to reading and writing double precision numbers in Fortran using formatted I/O. Participants are exploring the discrepancies encountered when reading back data from a file that was previously written, focusing on the specific formatting used and the behavior of the read operations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a problem with reading double precision numbers from a file, noting that the output does not match the expected values.
  • Another participant requests specific examples of the numbers written and the discrepancies observed when reading them back.
  • A participant suggests that if the file is being accessed by separate programs, the correct file must be confirmed for reading.
  • It is proposed that after writing to the file, it should be closed and reopened to ensure reading starts from the beginning, with a mention of a REWIND statement as a potential solution.
  • One participant mentions that when data is separated by blanks, the default input format may suffice, suggesting a simpler read command without specifying a format.
  • Another participant shares their experience with changing input numbers and observing unexpected changes in the output, indicating a potential issue with the reading process.
  • After trying the default format for reading, the participant reports that all output values are returned as zero, which raises further questions about the reading mechanism.
  • One participant provides their operating system and compiler version, possibly to contextualize their environment for troubleshooting.

Areas of Agreement / Disagreement

Participants express various hypotheses regarding the reading and writing process, but no consensus is reached on the underlying cause of the discrepancies. Multiple competing views on how to address the issue remain present throughout the discussion.

Contextual Notes

Participants have not resolved the assumptions regarding file handling, the impact of formatting on reading and writing, and the specific behavior of the Fortran I/O system in their respective environments.

Axios
Messages
4
Reaction score
0
Hi,

I have been struggling with the Fortran IO for a while. Here is the question, any comments will be much appreciated.

I output some double precision numbers with the format:

write (file_id,'(6(ES17.10E3,2X))'), a,b,c,d,e,f ! say the filename is 'data.dat'

It works fine.

But when I open this file with the command

open (unit = file_id_2, file = "data.dat", status="old")
read (file_id_2,'(6(ES17.10E3,2X))'), a,b,c,d,e,f

I cannot read back what I previously write to the file correctly. Is there any obvious mistake that I made?

Any comments will be appreciated. Thanks!
 
Technology news on Phys.org
Can you give some specific examples of numbers that you wrote to the file and what they looked like when you read them in again? (That is, how are they "wrong"?)
 
jtbell said:
Can you give some specific examples of numbers that you wrote to the file and what they looked like when you read them in again? (That is, how are they "wrong"?)

The input file looks like this: (with "B" representing the blank, and there are in total 6 number per line)
Code:
0.1234567000E+000BB1.0378216950E+000BB1.0378779900E-009BB...

These numbers are output previously by using the command:
Code:
write (file_id,'(6(ES17.10E3,2X))'), a,b,c,d,e,f

The problem is, when I use the command
Code:
read (file_id_temp,'(6(ES17.10E3,2X))'), a,b,c,d,e,f
(file_id and file_id_temp refers to the same file, I checked this.)

what the code reads are (I just show the first three number corresponding to the one shown above in the input file)
Code:
1.0399802540E+009BB1.0656704870E+009BB8.1465474200E+008BB...

It seems that the code reads something, but just not reading correctly...

Any comments will be appreciated, thanks.

--Axios
 
I'm just guessing wildly here:

1. If the file is being written and read by separate programs, are you sure you're opening the correct file for reading?

2. If the the file is being written and read in the same program: after you finish writing to the file, you need to close it and re-open it so it reads from the beginning again. I also seem to remember a REWIND statement that resets the file to its beginning, but it's been a long time since I used it.

3. When the input data is separated by blanks, you normally don't even need to specify an input format, but can use the default format instead. Try:

Code:
read (file_id_temp,*), a,b,c,d,e,f
 
jtbell said:
I'm just guessing wildly here:

1. If the file is being written and read by separate programs, are you sure you're opening the correct file for reading?

2. If the the file is being written and read in the same program: after you finish writing to the file, you need to close it and re-open it so it reads from the beginning again. I also seem to remember a REWIND statement that resets the file to its beginning, but it's been a long time since I used it.

3. When the input data is separated by blanks, you normally don't even need to specify an input format, but can use the default format instead. Try:

Code:
read (file_id_temp,*), a,b,c,d,e,f

Thanks for the reply.

1. The reason I'm sure I'm reading the correct file is that when I changed the number in the file I meant to read, the output is changed, but changing in a wield way. For example, when I changed the first three number in the first line of input to:
Code:
1.1234567000E+000BB1.1378216950E+000BB1.0378779900E-009BB...
the output becomes:
Code:
1.0663888460E+009  1.0665093480E+009  8.1465474200E+008

while the original input is (also shown in previous post):
Code:
0.1234567000E+000BB1.0378216950E+000BB1.0378779900E-009BB...
and output is:
Code:
1.0399802540E+009BB1.0656704870E+009BB8.1465474200E+008BB...

We can see that when the input number changes, the output also changes, in a strange way though...

2. The file to read and the file to write are different, although the file to read was previously generated using the same output format of the same code, but in a different run.

3. Thanks for the comment on using * as the format, I tried this method, and the input and output are:

Code:
open (unit = file_id_temp, file = particle_filename, status="old")
read (file_id_temp,*), a,b,c,d,e,f

input:
Code:
1.1234567000E+000BB1.1378216950E+000BB1.0378779900E-009BB...
output:
Code:
1.0000000000E+000BB1.0000000000E+000BB0.0000000000E+000BB...

So...quite wield...

Thanks for the comments!

--Axios
 
I'm using OSX 10.6, and ifort 11.1, if that helps...thanks.
 

Similar threads

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