fgetc(),fputc()のサンプル2015年02月08日 09:43

fgetc(),fputc()のサンプルを示します。

まずは、typex.for。テキストファイルをタイプアウトするプログラムです。

c typex.for -- type out test file
      program typex
      integer*1 c, fname(256)
      integer*1 fgetc
      integer fopen, getarg
      integer fin

      call initfile()

      if (getarg(1,fname,256) .eq. -1) then
          call error('USAGE: typex filename.')
      end if

      if (fopen(fin,fname,82) .eq. -1) then
          call error('file not found.')
      end if

      while (fgetc(fin,c) .ne. -1) do
          call putc(c)
      end while

      call fclose(fin)

      stop
      end
initfile()でファイルルーチンを初期化してから、fopen()します。

次は、filecopy.forです。ファイルをコピーするプログラムです。

c copyfile.for -- copy file
      program copyfile
      integer*1 c, finame(256), foname(256)
      integer*1 fgetc
      integer fopen, getarg
      integer fin, fout, junk

      call initfile()

      if (getarg(1,finame,256) .eq. -1) then ! EOF(-1)
          call error('USAGE: copoyfile source distination.')
      else if (getarg(2,foname,256) .eq. -1) then ! EOF(-1)
          call error('USAGE: copoyfile source distination.')
      end if

      if (fopen(fin,finame,82) .eq. -1) then ! LETR(82) EOF(-1)
          call error('source file not found.')
      end if

      junk = fopen(fout,foname,87)      ! LETW(87)

      while (fgetc(fin,c) .ne. -1) do   ! EOF(-1)
          call fputc(fout,c)
      end while

      call fclose(fin)
      call fclose(fout)

      stop
      end

ファイル関連のサブプログラムの使い方を紹介しました。