fortran逐行读取文件
时间: 2024-06-13 20:07:18 浏览: 115
fortran多文件的读入
以下是Fortran逐行读取文件的示例代码:
```fortran
program read_file
implicit none
character(len=20) :: filename
integer :: i, j, n
integer, parameter :: max = 10
integer :: line(max)
logical :: eof
! 打开文件
print *, "请输入文件名:"
read *, filename
open(unit=10, file=trim(filename), status='old', action='read')
! 逐行读取文件内容
do i = 1, max
eof = .false.
j = 0
do while (.not. eof)
read(10, *, iostat=n) line(j+1)
if (n /= 0) then
eof = .true.
else
j = j + 1
end if
end do
! 输出当前行的内容
if (j > 0) then
print *, "第", i, "行:"
do j = 0, j-1
print *, line(j)
end do
else
print *, "文件已读取完毕。"
exit
end if
end do
! 关闭文件
close(10)
end program read_file
```
阅读全文