ファイルに対して SELECT 句で FILE STATUS IS を指定し、作業場所節でファイル状態データ項目を定義すると、プログラムの各ファイルに、関連付けられたファイル状態データ項目を設定できます。
select in-file
assign to "user.dat"
file status is ws-file-status.
...
working-storage section.
01 ws-file-status pic xx.
各ファイルで別個のデータ項目を使用することも、複数のファイルで 1 つのデータ項目を使用することも可能です。
ファイル状態は、2 バイトのコードです。拡張ファイル状態コード以外のファイル状態の規則では、データ項目は英数字 2 文字 (PIC XX) で指定します。拡張ファイル状態コードの場合は、2 番目のバイトにはバイナリの数字が格納されます。
ファイル状態データ項目に格納された最初のバイトは、状態キー 1 と呼ばれ、入出力操作の結果として次のいずれかの状態を示します。
ファイル状態データ項目の最初のバイトが 9 以外である場合には、2 番目のバイト (状態キー 2 と呼ばれる) は英数字になります。
ファイル状態データ項目の最初のバイトが 9 である場合には、2 番目のバイトは、ランタイム システム エラー コードを含むバイナリ フィールドになります。
次のコード例は、ファイル状態の確認方法を示します。まず、最初のバイト (状態キー 1) を調べ、より詳細な情報が必要な場合に 2 番目のバイト (状態キー 2) を確認します。
select recseq
assign to "recseq.dat"
file status is ws-file-status
organization is record sequential.
...
file section.
fd recseq
record contains 80 characters.
01 recseq-fd-record pic x(80).
...
working-storage section.
01 ws-file-status.
05 status-key-1 pic x.
05 status-key-2 pic x.
05 binary-status redefines status-key-2 pic 99 comp-x.
...
procedure division.
...
perform check-status.
...
check-status.
evaluate status-key-1
when "0"
next sentence
when "1"
display "end of file reached"
perform check-eof-status
when "2"
display "invalid key"
perform check-inv-key-status
when "3"
display "permanent error"
perform check-perm-err-status
when "4"
display "logic error"
when "9"
display "run-time-system error"
perform check-mf-error-message
end-evaluate.
...
check-eof-status.
if status-key-2 = "0"
display "no next logical record"
end-if.
...
check-inv-key-status.
evaluate status-key-2
when "2" display "attempt to write dup key"
when "3" display "no record found"
end-evaluate.
...
check-perm-err-status.
if status-key-2 = "5"
display "file not found"
end-if.
...
check-mf-error-message.
evaluate binary-status
when 002 display "file not open "
when 007 display "disk space exhausted"
when 013 display "file not found "
when 024 display "disk error "
when 065 display "file locked "
when 068 display "record locked "
when 039 display "record inconsistent "
when 146 display "no current record "
when 180 display "file malformed "
when 208 display "network error "
when 213 display "too many locks "
when other
display "not error status "
display binary-status
end-evaluate.