Reading Real Numbers in Fortran 95: Tips and Tricks

  • Fortran
  • Thread starter Sirluke
  • Start date
  • Tags
    fortran
In summary, the speaker needed help with reading a row of real numbers on a text file using Fortran. They pointed out that the numbers are chemical parameters with specific formatting. They then shared their attempted solution and the results they got. The expert summarizer suggested trying a list-directed read as a solution, which worked perfectly for the speaker.
  • #1
Sirluke
3
0
Hi Guys

I need some help with reading a row of real numbers on a text file using fortran. On my text file the row is:
74.05 112.91 154.03 193.90 236 276.71 61.12.
I would like to point out that these numbers are chemical parameters that can vary but they usually have up to two digits in the decimal part and three in the unit part. So if I write:

open(1,'file.txt)
.
read(1,100) vector
100 format (7(f6.2))

then i get very wrong results in reading the data, how can i fix this? Thank you very much
 
Technology news on Phys.org
  • #2
Try a list-directed read. Just replace the 'read(1,100) with read(1,*). If the dimension of vector is 7, it should work. Otherwise, you need to specify that it should only try to read 7 numbers: read(1,100) vector(1), vector(2), vector(3), vector(4), vector(5), vector(6), vector(7), (There are other ways to specify the 7 variables, but this should work.)
 
  • #3
Thank you very much :-) The list-directed works perfectly!
 

Related to Reading Real Numbers in Fortran 95: Tips and Tricks

1. What is the purpose of the Fortran 95 read statement?

The Fortran 95 read statement is used to read data from an external source, such as a file or user input, into a program. It allows the program to retrieve and store data in variables for further use.

2. How is the Fortran 95 read statement used?

The Fortran 95 read statement is used by specifying the input source, such as a file or user input, and the variables where the data will be stored. It is typically used in conjunction with the Fortran 95 write statement to create a data input/output process.

3. What is the syntax for the Fortran 95 read statement?

The syntax for the Fortran 95 read statement is as follows:
READ (unit, format) variable1, variable2, ...
Where "unit" is the input source, "format" specifies the format of the data, and "variable1, variable2, ..." are the variables where the data will be stored.

4. Can the Fortran 95 read statement read data from different types of sources?

Yes, the Fortran 95 read statement can read data from various sources such as files, user input, and other program variables. It is a versatile tool for data retrieval and can be used in different scenarios.

5. Are there any limitations to using the Fortran 95 read statement?

One limitation of the Fortran 95 read statement is that it can only read data in a specific format. If the format of the data in the input source does not match the format specified in the read statement, it may result in errors or incorrect data being stored in the variables. Additionally, the read statement may not be suitable for large amounts of data, as it can be time-consuming and inefficient to use for bulk data retrieval.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
20
Views
8K
  • Programming and Computer Science
Replies
13
Views
4K
Back
Top