[Fortran] Problem reading formatted text file

In summary, the conversation was about a problem with reading data from an ascii file into another code. Despite trying various solutions, the fourth column, rho, was not being read correctly. However, after a computer reboot, the original attempt was successful and the solution was found to be opening the file in a specific way. The conversation ends with a note to future readers who may encounter a similar issue.
  • #1
Morridini
4
1
Hi all, I have a problem I'm trying to solve.

I have a code that has written a simple ascii text file like this:
write(ilun_out, '(E23.15,A,E23.15,A,E23.15,A,E23.15)' x, ' ', y, ' ', z, ' ', rho

So this provides a nice looking ascii file with four columns, separated by a small space. Now I want to read this data into another code... and nothing works. No matter how much I try to tweak the formatting, reading in the spaces into "dummy" objects etc, I get the same problem.

The problem is that the fourth column, rho, is never read. Upon checking what I had read in I see that instead of getting the FIRST value of column FOUR, rho instead has the value of the SECOND value of column THREE.

Anyone know what's going on here?

Cheers

UPDATE: Ok I have no idea WHAT was going on, it might have been some problems with libraries or the compiler acting up, but after a computer reboot my original try actually worked... so there wasn't anything wrong in the code itself. Oh well.

In case anyone ever has a similar problem, the solution is the rather simple way of doing this:
open(unit=ilun_ascii,file=fileloc_ascii,form='formatted', status='old')
read(ilun_ascii,*) x, y, z, rho
 
Last edited:
  • Like
Likes jim mcnamara
Technology news on Phys.org
  • #2
Thanks for noting the solution, it may help future searchers.
 

1. How do I read a formatted text file in Fortran?

To read a formatted text file in Fortran, you can use the read statement with the format specifier. This allows you to specify the format of the data in the file and read it into variables in your program.

2. Why am I getting an error when reading a formatted text file in Fortran?

There could be several reasons for this error. Some common reasons include using the wrong format specifier, not correctly specifying the data type of the variables in your program, or not handling end-of-line characters properly. Make sure to double check your code and consult the Fortran documentation for the correct usage of the read statement.

3. How do I specify the format of a text file in Fortran?

The format of a text file in Fortran can be specified using the format specifier in the read statement. This allows you to specify the data types and the order in which the data appears in the file. You can also use the format specifier in the format statement to define a format that can be used for multiple read statements.

4. Can I use a custom format to read a text file in Fortran?

Yes, you can use a custom format to read a text file in Fortran. You can define your own format using the format statement and then use it in the read statement. This allows you to have more control over how the data is read from the file.

5. How can I handle missing or invalid data when reading a text file in Fortran?

You can use the err specifier in the read statement to handle missing or invalid data when reading a text file in Fortran. This allows you to specify what action should be taken when the data in the file does not match the specified format. You can also use err to handle end-of-file conditions or other errors that may occur while reading the file.

Similar threads

  • Programming and Computer Science
Replies
2
Views
859
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
2
Replies
57
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
2
Views
241
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
22
Views
3K
Back
Top