Fortran Fortran reading scientific form

AI Thread Summary
The discussion revolves around issues with reading double precision numbers from a Fortran output file. The user successfully writes numbers to a file using a specific format but encounters problems when trying to read them back, resulting in incorrect values. The output format used is '(6(ES17.10E3,2X))', which appears to be correctly applied during writing but fails during reading. Suggestions from other participants include ensuring the correct file is being accessed, closing and reopening the file after writing, and using a default input format instead of a specified one. Despite trying these suggestions, the user continues to experience unexpected output values, indicating a deeper issue with either the file handling or format specifications. The user is using OSX 10.6 and ifort 11.1, which may also be relevant to the problem.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
5
Views
5K
Replies
12
Views
3K
Replies
12
Views
1K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
5
Views
3K
Replies
6
Views
2K
Replies
1
Views
3K
Back
Top