Fortran Help. Reading space delimited floats

  • Context: Fortran 
  • Thread starter Thread starter Rotarman
  • Start date Start date
  • Tags Tags
    Fortran Reading Space
Click For Summary
SUMMARY

The discussion centers on reading space-delimited double precision floats from a text file using Fortran95. The user initially struggled with parsing the data due to varying digit lengths and the presence of letters before the floats. The solution provided involves using the default format specifier with the read statement: read (unit_num, *) a, b, c, where a, b, and c are declared with the appropriate precision. This method successfully reads the data without issues, even with preceding letters.

PREREQUISITES
  • Understanding of Fortran95 syntax and data types
  • Familiarity with reading files in Fortran
  • Knowledge of format specifiers in Fortran
  • Basic concepts of double precision floating-point representation
NEXT STEPS
  • Explore advanced file I/O techniques in Fortran95
  • Learn about Fortran format specifiers and their applications
  • Investigate performance optimization strategies for reading large datasets in Fortran
  • Study error handling in Fortran file operations
USEFUL FOR

This discussion is beneficial for Fortran developers, researchers working with numerical data, and anyone looking to efficiently read and process large datasets in Fortran95.

Rotarman
Messages
3
Reaction score
0
Hi all, long time lurker: finally have a question I couldn't find already answered.

I'm doing some work with Fortran95 for a research project, and I'm a complete noob, so pardon me. The issue is that I have some data I'd like to read from a text file in double precision that is between 7 and 10 digits long on a single line (ex: 156.254569 25.485975 2.698757) and no pattern to how many digits (1-3) before the decimal. I'd thought about maybe doing something funky like turning them into strings then breaking it up into three strings somehow and turning each back into a float, but I'm not entirely sure how I'd go about it. As well, there are ~30k lines and each will be read at least 3 times for computations, so I'd like to keep it as simple as possible to speed up calculations. Anyone have a neat solution (or an ugly one) for this?

Thanks in advance
 
Technology news on Phys.org
Have you tried using the default format that a * format specifier gives you?

read (unit_num, *) a, b, c

(with a, b, c declared as having the appropriate precision, of course)
 
This worked! I tried it without high hopes because there's letters before the floats on each line, but adding a dummy variable formatted as a string, it read everything without a hiccup. Thanks so much!
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 4 ·
Replies
4
Views
13K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
26
Views
5K