开发者

How to copy a file in Fortran 90?

How ca开发者_JS百科n I copy a file in fortran 90 in a portable, cross plaform way ?


Use the SYSTEM with your OS's copy command. Practically all compilers support this feature.


You can read/write the file through a stream in Fortran 2003, but in Fortran 90/95 I think this would work to copy an arbitrary file (extremely inefficient though!!)

OPEN(UNIT=ISRC, FILE='', ACCESS='DIRECT', STATUS='OLD', ACTION='READ', IOSTAT=IERR, RECL=1)
OPEN(UNIT=IDST, FILE='', ACCESS='DIRECT', STATUS='REPLACE', ACTION='WRITE', IOSTATE=IERR, RE)
IREC = 1
DO
  READ(UNIT=ISRC, REC=IREC, IOSTAT=IERR) CHAR
  IF (IERR.NE.0) EXIT
  WRITE(UNIT=IDST, REC=I) CHAR
  IREC = IREC + 1
END DO

Of course, if it was a fortran generated file, you could use that information to make it more efficient.

On a personal note: if you need invoke system calls from inside fortran, what are you doing? Isn't it better to use some other language that is better suited for the task?


Yes, Fortran has pathetic I/O and shouldn't be used for this sort of thing if at all possible. What a shame that some of us are forced to do it.

I just read the source file and simultaneously write to the destination, line-by-line. So far this works for me, but is very inefficient.

Dealing with files and portability is annoying with Fortran, and SYSTEM calls are often not very good either. The windows OS doesn't properly follow linux linked files, and Windows/Linux/MacOS have different separaters, I have been caught out with stack limits inherent in the SYSTEM call, and so on.

Good luck !


For Intel Fortran

subroutine copy_file (file_name, file_name_new)
! copies a file file_name to file_name_new
! file_name and file_name_new must include the path information and may include wildcard characters

USE ifport 
implicit character*100 (f)
character*1000 fnam
logical*4 logical_result

len1 = len_trim(file_name); len2 = len_trim(file_name_new)
fnam = 'copy/y ' //file_name(1:len1) //' '//file_name_new(1:len2)

l = len_trim(fnam)
logical_result = systemqq(fnam(1:l))

return
end


The previous answer didn't work for me so I wrote the following subroutine

!=============================================================================================================================!
!                                                                                                                             !
!                           This subroutine copies file_name to file_name_new writing command to cmd                          !
!                                                                                                                             !
!=============================================================================================================================! 
 subroutine copy_file (file_name, file_name_new)
 use ifport 
 implicit none
!=============================================================================================================================
! D e c l a r a t i o n s
!=============================================================================================================================
 character(len=*),intent(IN) :: file_name_new,file_name
!----------------------------------------------------------------------------------------------------------------------------- 
 logical                     :: logical_result
!=============================================================================================================================
! S t a t e m e n t s
!============================================================================================================================= 

 logical_result = systemqq('copy "'//trim(file_name) //'" "'//trim(file_name_new)//'"')

!==============================================================================================================================
 end subroutine copy_file 


! For Compaq/Intel Visual Fortran

subroutine copy_file(source_,dest_)   
use kernel32,only:CopyFile,FALSE 
implicit none  
integer ret  
character*(*), intent(in) :: source_, dest_  
ret = CopyFile(trim(source_)//""C, trim(dest_)//""C, FALSE)  
end subroutine copy_file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜