views:

768

answers:

3

I want to read the lines out of STDIN (aka SYSIN) in COBOL. For now I just want to print them out so that I know I've got them. From everything I'm reading it looks like this should work:

  IDENTIFICATION DIVISION.
  PROGRAM-ID. APP.

  ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
  FILE-CONTROL.
  SELECT SYSIN ASSIGN TO DA-S-SYSIN ORGANIZATION LINE SEQUENTIAL.

  DATA DIVISION.
  FILE SECTION.
  FD SYSIN.
  01 ln PIC X(255).
     88 EOF VALUE HIGH-VALUES.
  WORKING-STORAGE SECTION.
  PROCEDURE DIVISION.
  OPEN INPUT SYSIN
  READ SYSIN
  AT END SET EOF TO TRUE
  END-READ
  PERFORM UNTIL EOF
  DISPLAY ln
  READ SYSIN
  AT END SET EOF TO TRUE
  END-READ
  END-PERFORM
  CLOSE SYSIN
  STOP RUN.

That compiles (using open-cobol and cobc -x), but running it I get:

libcob: File does not exist (STATUS = 35) File : ''

What am I doing wrong?

+4  A: 

My COBOL dates back to the DPS-6 minicomputer runnong GCOS-6 and I lasted touched that in 1992. But back then we used ACCEPT to get input from stdin.

Paul Mitchell
I'm OK with using ACCEPT instead (although I've read that's considered bad practise for large data), but then how do I construct the loop to detect EOF?
singpolyma
+1  A: 

The following was suggested to me on the OpenCOBOL forums.

SELECT SYSIN ASSIGN TO KEYBOARD ORGANIZATION LINE SEQUENTIAL.

It's the keyword KEYBOARD that makes it work.

Apparently DISPLAY is a similar word for STDOUT, but I have not tested that.

singpolyma
A: 

You can just use the ACCEPT keyword to grab keyboard output. Loop through until you hit a keyword such as 'end', or you can use the hex value of EOF (1A, I believe).

As in:

1000-YOUR-PARAGRAPH.
  ACCEPT WS-YOUR-VARIABLE.
  DISPLAY WS-YOUR-VARIABLE.
  IF WS-YOUR-VARIABLE IS NOT EQUAL TO EOF
    THEN GO TO 1000-YOUR-PARAGRAPH
  ELSE GO TO 1090-EXIT
  END-IF.
1090-EXIT.
  EXIT.

That will take everything up to an EOL marker (e.g. return).

ahlatimer