如何在 Fortran 90 中复制文件?

发布于 2024-10-20 02:43:17 字数 39 浏览 4 评论 0原文

如何以可移植、跨平台的方式复制 Fortran 90 中的文件?

How can I copy a file in fortran 90 in a portable, cross plaform way ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓦然回首 2024-10-27 02:43:17

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

无人问我粥可暖 2024-10-27 02:43:17

您可以在 Fortran 2003 中通过流读取/写入文件,但在 Fortran 90/95 中我认为这可以复制任意文件(虽然效率极低!!)

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

当然,如果它是 Fortran 生成的文件,您可以使用该信息可以提高效率。

就个人而言:如果您需要从 Fortran 内部调用系统调用,您在做什么?使用更适合该任务的其他语言不是更好吗?

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?

烟花肆意 2024-10-27 02:43:17

是的,Fortran 的 I/O 很差,如果可能的话,不应该用于此类事情。多么遗憾我们中的一些人被迫这样做。

我只是读取源文件并同时逐行写入目标。到目前为止,这对我有用,但效率很低。

对于 Fortran 来说,处理文件和可移植性很烦人,而且 SYSTEM 调用通常也不是很好。 Windows 操作系统不能正确遵循 linux 链接文件,并且 Windows/Linux/MacOS 有不同的分隔符,我遇到了 SYSTEM 调用中固有的堆栈限制,等等。

祝你好运 !

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 !

烟花肆意 2024-10-27 02:43:17

对于英特尔 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

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
别理我 2024-10-27 02:43:17

之前的答案对我不起作用,所以我编写了以下子例程

!=============================================================================================================================!
!                                                                                                                             !
!                           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 

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 
落花浅忆 2024-10-27 02:43:17

!对于康柏/英特尔 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

! 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文