Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
language:fortran [2018/06/11 09:43] – [Command line으로 Arguments를 받고 저장, 출력] ledyxlanguage:fortran [2021/02/07 03:15] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 {{tag>Language Fortran}} {{tag>Language Fortran}}
 +
 += gFortran에서 행 길이 제한 무시 =
 +
 +<sxh bash>
 +gfortran -ffree-line-length-none xxx.f90
 +# -ffree-line-length-0
 +</sxh>
 +
  
  
Line 13: Line 21:
   INTEGER :: i   INTEGER :: i
   CHARACTER(len=32) :: arg   CHARACTER(len=32) :: arg
-! CHARACTER(len=32) args(10)+ 
 +!CHARACTER(len=32) args(10)
  
 ! 동적 배열 선언 및 할당 ! 동적 배열 선언 및 할당
-CHARACTER(len=32), DIMENSION (:), ALLOCATABLE :: args+CHARACTER(len=50), DIMENSION (:), ALLOCATABLE :: args
 allocate(args(iargc())) allocate(args(iargc()))
  
   DO i = 1, iargc()   DO i = 1, iargc()
-  CALL getarg(i, arg) +   CALL getarg(i, arg) 
-  read(arg,*) args(i)+   read(arg,*) args(i)
     CALL getarg(i, args(i))     CALL getarg(i, args(i))
 +    args(i) = TRIM(args(i))
   END DO   END DO
  
-  DO i = 1, iargc() +print *,size(args) 
-    print *,args(i) + 
-  end do+do i = 1, iargc() 
 +        print *,args(i) 
 +end do
  
 ! 할당 해제 ! 할당 해제
Line 35: Line 47:
 </code> </code>
  
 += String, Integer 상호 변환 =
 +
 +* https://gcc.gnu.org/onlinedocs/gfortran/ICHAR.html
 +
 +<code fortran>
 +program read_val
 +  integer value
 +  character(len=10) string, string2
 +  string = '154'
 +  
 +  ! Convert a string to a numeric value
 +  read (string,'(I10)') value
 +  print *, value
 +  
 +  ! Convert a value to a formatted string
 +  write (string2,'(I10)') value
 +  print *, string2
 +end program read_val
 +</code>
 +
 +
 +== Command Line 응용 ==
 +
 +<code fortran>
 +PROGRAM test_getarg
 +  INTEGER :: i
 +  CHARACTER(len=8) :: arg
 +  INTEGER, DIMENSION (:), ALLOCATABLE :: args
 +
 +  allocate(args(iargc()))
 +
 +  DO i = 1, iargc()
 +    CALL getarg(i, arg)
 +    read(arg,'(I10)') args(i)
 +  END DO
 +
 +!print
 +  DO i = 1, SIZE(args)
 +        print *, args(i)
 +  end do
 +
 +  deallocate(args)
 +
 +END PROGRAM
 +</code>
 +
 += 문자열 동적 할당 =
 +<code fortran>
 +PROGRAM test
 + CHARACTER(len=128) :: arg
 + CHARACTER(len=:), ALLOCATABLE :: path
 +
 + INTEGER date(3)
 +
 + CALL getarg(1, arg)
 + path = TRIM(arg)
 +
 + DO i=1, 3
 +  CALL getarg(i+1, arg)
 +  READ(arg,'(I10)') date(i)
 + END DO
 +
 +print *,LEN(path)
 +print *,"path : ",path," /// ",date(1),date(2),date(3)
 +
 +END PROGRAM
 +</code>
language/fortran.1528706623.txt.gz · Last modified: 2021/02/07 03:15 (external edit)