[Fortran] 纯文本查看 复制代码
Program www_fcode_cn
Implicit None
Logical :: b , fcCopyFile
b = fcCopyFile( "a\b.bin" , "c\d.txt" , .true. )
write(*,*) b
End Program www_fcode_cn
Logical Function fcCopyFile( cSource , cDest , Overwrite )
Implicit None
Character(Len=*) , Intent(IN) :: cSource , cDest
Logical , Intent(IN) :: Overwrite
Integer , parameter :: nSizePerTime = 20*1024 !// 每次20KB
Character(Len=nSizePerTime ) :: cBuffer
integer :: iuSrc , iuDst , nSize , ierr , i , j
logical :: b
fcCopyFile = .false.
If ( .NOT.Overwrite ) then !// 如果不覆盖
Inquire( File = cDest , Exist = b )
if ( b ) return !// 且目标文件存在,则直接返回
End If
Inquire( File = cSource , Size = nSize ) !// 查询源文件大小
Open( NewUnit = iuSrc , File=cSource , access="Stream",form="unformatted" , iostat = ierr )
if ( ierr /= 0 ) return !// 打开失败,返回
Open( NewUnit = iuDst , File=cDest , access="Stream",form="unformatted" , iostat = ierr )
if ( ierr /= 0 ) return !// 打开失败,返回
j = mod( nSize , nSizePerTime ) !// 求余数
if ( j > 0 ) then
read ( iuSrc ) cBuffer(1:j)
write( iuDst ) cBuffer(1:j)
end if
Do i = j+1 , nSize , nSizePerTime
read ( iuSrc ) cBuffer
write( iuDst ) cBuffer
End Do
Close( iuSrc )
Close( iuDst )
fcCopyFile = .true.
End Function fcCopyFile