|
本帖最后由 楚香饭 于 2015-7-25 10:57 编辑
比如说
把 a 文件夹下 b.txt 复制到 c 文件夹下 d.txt
那么有几种方法:
第一.使用扩展语法,调用系统命令:
这种方法缺点是依赖具体编译器,需要你了解系统命令;优点是简单。
在windows上,是 copy
call systemqq("copy a\b.txt c\d.txt")
有的是可能是 call system("copy a\b.txt c\d.txt")
有的情况下,可能还需要 use IFPort 之类的语句。
在linux 上,是 cp
call system("cp ./a/b.txt ./c/d.txt")
这种方法可能会输出一些你不希望看到的信息,例如,复制成功,或复制失败。
此时,你可以用重定向。例如
windows 上
call systemqq("copy a\b.txt c\d.txt 1>NUL 2>NUL")
linux 上
call system("cp ./a/b.txt ./c/d.txt 1>/dev/null 2>&1")
这样就可以隐藏提示信息了。
第二.使用标准语法,调用系统命令:
这种方法的优点是通用性好,是语法标准。
call EXECUTE_COMMAND_LINE("copy a\b.txt c\d.txt")
同第一种方法,windows 和 linux命令行写法不同,也有提示信息的问题。
第三. 在windows上使用API函数:
这种方法的优点是没有错误信息显示,缺点是只在windows上使用。
[Fortran] 纯文本查看 复制代码 3 | i = CopyFile ( "a\\b.txt" c , "c\\d.txt" c , 1 ) |
第四. 打开源文件,把读到的数据全部相同的写入新文件。
这种方法优点是支持所有的编译器,所有的操作系统,效率也还可以,而且大文件可以改一下产生进度提示。棒棒哒!
给你一个我以前写好的代码。你可以直接调用。
[Fortran] 纯文本查看 复制代码 03 | Logical :: b , fcCopyFile |
04 | b = fcCopyFile ( "a\b.bin" , "c\d.txt" , .true. ) |
06 | End Program www_fcode_cn |
08 | Logical Function fcCopyFile ( cSource , cDest , Overwrite ) |
10 | Character ( Len = * ) , Intent ( IN ) :: cSource , cDest |
11 | Logical , Intent ( IN ) :: Overwrite |
12 | Integer , parameter :: nSizePerTime = 20 * 1024 |
13 | Character ( Len = nSizePerTime ) :: cBuffer |
14 | integer :: iuSrc , iuDst , nSize , ierr , i , j |
17 | If ( .NOT. Overwrite ) then |
18 | Inquire ( File = cDest , Exist = b ) |
21 | Inquire ( File = cSource , Size = nSize ) |
22 | Open ( NewUnit = iuSrc , File = cSource , access = "Stream" , form = "unformatted" , iostat = ierr ) |
23 | if ( ierr /= 0 ) return |
24 | Open ( NewUnit = iuDst , File = cDest , access = "Stream" , form = "unformatted" , iostat = ierr ) |
25 | if ( ierr /= 0 ) return |
26 | j = mod ( nSize , nSizePerTime ) |
28 | read ( iuSrc ) cBuffer ( 1 : j ) |
29 | write ( iuDst ) cBuffer ( 1 : j ) |
31 | Do i = j +1 , nSize , nSizePerTime |
32 | read ( iuSrc ) cBuffer |
33 | write ( iuDst ) cBuffer |
38 | End Function fcCopyFile |
|
|