Reading Real Numbers in Fortran 95: Tips and Tricks

  • Context: Fortran 
  • Thread starter Thread starter Sirluke
  • Start date Start date
  • Tags Tags
    fortran
Click For Summary
SUMMARY

The discussion focuses on reading a row of real numbers from a text file in Fortran 95. The user initially attempts to read the data using a formatted read statement, which results in incorrect values. The solution provided is to utilize a list-directed read by replacing the formatted read statement with 'read(1,*),' which successfully reads the data into the specified vector. This method is confirmed to work effectively for reading seven real numbers from the file.

PREREQUISITES
  • Understanding of Fortran 95 syntax and file I/O operations
  • Familiarity with formatted and list-directed input methods in Fortran
  • Basic knowledge of handling arrays in Fortran
  • Experience with reading data from text files
NEXT STEPS
  • Explore advanced Fortran 95 file handling techniques
  • Learn about error handling in Fortran I/O operations
  • Investigate the use of derived types for complex data structures in Fortran
  • Study performance optimization techniques for reading large datasets in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, researchers working with numerical data, and anyone involved in scientific computing who needs to read and process real numbers from text files.

Sirluke
Messages
3
Reaction score
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
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.)
 
Thank you very much :-) The list-directed works perfectly!
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
9K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K