Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

fortran - Number of lines of a text file

I'm trying to make a function that takes in a file name (i.e. "data.txt") and produces the number of lines of that file.

data.txt :
24 42
45 54
67 76
89 98
12 21
99 99
33 33

The below piece of code is my attempt to build a function that takes in the filename "data.txt" and produces the number of lines of the file. The first part (lines 1-12) of the code is defining the function and the second part (lines 14-19) is where I call the function in a program. The output of the below code is 1 rarther than 7 (look up - data.txt has 7 lines).

function count_lines(filename) result(nlines)
  character :: filename
  integer :: nlines 
  open(10,file=filename)
  nlines = 0
  do
  read(10,*,iostat=io)
  nlines = nlines + 1
  if (io/=0) exit
  end do
  close(10)
end function count_lines

program myread 
  integer :: count_lines

  print *, count_lines("data.txt")

end program myread

Attempt a debuging:

I think it has something to do with the do loop not working, but I'm unsure. I have changed the if statement to if (io>0) and this gives an output of 2, which I don't understand. At present I'm not getting an error outputs when I compile just the wrong output.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

filename (the dummy argument) is only one character long. Anything might happen if you provide a longer one (usually it is capped, though). This does not result in an error as you did not specify the status of the file, or even checked whether the operation was successful (In fact, you just created a new file d). You can avoid this by using an assumed length string:

function count_lines(filename) result(nlines)
  character(len=*) :: filename
  ! ...

  open(10,file=filename, iostat=io, status='old')
  if (io/=0) stop 'Cannot open file! '
function count_lines(filename) result(nlines)
  implicit none
  character(len=*)    :: filename
  integer             :: nlines 
  integer             :: io

  open(10,file=filename, iostat=io, status='old')
  if (io/=0) stop 'Cannot open file! '

  nlines = 0
  do
    read(10,*,iostat=io)
    if (io/=0) exit
    nlines = nlines + 1
  end do
  close(10)
end function count_lines

[Increment after the check to ensure a correct line count.]


For the second part of your question:

Positive (non-zero) error-codes correspond to any error other than IOSTAT_END (end-of-file) or IOSTAT_EOR (end-of-record). After reading in the (new) file in the first round of the loop (io==IOSTAT_END, I checked with my compiler), you are trying to read beyond the end... This results in a positive error. Since the increment occurs before you exit the loop, the final value is 2.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...