Gfortran/f90 (MinGW) read file problem

  • Fortran
  • Thread starter solarblast
  • Start date
  • Tags
    File
In summary, the data file contains a line for every day of the year, with each line containing information about a certain celestial object. The relevant part of the program is when it tries to read the data into a namelist form. It gets an error because it can't read the line containing the data for the day 6364.1543.95003538.953223371.97825229.100000.6.203.200.00.6480MEANOK.
  • #1
solarblast
152
2
I'm trying to read a dat (txt) file. Win7. It has this 80 character line that I want to read:
6364.1543 .95003538 .953223371.97825229 .100000 6.203.200 00.6480MEANOK A

The relevant parts of the program are:
real :: dummy
500 format(f10.4)
read(unit=astro_in, fmt=500) dummy !<----line 92
write(*,*) "dummy",dummy

It prints this:

dummy 6364.2583

At line 92 of file create-meteor_orbit-namelists.f90 (unit = 10, file = 'METEOR_Legacy.DAT')
Fortran runtime error: Bad value during floating point read.

Any idea what's going on?

It has read read lines like this above it:

Open(unit=astro_in, file="METEOR_Legacy.DAT", status = "OLD")

eof_swt = .False.
do while(eof_swt .EQV. .False.)
Read(unit=astro_in, fmt="(a80)", iostat=eof) card
 
Technology news on Phys.org
  • #2
By using Err=k, I found that the error corresponds to 5010 LIBERROR_READ_VALUE, which is not really very helpful, since it already shows a "bad value" error.

Possibly this has to do with something related to the Linux end of line character. The file came from Linux.
 
  • #3
I'm wondering if there might be some control character embedded in the first number, one that doesn't show up when you view the file as text, but causes problems when you attempt to read the number into your fortran code. If you can view the file in a hex editor/viewer, it's possible there is an extra character right after the '.' in your first number.

Just a guess...
 
  • #4
Thanks for your response. I had some code mixed up between using

Read(unit=astro_in, fmt="(a80)", iostat=eof) card
and Read(card, fmt="(a80)", iostat=eof) card

As a consequence, I really had read a card ahead that had a completely different format. I'm reading a txt file that looks like:
#solar data
#f10.4!f12.7!
12345.6789 543.2111111 <-wanted to get data here
#station data <- but was on this
#coment
#comment
#comment
data
data
data

The file is sprinkled with a varying number comments followed by varying amount of data lines. Each group of data cards requires a different read format. I'm trying to put the data into a namelist form. There are nine groups.

The data file contains about 150 lines. I think I'll manually modify the comments, so there really is only one comment card before each group, which will include a count of the number of cards below it. Too complex otherwise. Converting old (punch cards format) fortran to f90.
 
  • #5

If (eof .NE. 0) exit
If (card(1:3) .EQ. "END") then

It appears that there may be an issue with the format specified in line 92 of the program. The format (f10.4) is only 10 characters long, but the line in the file is 80 characters long. This could potentially be causing the error when trying to read the line. You may need to adjust the format to match the length of the line in the file, or use a different method of reading the line, such as using a loop to read each character individually. Additionally, it is possible that there may be an issue with the data in the file itself, so it may be worth checking the file for any errors or inconsistencies.
 

1. How do I fix a "read file problem" in Gfortran/f90 (MinGW)?

There are a few potential solutions to this issue. One possibility is that there is an error in your code, such as a missing or incorrect file path. Another possibility is that there is an issue with your compiler or installation. Try checking your code for errors and updating your compiler to see if that resolves the problem.

2. Why am I getting an "end-of-file" error when trying to read a file in Gfortran/f90 (MinGW)?

This error typically occurs when the program reaches the end of a file before it has finished reading all the data. Make sure your program is properly structured to read all the data in the file, and check for any errors in your code that may be causing the issue.

3. How do I read a file with multiple data types in Gfortran/f90 (MinGW)?

In order to read a file with multiple data types, you will need to use the proper format specifier for each type of data. For example, you can use "I" for integers, "F" for floating-point numbers, and "A" for character strings. Make sure you are using the correct format specifiers in your code to properly read all the data in the file.

4. Can I use Gfortran/f90 (MinGW) to read a file from a different directory?

Yes, it is possible to read a file from a different directory in Gfortran/f90 (MinGW). You will need to provide the full path to the file in your code. Additionally, make sure the file is accessible from your current working directory or specify the full path to the directory where the file is located.

5. How do I read a specific line from a file in Gfortran/f90 (MinGW)?

In order to read a specific line from a file, you will first need to open the file and then use a loop to read through the lines until you reach the desired line. You can use the "READ" statement with the "UNIT" and "IOSTAT" options to read a line from a file. Make sure to close the file once you have finished reading the desired line.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
8K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
7K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
5
Views
10K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
9
Views
19K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
11K
Back
Top