program main
implicit none
integer::list,inputn !do 循环计数用的变量
character(len=19):: file(23) !存放文件名的字符型一维数组
character(len=19):: file1
integer::file_state,num
open(101,file = 'filelist.txt',status='old',action='read')
open(102,file = 'check.txt',status='old',action='readwrite')
do list = 1,23 !将filelist.txt中存储的文件名写入一维数组 file 中
read(101,*)file(list)
end do
write(102,*)file !检查filelist.txt中的数据是否读取
do inputn=1,23
file1=file(inputn)
num=1
call input(num,file1) !调用input打开名字为 file(i)的txt文档
num=num+1
end do
write(102,*)num !记录循环进行的次数********结果为2,只循环了一次TT
end program main
subroutine input(numb,filename)
implicit none
character(len=19)::filename
integer::file_state,numb
open(unit=numb,file =filename ,status='old',action='read',iostat=file_state)
open (unit=100,file="result.txt",status='old',action='readwrite')
if(file_state/=0)then !检查文件是否正常打开
write(100,*)"failed to open",filename
else
write(100,*)"successed to open",filename
end if
close(numb,status='keep')
close(100,status='keep')
return
end subroutine input
1.6 KB, 下载次数: 2
代码文件
珊瑚虫 发表于 2014-12-2 21:56
请写清楚程序在运行时报的什么错误,
另外建议你的管道100在主程序的101 102 一起打开,这样每一次是否正确 ...
fcode 发表于 2014-12-2 22:02
num=1 放到 do 循环前面,而不是里面。
program main
implicit none
integer::list,inputn !do 循环计数用的变量
character(len=19):: file(23) !存放文件名的字符型一维数组
character(len=19):: file1
integer::file_state,num
open(101,file = 'filelist.txt',status='old',action='read')
open(102,file = 'check.txt',status='old',action='readwrite')
open (unit=100,file="result.txt",status='old',action='readwrite')
do list = 1,23 !将filelist.txt中存储的文件名写入一维数组 file 中
read(101,*)file(list)
end do
write(102,*)file !检查filelist.txt中的数据是否读取
num=1
do inputn=1,23
file1=file(inputn)
call input(num,file1) !调用input打开名字为 file(i)的txt文档
num=num+1
end do
write(102,*)num !记录循环进行的次数********结果为2,只循环了一次TT
close(100,status='keep')
end program main
subroutine input(numb,filename)
implicit none
character(len=19)::filename
integer::file_state,numb
open(unit=numb,file =filename ,status='old',action='read',iostat=file_state)
if(file_state/=0)then !检查文件是否正常打开
write(100,*)"failed to open",filename
else
write(100,*)"successed to open",filename
end if
close(numb,status='keep')
return
end subroutine input
笨蛋哈哈 发表于 2014-12-2 22:11
嗯嗯,拿出去就是记录循环23次了,但是result.txt只记录了 successed to open670420071124082.txt,这是 ...
珊瑚虫 发表于 2014-12-2 22:18
[mw_shl_code=fortran,true]program main
implicit none
integer::list,inputn !do 循 ...
珊瑚虫 发表于 2014-12-2 22:18
[mw_shl_code=fortran,true]program main
implicit none
integer::list,inputn !do 循 ...
fcode 发表于 2014-12-2 22:20
把
open (unit=100,file="result.txt",status='old',action='readwrite')
放到主程序 Open(101) 一起
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |