Fortran怎么读取和写入文件


在Fortran中,可以使用OPENREADWRITECLOSE等关键字来读取和写入文件。

下面是一个示例代码,展示了如何读取和写入文件:

program file_ioimplicit noneinteger :: i, n, statusreal :: xcharacter(len=50) :: filenamecharacter(len=50) :: line! 打开文件filename = 'data.txt'open(unit=10, file=filename, status='old', action='read', iostat=status)if (status /= 0) thenprint*, 'Error opening file'stopend if! 读取文件中的数据do i = 1, 5read(10, '(A)', iostat=status) lineread(line, *) n, xprint*, 'Read from file:', n, xend do! 关闭文件close(unit=10)! 打开文件以写入数据open(unit=20, file='output.txt', status='replace', action='write', iostat=status)if (status /= 0) thenprint*, 'Error opening file'stopend if! 写入数据到文件do i = 1, 5write(line, '(2I5, F10.2)') i, i*2, real(i)*2write(20, '(A)') trim(adjustl(line))end do! 关闭文件close(unit=20)end program file_io

在这个示例中,程序首先打开一个名为data.txt的文件,读取其中的数据,并输出到屏幕上。然后,程序再打开一个名为output.txt的文件,并将数据写入到文件中。

需要注意的是,在读取和写入文件时,需要使用READWRITE语句,同时也需要使用OPENCLOSE语句来操作文件。


上一篇:没有了

下一篇:没有了


Fortran
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器