超音波流体屋のプログラム備忘録

Fortran汎用コード

最終更新:

usapfrog

- view
管理者のみ編集可

行番号カウント

コマンド wc -l の手実装版
  1. integer function wc_fort(inpfile)
  2. implicit none
  3. character(*) :: inpfile
  4. integer :: stat, num_inp
  5.  
  6. open(11, file=inpfile, status='old', iostat=stat)
  7. if(stat .ne. 0) then
  8. print *, 'Input file does not exist, exit...'
  9. stop
  10. end if
  11. num_inp = -1
  12. do while(stat .eq. 0)
  13. read(11, *, iostat=stat)
  14. num_inp = num_inp + 1
  15. end do
  16. close(11)
  17.  
  18. wc_fort = num_inp
  19. end function wc_fort
  20.  


ディレクトリ生成

多分もう少しきれいに書ける気がする
  1. subroutine prep_dir(status, outdir)
  2. !use ifport !intel fortranなら
  3. implicit none
  4.  
  5. integer :: status
  6. character(*) :: outdir
  7. character(len=256) :: syscall
  8. character(len=16) :: devnull = 'nul' !linuxなら /dev/nullにする
  9. !character(len=16) :: devnull = '/dev/null'
  10.  
  11. character(len=64), parameter :: CMDPATH = ''
  12. !character(len=64), parameter :: CMDPATH = 'C:\MinGW\msys\1.0\bin\'
  13.  
  14. write(syscall, '(a,a,a,a,a)'), 'ls ', trim(outdir), '>', devnull, ' 2>&1'
  15. status = system(trim(syscall))
  16.  
  17. if (status .ne. 0 ) then
  18. write(syscall, '(a,a,a)'), trim(CMDPATH), 'mkdir ', trim(outdir)
  19. status = system(trim(syscall))
  20. if (status .ne. 0) then
  21. print *, 'Fail to create dir :', outdir
  22. status = -1
  23. return
  24. end if
  25. end if
  26.  
  27. status = 1
  28. end subroutine prep_dir
  29.  


特定シンボルまでファイル読み飛ばし

  1. integer function Skip_Line_Until(symbol, fi, line)
  2. character(*), intent(in) :: symbol
  3. integer, intent(in) :: fi
  4. character(*), intent(inout) :: line
  5.  
  6. integer :: num_skiped, status
  7. logical :: wait_symbol
  8.  
  9. status = 0; num_skiped = 0; wait_symbol = .true.
  10.  
  11. do while( wait_symbol .and. (status .eq. 0) )
  12. read(fi, '(a)', iostat=status), line
  13. num_skiped = num_skiped + 1
  14. wait_symbol = (index(line, symbol) .eq. 0)
  15. end do
  16.  
  17. if ( status .ne. 0 ) then
  18. Skip_Line_Until = -1
  19. else
  20. Skip_Line_Until = num_skiped
  21. end if
  22. end function Skip_Line_Until
  23.  
目安箱バナー