Fortran read from file problems

In summary, the programmer was trying to open a file using a filename, but it didn't work. They found out that the problem was with their IDE and not the program.
  • #1
thomsonm
13
0
I've been given a data file laid out as such:

heading1 heading2 heading3
x1 y1 z1
x2 y2 z2
x3 y3 z3
.etc.

where x=integer, y,z=real

I need to count the number of lines to allocate x, y and z their sizes. My first attempt looked like:

...

OPEN(21, FILE='data.dat', STATUS=OLD)

counter=1
ok=.FALSE.

DO WHILE (.NOT. ok)
READ(21,'\,1X,I1') first
counter=counter+1
IF (first .EQ. ' ') THEN

numberoflines=counter-1
ok=.TRUE.

ELSE CONTINUE

END IF

END DO

...

This runs into the problem that i don't know what type to make "first" as it'll be looking at integers until the last line.

If anyone can help it'd be much appreciated!
 
Technology news on Phys.org
  • #2
Read the data as character data, then it will accept anything.

Also, the simplest way to detect the end of file is to specify an "iostat variable" to receive the status of the read statement. After the read statement, if the iostat variable is 0, the read was OK; if it's -1, you reached the end of file; otherwise there was some kind of input error.

Something like this (untested because I don't have a Fortran compiler handy):

Code:
      integer status, counter
      character*1 dummy

      status = 0
      counter = 0
      do while (status .eq. 0)
          read (21, '(1x, a1)', iostat=status) dummy
          if (status .eq. 0)
              counter = counter + 1
          end if
      end do

      if (status .eq. -1) then
          print *, 'The file has ', counter, ' lines.'
      else
          print *, 'There was an input error!  Status code = ', status
      end if
 
  • #3
That code worked like a charm, thanks!
 
  • #4


As the title suggests, there's more I'd like to ask:

I want to allow users to input a file name for the program to open:

...
CHARACTER*40 filename

...
OPEN(21,FILE=filename,STATUS='OLD')
...

Somehow this doesn't work, it just says the file doesn't exist. Any thoughts?
 
  • #5
The program may be looking for the file in a different directory (folder) than you're expecting. Have you tried using a full pathname?

Also, this may depend on the operating system, so it might be useful to tell us which OS you're using.
 
  • #6
After talking with some friends at uni we realized that it's just the IDE I was using that was the problem. (Silverfrost plato)

After using the universities software it works fine.

Thanks for the speedy response once again!
 

1. What is Fortran?

Fortran is a high-level, general-purpose programming language that is commonly used in scientific and engineering applications. It was developed in the 1950s and is still widely used today, particularly in fields such as weather forecasting and computational physics.

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

To read data from a file in Fortran, you can use the READ statement, which takes the following format:

READ(unit, format) list

The "unit" refers to the logical unit number of the file, the "format" specifies the type and format of the data being read, and the "list" is a list of variables that will receive the data from the file.

3. Why am I getting an error when trying to read from a file in Fortran?

There are several possible reasons for this error. Some common issues include not properly opening the file with the OPEN statement, not specifying the correct file path or name, or not using the correct format for the data being read. It is important to carefully check your code and make sure all necessary steps are being taken to read from the file.

4. How do I handle errors when reading from a file in Fortran?

You can use the IOSTAT variable to check for errors when reading from a file in Fortran. The IOSTAT variable will contain a value of 0 if the read was successful, or a non-zero value if there was an error. You can then use an IF statement to handle any errors that may occur during the read.

5. Can I read from multiple files in Fortran?

Yes, you can read from multiple files in Fortran by using different logical unit numbers for each file. You can then use separate READ statements for each file, specifying the appropriate unit number and format. However, it is important to make sure that you properly close each file after reading from it to avoid any potential errors.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
499
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
4
Views
11K
Back
Top