Microsoft KB Archive/62207

Common Cause of Run-Time Error 018 in COBOL

PSS ID Number: Q62207 Article last modified on 05-25-1990

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: A common cause of a run-time error 018 is defining a file as a record sequential file when the file should really be defined as a line sequential file. The only time you need to use ORGANIZATION IS SEQUENTIAL is when the file is created within a COBOL program. ORGANIZATION IS SEQUENTIAL should not be used for a file that is created outside of COBOL, such as a file created in a word processor or text editor. Files that are being written to the printer or files that are created by a word processor or text editor should have ORGANIZATION IS LINE SEQUENTIAL in the SELECT clause. This information applies to Microsoft COBOL versions 3.00 and 3.00a for MS-DOS and MS OS/2.

More Information: The default file organization for Microsoft COBOL is ORGANIZATION IS SEQUENTIAL, specifying a record sequential file. Record sequential files are not just ASCII text files. They may contain a header record and extra information about the record’s size and status. Therefore, if a file is to be written to a printer or if it is created in a word processor or text editor, ORGANIZATION IS LINE SEQUENTIAL should be specified in the SELECT clause. A file created in a word processor or text editor must be treated as a line sequential file because when a file is created in this manner, each line is terminated with a 2-byte hexadecimal sequence, 0D0A (carriage return, linefeed). If a file is defined as record sequential instead of LINE SEQUENTIAL, the 0D0A sequence will be seen as part of the data to be read in instead of as a line terminator. When a record from a LINE SEQUENTIAL file is read in, data is read in until a 0D0A terminator is encountered. The remaining part of the record is then padded with blanks. When a record from a line sequential file is being written out to a file, the trailing blanks are removed and the 0D0A sequence is added to the end of the record. If you try to read a LINE SEQUENTIAL file as ORGANIZATION IS SEQUENTIAL, the program generates run-time error 018, “Read part record error: EOF before EOR or file open in wrong mode.” The following program generates the 018 run-time error if TEST.DOC is any file created from within a word processor or a text editor. The error is not generated if ORGANIZATION IS LINE SEQUENTIAL is added to the SELECT clause.

Code Example
INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT IN-FILE ASSIGN TO &quot;TEST.DOC&quot;. * To get rid of the 018 error, add the following line * and delete the period in the previous line. *     ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD IN-FILE LABEL RECORDS ARE OMITTED RECORD CONTAINS 80 CHARACTERS. 01 READ-IT-IN    PIC X(80). WORKING-STORAGE SECTION. 01 EOF      PIC X VALUE 'N'. PROCEDURE DIVISION. OPEN INPUT IN-FILE. READ IN-FILE AT END MOVE 'Y' TO EOF. PERFORM LOOP UNTIL EOF = 'Y'. CLOSE IN-FILE. STOP RUN. LOOP. DISPLAY READ-IT-IN. READ IN-FILE AT END MOVE 'Y' TO EOF. Copyright Microsoft Corporation 1990.