Fortran 90 question about reading files with text

In summary, The format of the file includes text and adjacent numbers, with various formats for the numbers. The speaker is wondering how to read only the numbers without affecting the text. They suggest using DO loops to read each format separately and provide a tutorial with source code as a resource.
  • #1
dealove
3
0
I have a file with text and adjacent to it, the number i want to read. Below is the format. I was wondering how to read just the numbers without touching the text.

trhy 30.0
difje 30.0
sdjk 66.5
xmb 15.0
dcos 15.0
fjvm 2.573e-4
srht 9.05e-3
art3 0.0
19.954
39.499
43.852
48.713
51.660
53.801
54.715
55.602

another format is:

is...o2. mw2 31.998 htf2 0.0
is...n2. mw3 28.014 htf3 0.0
is..co2. mw4 44.009 htf4 -93.965
is..h2o. mw5 18.015 htf5 -57.103

if above two formats are in the same file, can I use DO loops for first format and second format individually by counting the number of lines?
 
Technology news on Phys.org
  • #2
if the second set always starts with 'is' then once you are able to identify strings you can use this as a starting point to start your second do loop.
I have found a tutorial with source code online. This should help you out:
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/class-avg.html" [Broken]
 
Last edited by a moderator:
  • #3



Yes, you can use DO loops to read both formats separately by counting the number of lines. For the first format, you can use the READ statement with the format specifier 'A' to read the text and 'F' to read the numbers. For the second format, you can use 'A' to read the first two columns and 'F' for the last two columns. You can use the INQUIRE statement to count the number of lines and use that as the stopping condition for your DO loop. Alternatively, you can use the ENDFILE statement to read until the end of the file is reached.
 

1. How do I open a file in Fortran 90 for reading?

In Fortran 90, you can use the OPEN statement to open a file for reading. The syntax is as follows: OPEN (UNIT = unit_number, FILE = file_name, STATUS = 'OLD'). The unit_number is a unique identifier for the file, and the file_name is the name of the file you want to open. The STATUS = 'OLD' tells the compiler that you want to open the file for reading.

2. How do I read text from a file in Fortran 90?

Once you have opened the file, you can use the READ statement to read text from the file. The syntax is as follows: READ (unit_number, *) variable. The unit_number is the identifier for the file, and the variable is the variable where you want to store the text that is read from the file.

3. How do I read multiple lines of text from a file in Fortran 90?

To read multiple lines of text from a file, you can use a DO loop along with the READ statement. The DO loop will continue until the end of the file is reached. The syntax is as follows: DO WHILE (.NOT. EOF(unit_number)), followed by the READ statement. Make sure to close the file after the loop is finished using the CLOSE statement.

4. How do I handle errors while reading files in Fortran 90?

If there are errors while reading the file, you can use the IOSTAT keyword in the READ statement to check for errors. The syntax is as follows: READ (unit_number, *, IOSTAT = io_status) variable. The io_status variable will contain a value of 0 if there are no errors, and a non-zero value if there are errors. You can then use an IF statement to handle the errors accordingly.

5. How do I close a file after reading it in Fortran 90?

To close a file after reading it, you can use the CLOSE statement. The syntax is as follows: CLOSE (unit_number). This will close the file associated with the unit_number you used when opening the file. It is important to close the file after reading it to free up system resources.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
33
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
12
Views
14K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
8K
Back
Top