这是因为 你的 a 是一个数组,但是实际只读取了它的一部分。 Ftn95 检查比较严格,当你要输出全部 a 数组的时候,Ftn95 告诉你,有一部分 a 数组没有定义(即没有给值) 其实 ,a 是一个单变量就可以,不需要定义为数组。 以下为其他错误,见 !//// 开头的注释 [Fortran] 纯文本查看 复制代码 program test12 implicit none integer,parameter :: irow = 100 !给一个文件可能的最大数量 integer,parameter :: iline = 60 !行数 real,dimension(iline,irow) :: a !定义一个二维数组用于存放提取的所有数据 character,dimension(irow) :: b !定义一个一维数组用于存放名字 logical :: exceed = .false. character(len = 20) :: filename integer :: i,j,k character :: name integer :: nvals = 0 integer :: status integer :: num = 100 integer :: height real :: windspeed1 !!!打开存放文件名的path.txt文件 open(unit=11,file='path.txt',status='old',action='read',IOSTAT=status) if(status /= 0)then !!!逐行读取文件名,并保存在数组b中 do read(11,*,iostat=status) name if(status /=0)EXIT nvals = nvals +1 size:if(nvals<=irow)then b(nvals) = name else exceed = .true. end if size end do !!!读取数据 do i=1,nvals !根据b(i)的文件名循环打开数据文件 open(num,file = 'b(i)',status='old',action='read',IOSTAT=status) if(status /= 0 )EXIT !将前三行说明文件读空 do j=1,3 read(unit=num,*) end do !读取所要的数据 do k=1,iline read(2,*) a(k,i) !2是想要提取的数据列数 if(status/=0)EXIT end do if(i>nvals)exit end do !!!新建一个文件,将读出的数组写入其中保存用于计算 open(unit=12,file='windspeed.txt',status='new') read(*,*)a stop end 实际上,你的代码可以简化为: [Fortran] 纯文本查看 复制代码 program test12 implicit none real :: a !//// a 可以不用数组 character(len=100),dimension(100) :: b !////要给长度 logical :: exceed = .false. integer :: i,j character(len=100) :: name !////要给长度 integer :: nvals = 0 integer :: status integer :: num = 100 real :: xx !!!打开存放文件名的path.txt文件 open(unit=11,file='path.txt',status='old',action='read',IOSTAT=status) open(unit=12,file='windspeed.txt') !//// 把这个文件放前面打开 if(status /= 0) stop !// 此行直接改成如果出错,则 stop !!!逐行读取文件名,并保存在数组b中 do read(11,*,iostat=status) name if(status /=0)EXIT nvals = nvals +1 if(nvals<=size(b))then b(nvals) = name else exceed = .true. end if end do !!!读取数据 do i=1,nvals !根据b(i)的文件名循环打开数据文件 open(num,file = b(i),status='old',action='read',IOSTAT=status) !//此处b(i)不应该带引号 if(status /= 0 ) cycle !//此处可以用 cycle,第i个文件有错,还可以读第i+1个 !将前三行说明文件读空 do j=1,3 read(num,*) !//此处去掉 unit= end do !读取所要的数据 do read(num,*,iostat=status) xx , a !////此处是否应该是read(num ,然后xx是第一列,读了但不使用。要加 iostat write(12,*) a !////写入 a if(status/=0)EXIT end do close(num) !////关闭num end do end program test12 |
fcode 发表于 2017-2-24 12:38 我最后生成了windspeed.txt文件,但是文件里面是空的~我计划写进去的数组没有写进去是什么问题? |
确保您的 Code::blocks 是包含了 gfortran 编译器的。 然后按照这个操作: |
fcode 发表于 2017-2-23 18:51 你总是给与我很直接的帮助!!! |
vvt 发表于 2017-2-23 18:46 谢谢!其实我也想换一个编译器,但是一直都拖着没有做~谢谢你的推荐~我准备换一个了~ |
li913 发表于 2017-2-23 17:48 感谢感谢~但是,我应该从哪里找这两个文件啊?貌似这是个群对吗?我好像下载不了我不在群里 |
read(unit=num,*) 改为 read( num , * ) 或 read( unit=num , format = * ) 其他错误见下面的代码,以 !// 为注释 [Fortran] 纯文本查看 复制代码 program test12 implicit none real :: a , xx integer :: j , num = 100 , status character(len=100) :: name !////要给长度 !!!打开存放文件名的path.txt文件 open(unit=11,file='path.txt',status='old',action='read',IOSTAT=status) if(status /= 0) stop !// 此行直接改成如果出错,则 stop open(unit=12,file='windspeed.txt') !//// 把这个文件放前面打开 !!!逐行读取文件名,并保存在数组b中 do read(11,*,iostat=status) name if(status/=0)EXIT open(num,file = name,status='old',action='read',IOSTAT=status) !//此处b(i)不应该带引号 if(status /= 0 ) cycle !//此处可以用 cycle,第i个文件有错,还可以读第i+1个 do j=1,3 read(num,*) !//此处去掉 unit= end do do read(num,*,iostat=status) xx , a !////此处是否应该是read(num ,然后xx是第一列,读了但不使用。要加 iostat if(status/=0)EXIT write(12,*) a !////写入 a end do close(num) !////关闭num end do close(11) close(12) end program test12 |
捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )
GMT+8, 2024-5-2 21:42