2014-11-04 2 views
2

Fortran 90 코드에서 작업 디렉토리를 변경하고자합니다. 컴파일러가 아닌 특정 방식으로이 작업을 수행 할 수 있습니까? 여기 내 코드는 다음과 같습니다.컴파일러가 아닌 특정 방식으로 Fortran에서 디렉토리가 변경됨

program change_directory 
    integer :: ierr 

    call system("mkdir -p myfolder/") 
    !call system("cd myfolder/") !doesn't work 
    ierr = chdir("myfolder") 
    if (ierr.NE.0) then 
     write(*,'(A)') "warning: change of directory unsuccessful" 
    end if 

    open(unit=33,file="myfile.txt",iostat=ierr) 
    if (ierr.EQ.0) then 
     write(unit=33,fmt='(A)') "Test message" 
     close(unit=33) 
    end if 
end program change_directory 

분명히 시스템 호출에서 cd myfolder/을 사용하면 작동하지 않습니다. Intel reference에 'use ifport'을 추가해야한다고 나와 있습니다. 하지만 GCC reference에는 그런 언급이 없습니다. 'use ifport'을 제외하고 위의 코드를 아무런 문제없이 ifort 아래에 컴파일 할 수 있습니다. 하지만 gcc는 ifport 모듈을 가지고 있지 않기 때문에 gcc로 컴파일하지 않을 것입니다.뿐만 아니라 인텔 포트란에서도 컴파일되지 않습니다. 다음과 같이됩니다. 오류 :

$ ifort change_dir.f90 -o change_dir 
change_dir.f90(5): error #6552: The CALL statement is invoking a function subprogram as a subroutine. [SYSTEM] 
    call system("mkdir -p myfolder/") 
---------^ 
compilation aborted for change_dir.f90 (code 1) 

제 질문은 다음과 같습니다. 더 좋은 방법이 있습니까? 가능한 한 컴파일러에 독립적 인 코드를 작성하고 싶습니다. 현재 gfortran/ifort와 mpif90/mpiifort를 주로 사용합니다.

답변

1

도 참조하십시오. Is there any way to change directory using C language?. chdir()POSIX call에 대한 고유 한 인터페이스를 인텔의 인터페이스와 독립적으로 만들 수 있습니다. Windows에서도 마찬가지입니다.

module chdir_mod 

    implicit none 

    interface 
    integer function c_chdir(path) bind(C,name="chdir") 
     use iso_c_binding 
     character(kind=c_char) :: path(*) 
    end function 
    end interface 

contains 

    subroutine chdir(path, err) 
    use iso_c_binding 
    character(*) :: path 
    integer, optional, intent(out) :: err 
    integer :: loc_err 

    loc_err = c_chdir(path//c_null_char) 

    if (present(err)) err = loc_err 
    end subroutine 
end module chdir_mod 


program test 

    use chdir_mod 

    call chdir("/") 

    call system("ls -l") 

end 

ifort

> gfortran chdir.f90 
> ./a.out 
celkem 120 
drwxr-xr-x 2 root root 4096 15. říj 14.42 bin 
drwxr-xr-x 5 root root 4096 15. říj 14.43 boot 
... 

을 실행할 때 sunf90에처럼 너무 작동합니다.

(참고 :...이 기본 characterc_char와 같은 인에 의존 즉, 매우 안전한 가정이다는 컴파일러가 불평이 아니다 및 변환이 이루어해야하는 경우)

관련 문제