274068459 发表于 2017-2-28 15:46:36

fcode 发表于 2017-2-24 12:38
确保您的 Code::blocks 是包含了 gfortran 编译器的。

然后按照这个操作:


我最后生成了windspeed.txt文件,但是文件里面是空的~我计划写进去的数组没有写进去是什么问题?

274068459 发表于 2017-2-28 15:54:41

fcode 发表于 2017-2-24 12:38
确保您的 Code::blocks 是包含了 gfortran 编译器的。

然后按照这个操作:


run后有这样一个运行错误提示~

fcode 发表于 2017-2-28 16:26:50

这是因为 你的 a 是一个数组,但是实际只读取了它的一部分。

Ftn95 检查比较严格,当你要输出全部 a 数组的时候,Ftn95 告诉你,有一部分 a 数组没有定义(即没有给值)

其实 ,a 是一个单变量就可以,不需要定义为数组。

以下为其他错误,见 !//// 开头的注释

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
实际上,你的代码可以简化为:

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
页: 1 [2]
查看完整版本: 从N个txt文件中读取相同的一列组成数组用于计算!!