Fortran Coder

查看: 677|回复: 5

[输入输出] 文件的读取与写出问题

[复制链接]

13

帖子

3

主题

0

精华

入门

F 币
74 元
贡献
41 点
发表于 2018-4-16 16:10:32 | 显示全部楼层 |阅读模式
如图一,画红圈的是我读取文件的单元名称,我需要将这个读入的数据变成图二的样式。规律是画红圈的部分,前两个字符不变,后面的字符从末尾气依次加一。前两个字符相同的单元号,其后面对应的数值也不变,我编的程序如图三,图四所示,但是导出的文件的结果如图五所示,通过程序编译,可知问题应该是发生在图六画红圈的部分,但是我不知如何修改吗,希望大神能帮我看一下应该如何修改?
代码如下:
[Fortran] 纯文本查看 复制代码
module typedef
    type para
            character(len=2) line
            integer :: column,addcolumn
            character(len=3) stateindex
            character(len=1) colon
            character(len=1) pressure
            character(len=1) commaa
            character(len=15) variables
            real(kind=8) :: porosity,Numcon1,Numcon2,Numcon3,Numcon4
    end type
    end module
    
        
    program main
    use typedef
    implicit none
    integer,parameter :: fileid = 3                       ! number of save.txt
    integer,parameter :: fileids = 4                      ! number of INCON.txt
    integer,parameter :: linenum = 2                      ! the number of lines,can change
    integer,parameter :: columnnum = 150                      ! the number of column,can change
    character(len=80) title
    type(para) :: s(linenum)                              ! save the parameters of save.txt
    type(para) :: a(columnnum)                            ! save the parameters of columnnum
    integer i,j,error,truecolumn
    
    truecolumn=5
    open( fileid,file="save.txt",status="old",iostat=error)
    if(error/=0)then
        write(*,*)"open save.txt fail."
        stop
    end if
    
    OPEN(UNIT=fileids,FILE='INCON.txt',FORM='FORMATTED',STATUS='NEW',IOSTAT=error)
          if(error/=0) then
              write(*,*)"open elem_elem.txt fail"
              stop
          end if
    
    read(fileid,*) title
    write(fileids,"(A80)") title
    
    do i=1,linenum
    read(fileid,FMT = 5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
    write(fileids,FMT=5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
    a(i)%addcolumn = s(i)%column
    a(i)%line = s(i)%line
    a(i)%porosity = s(i)%porosity
    a(i)%stateindex = s(i)%stateindex
    a(i)%colon = s(i)%colon
    a(i)%pressure = s(i)%pressure
    a(i)%commaa = s(i)%commaa
    a(i)%variables = s(i)%variables
    a(i)%Numcon1 = s(i)%Numcon1
    a(i)%Numcon2 = s(i)%Numcon2
    a(i)%Numcon3 = s(i)%Numcon3
    a(i)%Numcon4 = s(i)%Numcon4
    do j=1,truecolumn
        a(j)%addcolumn=a(j)%addcolumn+j
        write(fileids,FMT=5001) a(i)%line,a(j)%addcolumn,a(i)%porosity,a(i)%stateindex,a(i)%colon,a(i)%pressure,a(i)%commaa,a(i)%variables,a(i)%Numcon1,a(i)%Numcon2,a(i)%Numcon3,a(i)%Numcon4
    end do
    end do
    
!      do i=1,linenum
!          write(fileids,FMT=5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
!      end do
!      stop
5001      FORMAT(A2,A3,11X,ES14.8,2X,A3,A1,1X,A1,A1,5X,A15/1X,ES19.13,1X,ES19.13,1X,ES19.13,1X,ES19.13)
          end program

图一

图一

图二

图二

图三

图三

图四

图四

图五

图五

图六

图六
回复

使用道具 举报

613

帖子

0

主题

0

精华

大师

F 币
592 元
贡献
367 点

规矩勋章元老勋章新人勋章水王勋章

QQ
发表于 2018-4-17 01:08:53 | 显示全部楼层
对于大段的代码,请直接复制粘贴,不截图。
对于输入文件也是一样。

截图不能复制粘贴,对于想要帮助你的人来说,需要照着图片敲一遍代码,才能帮助你测试。这是很复杂的一件事情。

(请注意,通过眼睛看出错误,是很难的)

13

帖子

3

主题

0

精华

入门

F 币
74 元
贡献
41 点
 楼主| 发表于 2018-4-17 17:13:35 | 显示全部楼层
vvt 发表于 2018-4-17 01:08
对于大段的代码,请直接复制粘贴,不截图。
对于输入文件也是一样。

好的,我已经修复了帖子

13

帖子

3

主题

0

精华

入门

F 币
74 元
贡献
41 点
 楼主| 发表于 2018-4-17 18:24:11 | 显示全部楼层
我自己解决了我的问题,下面把我修改的程序贴出来
[Fortran] 纯文本查看 复制代码
module typedef
    type para
            character(len=3) line
            integer :: column
            character(len=3) stateindex
            character(len=1) colon
            character(len=1) pressure
            character(len=1) commaa
            character(len=15) variables
            real(kind=8) :: porosity,Numcon1,Numcon2,Numcon3,Numcon4
    end type
    end module
    
        
    program main
    use typedef
    implicit none
    integer,parameter :: fileid = 3                       ! number of save.txt
    integer,parameter :: fileids = 4                      ! number of INCON.txt
    integer,parameter :: linenum = 2                      ! the number of lines,can change
    integer,parameter :: columnnum = 150                      ! the number of column,can change
    character(len=80) title
    type(para) :: s(linenum)                              ! save the parameters of save.txt
    integer i,j,error,truecolumn
    
    truecolumn=5
    open( fileid,file="save.txt",status="old",iostat=error)
    if(error/=0)then
        write(*,*)"open save.txt fail."
        stop
    end if
    
    OPEN(UNIT=fileids,FILE='INCON.txt',FORM='FORMATTED',STATUS='NEW',IOSTAT=error)
          if(error/=0) then
              write(*,*)"open elem_elem.txt fail"
              stop
          end if
    
    read(fileid,*) title
    write(fileids,"(A80)") title
    
    do i=1,linenum
    read(fileid,FMT = 5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
    write(fileids,FMT=5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
    do j=1,truecolumn
        s(i)%column=MOD(j,100)
        write(fileids,FMT=5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
    end do
    end do
    

!      do i=1,linenum
!          write(fileids,FMT=5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
!      end do
!      stop

5001      FORMAT(A3,I2.2,11X,ES14.8,2X,A3,A1,1X,A1,A1,5X,A15/1X,ES19.13,1X,ES19.13,1X,ES19.13,1X,ES19.13)
          end program


1323

帖子

12

主题

5

精华

论坛跑堂

Fcode跑堂伙计

F 币
220 元
贡献
260 点

新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

发表于 2018-4-17 22:04:37 | 显示全部楼层
其实你可以这样定义结构体
[Fortran] 纯文本查看 复制代码
type para
            character(len=3) line
            integer :: column
           real(8) :: porosity
            character(len=3) stateindex
            character(len=1) colon
            character(len=1) pressure
            character(len=1) commaa
            character(len=15) variables
            real(kind=8) :: Numcon(4)
    end type


这样有个好处,就是不再需要写
read(fileid,FMT = 5001) s(i)%line,s(i)%column,s(i)%porosity,s(i)%stateindex,s(i)%colon,s(i)%pressure,s(i)%commaa,s(i)%variables,s(i)%Numcon1,s(i)%Numcon2,s(i)%Numcon3,s(i)%Numcon4
而可以简单的写成
read(fileid,5001) s(i)

当然你还要调整 5001 format 的顺序。

非常方便

13

帖子

3

主题

0

精华

入门

F 币
74 元
贡献
41 点
 楼主| 发表于 2018-4-18 10:33:40 | 显示全部楼层
fcode 发表于 2018-4-17 22:04
[mw_shl_code=fortran,true]其实你可以这样定义结构体
type para
            character(len=3) line

非常感谢您的意见,我已经做出修改
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

QQ|捐赠本站|Archiver|关于我们 About Us|群聊|Fcode

GMT+8, 2018-12-12 15:01

Powered by Discuz! X3.2

© 2001-2017 Comsenz Inc.

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