cobol

Reading cobol file line by line seperated by new line character


I'm having trouble reading a file line-by-line. This is a current snippet of the code:

file-control.
    select input-file
    assign input-file-name
    organization is sequential
file section.
    fd input-file.
    01 input-file-record picturex(25)
working-storage section.
01 ws-eof picture a(1).

and here's where I actually read in the file:

perform until ws-eof = 'Y'
    read input-file into input-file-record
    at end move 'Y' to ws-eof
    not at end
        display input-file-record
    end-read
end-perform
    close input-file

The problem is, i'm trying to read the file line by line, but it seems like it's just filling up 25 characters, then reading again instead of looping by the return character in the text file.

The text file would look something like this:

AAAA
BBBB
CCCC
DDDD

Solution

  • The problem is, i'm trying to read the file line by line, but it seems like it's just filling up 25 characters, then reading again instead of looping by the return character in the text file.

    The system is exactly doing what you tell it to do:

    organization is sequential             *>  sequential, fixed length
    01 input-file-record picture x(25)     *>  the fixed length is 25 bytes
    

    Depending on the compiler you use (it is always a good idea to specify this if there isn't a specific tag for it already that you can use, and even in this case the version number never harms) you can either use the common extension (which may even get standard with COBOL 202x):

    organization is line sequential        *>  sequential, read until line break found
    

    or have to read it sequential (in this case likely with a bigger size) and

    inspect file-rec converting all x'0d' by x'0a' *> if you expect windows or mac line breaks move 1 to strpoint unstring file-rec delimited by all x'0a' into real-rec with pointer strpoint end-unstring