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?

More than 1 year has passed since last update.

fortranで連番のファイル名を作るには?

Last updated at Posted at 2023-03-04

連番のファイル名を作りたいことがある。そのとき数字を文字列に
変換しないといけないのだけどread文を使えなどと書いているものもある。
それでもいいけれども以下のプログラムxtのほうが便利かもしれない。

program testxt
  character(8)::xt
  do i=1,100
     write(6,*)'file'//trim(xt(i))
  enddo
end program

character(8)  function xt(num)
  integer :: num
  if(num==0) then
     xt=''
     return
  endif
  xt = char(48+mod(num,10))
  if(num>9)     xt = char(48+mod(num/10,10))//xt
  if(num>99)    xt = char(48+mod(num/100,10))//xt
  if(num>999)   xt = char(48+mod(num/1000,10))//xt
  if(num>9999)  xt = char(48+mod(num/10000,10))//xt
  if(num>99999) xt = char(48+mod(num/100000,10))//xt
  if(num>999999) call exit(-1)
  xt='.'//xt
END function xt

追記:fortranプログラムでもcall exitで終了しないとそれをドライブするシステム側でがエラーキャッチできない。

0
0
1

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?