Fortran Coder

标题: 把txt文档中的每一列数据按每一行固定数据个数输出 [打印本页]

作者: wawewen    时间: 2019-11-25 15:31
标题: 把txt文档中的每一列数据按每一行固定数据个数输出
本帖最后由 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.png (44.8 KB, 下载次数: 166)

图1

图1

2.png (19.08 KB, 下载次数: 186)

图2

图2

3.JPG (16.33 KB, 下载次数: 170)

图3

图3

作者: necrohan    时间: 2019-11-26 12:16
本帖最后由 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

作者: wawewen    时间: 2019-11-26 13:33
necrohan 发表于 2019-11-26 12:16
[mw_shl_code=fortran,true]program main
implicit none
integer,parameter::nx=721,ny=604

多谢指点,已经完成了




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2