2014년 5월 19일 월요일

Fortran : string 관련 tips


1. Change between numbers and characters(string)
  (1) Use CHAR(48+num) trick: However this works only for 1 digit number
  (2) Use internal file method :

* To convert number into strings, write

          WRITE(string_name,format) number

          (be careful that we need to use I3.3 instead of I3 to
          put zeroes on the left of number )

        * To convert strings into number, read

         READ(string_name,format) number

   (3) If we need only part of  string, use slicing

         string(begin:end)

       *In case of one character, string(num) does not work.
        Have to use

         string(num:num)  

   예를 들어서 길이가 달라지는 변수에 대해 format string을 쓰고 싶은 경우. 
   길이가 n 인 실수 array v(1:n) 를 출력한다고 하자. 
 
    WRITE(fmt_str,'(a,i2.2,a)') '(1x,' , n , '(f15.7,1x))'     

    ( n=3인 경우) fmt_str 은 '(1x,03(f15.7,1x))' 가 되고 

    WRITE(* , trim(fmt_str)) v    
    로 v 를 출력할 수 있다. 
  

2. Deal with system commands in Fortran
(1) get_command_argument(i,arg): get command arguments  
                             return i-th command argument as string
                             0-th argument is command itself

(2) SYSTEM( strings  )  : use system command

(3) TRIM : Removes trailing blank characters of a string

(4) call getcwd(path): get current working directory
      call chdir(path)   :change current working directory
      포트란의 기본 인풋/아웃풋 들, 파일 핸들링은 current working directory를
      기준으로 한다. call SYSTEM(‘cd path’)
      does not actually change the environment for fortran

3. quotes in strings : ‘’ represent ‘ in string. thus ‘’’ x’’’ -> ‘x’

4. Fw.d : 전체 캐릭터 수가 w 이고,( 부호, . 포함), 소수점뒤로 d 캐릭터
   Ew.d:  전체 캐릭터 수가 w 이고,( 부호, ., E+00 포함) 소수점 뒤로 d 캐릭터

댓글 없음:

댓글 쓰기