Fortran Coder

查看: 8143|回复: 2

[文件读写] 把txt文档中的每一列数据按每一行固定数据个数输出

[复制链接]

21

帖子

7

主题

0

精华

熟手

F 币
169 元
贡献
93 点

新人勋章

发表于 2019-11-25 15:31:30 | 显示全部楼层 |阅读模式
本帖最后由 wawewen 于 2019-11-25 16:10 编辑

文件如图1所示,纵轴是角度,横轴是半径,现在要将每个半径下的数据生成到另一个txt文档中,按每行十个数据输出,每组数据之前要有对应的半径,如图2所示
写的代码却只输出了第一列也就是角度的数据,还是按列输出如图3,请大佬指正
[Fortran] 纯文本查看 复制代码
 program main
        implicit none
        real,dimension(721,604)::a
        integer i,j
        open(unit=10,file="C:\Users\Lenovo\Desktop\1.csv")
        open(unit=20,file="C:\Users\Lenovo\Desktop\BMap.txt")
        do j=2,604
        do i=1,721
        read(10,*,end=1)a(i,j)
        if(i==1)then
        write(20,*)a(i,j)
        else if(i/=1)then
        write(20,100)a(i,j)
100  format(10f9.2)
        end if
        end do
        end do
 1        close(10)
        close(20)
        end

第一个把end=1去掉后提示end-of-file during read数据是721行,604列用第二个写了一下运行后提示array bounds exceeded
[Fortran] 纯文本查看 复制代码
      program main
        implicit none
        real,dimension(721,604)::a
        integer i,j
        open(unit=10,file="C:\Users\Lenovo\Desktop\1.csv")
        open(unit=20,file="C:\Users\Lenovo\Desktop\BMap.txt")
        do j=1,604
        read(10,*,end=1)(a(i,j),i=1,721)
        if(j==1)cycle
        if(i==1)then
        write(20,*)a(i,j)
        else if(i/=1)then
        write(20,100)a(i,j)
  100 format(10f9.2)
        end if
        end do
1        close(10)
        close(20)
        end



图1

图1

图2

图2

图3

图3
心怀不惧,方能翱翔于天际

250

帖子

2

主题

0

精华

宗师

F 币
1730 元
贡献
872 点

规矩勋章

发表于 2019-11-26 12:16:14 | 显示全部楼层
本帖最后由 necrohan 于 2019-11-26 12:17 编辑

[Fortran] 纯文本查看 复制代码
program main
implicit none
integer,parameter::nx=721,ny=604
real::a(nx,ny)
integer i,j
! 读入全部数据
open(10,file="C:\Users\Lenovo\Desktop\1.csv")
do j=1,ny
  read(10,*,end=1)(a(i,j),i=1,nx)
enddo
close(10)
! 输出
open(20,file="C:\Users\Lenovo\Desktop\BMap.txt")
do i=2,nx
  write(20,*)a(i,1)
  write(20,'((10f9.2))')(a(i,j),j=1,ny)
enddo
close(20)
1 continue
end

21

帖子

7

主题

0

精华

熟手

F 币
169 元
贡献
93 点

新人勋章

 楼主| 发表于 2019-11-26 13:33:17 | 显示全部楼层
necrohan 发表于 2019-11-26 12:16
[mw_shl_code=fortran,true]program main
implicit none
integer,parameter::nx=721,ny=604

多谢指点,已经完成了
心怀不惧,方能翱翔于天际
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-29 08:26

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表