当前位置:网站首页 > Fortran编程 > 正文

fortran输出txt文件_fortran用什么软件编写

Fortran读写文本文件。

1 文件写入

此示例演示如何打开新文件以将某些数据写入文件。编译并执行代码时,它会创建文件data1.dat并将x和y数组值写入其中。 然后关闭文件。

 program outputdata implicit none real, dimension(100) :: x, y real, dimension(100) :: p, q integer :: i ! data do i=1,100 x(i) = i * 0.1 y(i) = sin(x(i)) * (1-cos(x(i)/3.0)) end do ! output data into a file open(1, file = 'data1.dat', status = 'new') do i=1,100 write(1,*) x(i), y(i) end do close(1) end program outputdata 

2 文件读取

在这个程序中,我们从文件中读取,我们在最后一个例子中创建了data1.dat,并在屏幕上显示它。

 program outputdata implicit none real, dimension(100) :: p, q integer :: i ! opening the file for reading open (2, file = 'data1.dat', status = 'old') do i = 1,100 read(2,*) p(i), q(i) end do close(2) do i = 1,100 write(*,*) p(i), q(i) end do end program outputdata 

文件读取时,自动判别文件结尾:

 program outputdata implicit none real, dimension(100) :: p, q integer :: i integer :: io ! opening the file for reading open (2, file = 'data1.dat', status = 'old') do read(2,*,IOSTAT=io) p(i), q(i) if (io/=0) then exit end if end do close(2) end program outputdata 

自动判别文件结尾可参考如下链接:
[1] Handling End-of-File: the READ Statement Revisited
[2] How to know that we reached EOF in Fortran 77?

到此这篇fortran输出txt文件_fortran用什么软件编写的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • fortran 入门_fortran入门教程2024-11-12 22:00:05
  • fortran入门_fortran属于系统软件2024-11-12 22:00:05
  • fortran编程规范_fortran软件2024-11-12 22:00:05
  • c和fortran混合编程_fortran语言用什么软件2024-11-12 22:00:05
  • Fortran编程之我的第一个大坑_fortran简单编程例子2024-11-12 22:00:05
  • fortran2013安装步骤_fortran软件2024-11-12 22:00:05
  • vs编译fortran报错_visual fortran安装教程2024-11-12 22:00:05
  • fortran 数据类型_fortran简单编程例子2024-11-12 22:00:05
  • fortran程序编写_大一python基础编程题2024-11-12 22:00:05
  • fortran语言基础_fortran语言编程2024-11-12 22:00:05
  • 全屏图片