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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
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...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

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