Fortran reading scientific form

In summary,I have been struggling with the Fortran IO for a while. Here is the question, any comments will be much appreciated.It works fine.But when I open this file with the commandopen (unit = file_id_2, file = "data.dat", status="old")read (file_id_2,'(6(ES17.10E3,2X))'), a,b,c,d,e,fI 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!
  • #1
Axios
4
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
  • #2
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"?)
 
  • #3
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
 
  • #4
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
 
  • #5
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
 
  • #6
I'm using OSX 10.6, and ifort 11.1, if that helps...thanks.
 

Related to Fortran reading scientific form

1. What is Fortran and why is it used in scientific form?

Fortran is a programming language used in scientific computing for its efficient and powerful capabilities in handling mathematical and scientific calculations. It is commonly used in fields such as physics, engineering, and mathematics due to its speed, reliability, and ability to handle large amounts of data.

2. How do I read data in scientific form using Fortran?

To read data in scientific form using Fortran, you can use the "read" statement and specify the format of the data using the "E" format code, which stands for scientific notation. For example, "read(unit, '(E)') variable" will read in a value in scientific notation and assign it to the variable.

3. Can Fortran read data in other formats besides scientific form?

Yes, Fortran has various format codes that can be used to read data in different formats, such as integers, real numbers, and character strings. These include "I" for integer, "F" for floating-point, and "A" for character strings. You can also specify the width and precision of the data to be read using these format codes.

4. How do I handle errors when reading data in scientific form using Fortran?

In Fortran, you can use the "iostat" parameter in the "read" statement to check for any errors that may occur during data reading. If an error occurs, the value of "iostat" will be non-zero, and you can use it to handle the error accordingly. You can also use the "err" parameter to specify a custom error message.

5. Is there a way to control the output format when writing data in scientific form using Fortran?

Yes, you can use the "write" statement and specify the format of the data using the "E" format code, along with the desired width and precision. For example, "write(unit, '(E10.3)') variable" will write the value of the variable in scientific notation with a width of 10 characters and 3 decimal places. You can also use other format codes and control the output format accordingly.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top