cobol

Does a WRITE on an OUTPUT file overwrite previous rows?


Some thing I don't seem to understand about COBOL, which bothers me, is what happens if a file is opened in OUTPUT mode, and there are multiple writes to that file?

For example, for a file named TEST:

OPEN OUTPUT TEST
WRITE TEST-ROW FROM TEST-ROW-D END-WRITE
WRITE TEST-ROW FROM TEST-ROW-D END-WRITE

Will the final TEST file have two rows or a single row? I know that opening the file in OUTPUT mode sets the file cursor to the beginning, this meaning that the file will be overwritten. But I am not sure if after the second WRITE statement, that row would be added as a new one or will overwrite the previous row of the file.


Solution

  • OPEN only specifies how the file is ... opened. OUTPUT means "if it exists, then recreate (empty) at the same place; otherwise create according to the implementor rules" (depending on which COBOL environment you use, there may multiple places).

    WRITE means "add to the file, error if that isn't possible".

    Both ar rules defined in the COBOL standard and handled by every implementor I know (where you can create the file from the COBOL code).

    While this information is missing I assume that this is about a SEQUENTIAL file (either RECORD SEQUENTIAL or LINE SEQUENTIAL) without a KEY. In this case (if no other error like disk/set full occurs): You will have two identical records in the file afterwards. Note that these may only be "seen" on disk after CLOSE (depending on the cache/flushing done by the implementor/operating system).