File Input Output in COBOL

Published on 05 May 2026 (Updated: 05 May 2026)

Welcome to the File Input Output in COBOL page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

identification division.
program-id. file-input-output.

environment division.
input-output section.
file-control.
    select output-file assign to "output.txt"
        organization is line sequential
        file status is ws-status.

data division.
file section.
fd output-file.
01 file-line pic x(256).

working-storage section.
01 ws-status        pic xx.
   88 file-ok       value "00".
   88 file-eof      value "10".

01 ws-message       pic x(256) value "Hello from COBOL!".
01 ws-eof-flag      pic x value "n".
   88 eof           value "y".
   88 not-eof       value "n".

procedure division.
main.
    perform write-file
    perform read-file
    stop run.

write-file.
    open output output-file

    if not file-ok
        display "Error: could not open file for writing"
        stop run
    end-if

    move ws-message to file-line
    write file-line

    close output-file.

read-file.
    open input output-file

    if not file-ok
        display "Error: could not open file for reading"
        stop run
    end-if

    set not-eof to true
    perform until eof
        read output-file
            at end
                set eof to true
            not at end
                display function trim(file-line)
        end-read
    end-perform

    close output-file.

File Input Output in COBOL was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.