Fortran read from file problems

  • Context: Fortran 
  • Thread starter Thread starter thomsonm
  • Start date Start date
  • Tags Tags
    File Fortran
Click For Summary

Discussion Overview

The discussion revolves around issues related to reading data from a file in Fortran, specifically focusing on counting lines in a data file and handling user input for file names. Participants explore different methods for reading data and detecting the end of the file, as well as troubleshooting file access problems.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes a problem with reading integers and real numbers from a file and not knowing how to define the variable type for reading the first line.
  • Another participant suggests reading the data as character data to accommodate any input and recommends using an "iostat variable" to check for end-of-file conditions.
  • A later reply confirms that the suggested code worked successfully for counting lines in the file.
  • Another participant raises a new issue about allowing users to input a file name, which leads to a problem where the file is reported as non-existent.
  • One participant proposes that the issue might be related to the file's directory location and suggests using a full pathname, also noting that the operating system could affect file access.
  • A subsequent reply indicates that the issue was related to the IDE being used, which was resolved by switching to a different software environment.

Areas of Agreement / Disagreement

Participants generally agree on the methods for reading data and detecting end-of-file conditions, but there is a lack of consensus on the file access issue related to user input for file names, which is resolved in a later post.

Contextual Notes

Limitations include the dependence on the specific IDE and operating system for file access, as well as the potential for input errors that are not fully explored in the discussion.

Who May Find This Useful

This discussion may be useful for individuals working with Fortran who are dealing with file input/output operations, particularly in educational settings or those encountering similar issues with file handling in different environments.

thomsonm
Messages
13
Reaction score
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
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
 
That code worked like a charm, thanks!
 


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?
 
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.
 
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!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 12 ·
Replies
12
Views
16K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K