0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fortranチートシート

Last updated at Posted at 2025-10-24

Fortranチートシート

 自分は普段Fortranを主に使っているのですが、チートシートらしいチートシートを作ってこなかったために毎回過去のコードを解読してコードの流れを思い出したり、GPT先輩に組み込み関数の仕組みを聞いたりしています。しかし、Fortanユーザーとして、これは褒められたものではありません。Fortranという最古の高級言語を使う以上、気品あるプログラマーとして、自分で作ったチートシートを参照するべきではないでしょうか。だって、Fortranは紳士のプログラミング言語だからね!
 というわけで、以下がそのチートシートになります。(随時編集するつもりです。)

入出力

ファイルから入力
program main
    implicit none
    integer::io,filesize,i
    real(8),allocatable::data_in(:)

    !fileを開く
    open(10,file='../datafile/input.dat',status='old',action='read')
    
    !ファイルサイズを取得する
    filesize=0
    read(10,'(A)')
    do
        read(10,'(A)',iostat=io)        
        if(is_iostat_end(io)) then
            exit
        end if
        filesize=filesize+1
    end do

    !filesizeを取得したら、data_inにallocateして、読み込みを開始する。
    allocate(data_in(filesize))
    rewind(10)
    do i = 1,filesize
        read(10,*) data_in(i)
    end do
    
    close(10)
    
end program

とにかくめんどくさい。まず一回、ファイルの終わりに到達したかどうかチェックしながらdoループを回すことで、filesizeを取得する。そしたら、データの入れ先の配列をfilesizeにallocateして、もう一度doループを回しながらデータを読み込んでいく。めんどくさい!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?