test_read.f90
count_vaild_dataのラベルのついたdoループ
#で始まる行はコメント行とみなしスキップし、コメント行以外の行数をカウントします。
skip_commentのラベルのついたdoループ
#で始まる行はコメント行とみなしスキップし、コメント行以外の行のデータを読み込みます。
infle: 入力ファイル名
strm: 1行のデータを一時的に読み込む変数
nt: 総データ数
x(:): 入力データを収納する配列
test_read.f90
program test_readcharacter(len=500)::inflereal,allocatable::x(:) character(len=2000)::strmnamelist /para/infleread(*,nml=para)open(11,file=infle,action="read")n=0count_vaild_data: do read(11,'(A)',iostat=ios)strm if(ios<0)exit if(strm(1:1) == "#")then cycle else n=n+1 endifenddo count_vaild_datant=nrewind(11)allocate(x(nt))n=0skip_comment: do read(11,'(A)',iostat=ios)strm if(ios<0)exit if(strm(1:1) == "#")then cycle else n=n+1 read(strm,*)x(n) endifenddo skip_commentprint *do n=1,nt print *,'x(', n ,')=',x(n)end doprint *end program test_read
test_read.run.sh
#/bin/bash
exe=test_readinfle="test_data.txt"cat << EOF > $infle# Comment 1# Comennt 2123# Comment 445EOFecho "${infle}: "cat $infleechonamelist=${exe}.namelist.txtcat <<EOF > $namelist¶infle=$infle&endEOF$exe < $namelistexit 0$ chmod u+x *.sh
$ test_read.run.sh
test_data.txt:
# Comment 1
# Comennt 2
1
2
3
# Comment 4
4
5
x( 1 )= 1.000000
x( 2 )= 2.000000
x( 3 )= 3.000000
x( 4 )= 4.000000
x( 5 )= 5.000000