Reading formatted input in fortran

In summary, the user is seeking help on how to read exponential data in Fortran and is provided with the E format syntax and examples. The default format (*) can be used to read data separated by spaces. The user is also directed to another website for more details.
  • #1
agalya
9
0
format statement to read input in fortran

hello everyone,
I've to read this following line in fortran,
I don't know to read the exponential form
please help me

3.49E+03 2.73E+01 2.01E-01 9.16E-02 5.94E-02 5.11E-02 3.27E+04 3.27E+04

can somebody help me to write the format statement to read the whole line
or can please tell me how should I read the exponential data eg 3.49E+01 and 9.44E-09

thank you,
agalya
 
Last edited:
Technology news on Phys.org
  • #2
Taken from this site: http://elsa.berkeley.edu/sst/fmttop.html

The E format allows the user to enter data stored in scientific notation. The syntax of the E format is the same as the F format:

Ew.d

Here W is the total width of the variable, including exponent, while D indicates the number of implied decimal places in the mantissa. The data field should contain the mantissa and the letter E followed by an integer indicating the power of ten to which the mantissa is to be raised. For example, E6.0 would read `1234E2' as `123400' (1234.0 x 10^2), while E6.1 would read `1234E2' as `12340' (123.4 x 10^2). As before, any decimal actually coded overrides the format specification, so that both E6.0 and E6.1 would read `123.4E2' as `12340'. "

Try also this site, for more details: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html
 
  • #3
If the data is separated by spaces as you have here, you can use the default format (*):

Code:
      real a, b, c, d, e, f, g, h
      open (unit=10, file='agalya.txt')
      read (10, *) a, b, c, d, e, f, g, h
      write (*, *) a, b, c, d, e, f, g, h
      end

This "write" statement doesn't give you the numbers in the same format in which you read them; it uses its own default format. You can of course specify your own output format as needed.
 
Last edited:

1. How do I read formatted input in Fortran?

To read formatted input in Fortran, you can use the READ statement followed by the input variables and the format specifier. For example, "READ(5,*) A, B" will read two input values from the unit 5 and store them in variables A and B. You can specify the format of the input using the format specifier, which includes the data type, width, and decimal precision.

2. What is the difference between formatted and unformatted input in Fortran?

The main difference between formatted and unformatted input in Fortran is the way the data is stored in memory. Formatted input is read and stored in a human-readable format, while unformatted input is stored in binary format. This means that formatted input can be viewed and edited by humans, while unformatted input is more efficient for computer processing.

3. How can I specify the format of the input in Fortran?

You can specify the format of the input in Fortran using the format specifier in the READ statement. The format specifier includes the data type, width, and decimal precision of the input. For example, "READ(5, '(I5)') A" will read an integer value of width 5 from unit 5 and store it in variable A.

4. Can I read input from a file in Fortran?

Yes, you can read input from a file in Fortran using the OPEN statement to open the file and the READ statement to read the input values. You will need to specify the unit number for the file in the OPEN statement and use that unit number in the READ statement. Additionally, you will need to use the END statement to close the file after reading the input.

5. How does Fortran handle errors in formatted input?

If the input does not match the specified format in the READ statement, Fortran will return an error and stop the program. It is important to carefully specify the format of the input to avoid errors. You can also use the ERR and IOSTAT specifiers in the READ statement to handle errors and check if the input was read successfully.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
11
Views
7K
Back
Top