Microsoft KB Archive/50464

From BetaArchive Wiki

COBOL 2.x, 3.00 Don’t Support Random Binary-Mode File Access

PSS ID Number: Q50464 Article last modified on 11-13-1989

2.00 2.10 2.20 3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The ability to read from or write to any byte in a file, without automatically requiring any particular byte to delimit records or serve some other purpose, is generally called “binary-mode file access,” or “binary access.” The COBOL language does not support a random, binary-mode file access. Many other languages do support a random binary-mode access, such as the RANDOM access in most Microsoft BASICs, the BINARY access of the OPEN statement supported in Microsoft QuickBASIC Versions 4.00 and later, and the binary (“b”) mode of the fopen() function supported in Microsoft C. While Microsoft COBOL Versions 1.10, 1.12, 2.00, 2.10, 2.20, 3.00, and 3.00a don’t offer a random binary-mode access, COBOL 3.00 and 3.00a (but not Versions 1.x or 2.x) can use a sequential binary-mode access, through the use of ORGANIZATION IS SEQUENTIAL and fixed-length records. But this technique has some limitations described below. Fortunately, Microsoft COBOL 3.00 and 3.00a can call Microsoft C 5.10 functions. C supports binary files, accessing files randomly at any position, and binary and bit-wise operations on data. By writing a series of C functions to read the data and manipulate the data, a COBOL program could be enhanced to handle any type of file operation.

More Information:

Random, Binary-Mode File Access

The lack of a random, binary-mode file access is a limitation of the COBOL language itself. The COBOL language was defined by ANSI (the American National Standards Institute), not by Microsoft. This information applies to Microsoft COBOL Versions 1.10, 1.12, 2.00, 2.10, and 2.20 for MS-DOS and to Versions 3.00 and 3.00a for MS-DOS and MS OS/2. Both BASIC and C can open a file as an input stream and read or write any bytes in random order at any byte position in the file. In QuickBASIC Versions 4.00 and later, to use a BINARY file, the file is OPENed for BINARY, and then GET and PUT are used to read and write the bytes. The SEEK statement can specify the byte position (in any random order within the file) where the next GET will input or PUT will output. In C, all files that are opened (with the fopen function) are viewed as a stream of bytes. C has functions (such as getc, putc, and fseek) that support random and sequential binary-mode file access.

Sequential, Binary-Mode File Access in COBOL 3.00 and 3.00a

In COBOL 3.00 and 3.00a, a limited form of binary-mode file access can be attained by using ORGANIZATION IS SEQUENTIAL, with fixed-length records (the default). With fixed-length-record SEQUENTIAL access, a COBOL program can read from and write to any kind of file. This cannot be done with variable-length-record SEQUENTIAL access, which requires only COBOL 3.00- or 3.00a-generated files. Also, don’t confuse SEQUENTIAL access with LINE SEQUENTIAL access, where COBOL writes, or expects to read, a carriage return byte plus a line feed byte at the end of every record in a file. In COBOL, when you open a file with fixed-length-record SEQUENTIAL access, that file is viewed as just a sequence of bytes. Those bytes can have any values (from 0 to 255, the complete range allowed for a byte), including both printable and unprintable ASCII bytes. The program can read and write the bytes and treat them as being any type of data. This method even lets you read in a byte value of 26 decimal (also known as 1A in hexadecimal notation, or CTRL+Z) without getting an end-of-file status. There are several limitations of this binary-mode access in COBOL: 1. Because the file is opened with SEQUENTIAL access (ORGANIZATION), the program must read from the beginning and move sequentially to the end. The program cannot move to any random position in the file. 2. COBOL requires the file to be accessed with a fixed record length. All READs and WRITEs must be of a predetermined number of bytes. This means that to best handle a binary file of any size, the program should use a record size of 1 byte. (Larger record sizes might be clumsier to handle since you would probably want to examine each byte anyway.) The file description or FD PICTURE clause must be a PIC X(1). This means that the program should only read and manipulate 1 byte at a time. 3. Unfortunately, even though binary data can be read with the above method, it is very difficult in COBOL to manipulate binary data. There are no built-in functions to examine bytes of information. There are no bit-wise operators for data in COBOL. This also is a limitation of the COBOL language itself, not of Microsoft COBOL. Calling Microsoft C routines from COBOL 3.00 and 3.00a can work around these limitations.

Code Example

Below is an sample program that reads LINK.EXE as a file and writes it out byte by byte to a file called LINK2.EXE. After running this sample program, LINK2.EXE can be run just as if it were the original LINK.EXE program. This sample program shows a minimal form of binary file access from COBOL. Compile as follows: COBOL COPYIT.COB; Link as follows: LINK COPYIT; Below is COPYIT.COB: $SET ANS85 ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT BINARY-FILE ASSIGN TO DISK ORGANIZATION IS SEQUENTIAL. SELECT BINARY-OUT ASSIGN TO DISK ORGANIZATION IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD BINARY-FILE LABEL RECORDS OMITTED VALUE OF FILE-ID IS “LINK.EXE”. 01 BINARY-INP-REC PIC X(1). FD BINARY-OUT LABEL RECORDS OMITTED VALUE OF FILE-ID IS “LINK2.EXE”. 01 BINARY-OUT-REC PIC X(1). WORKING-STORAGE SECTION. 77 EOF-BINARY-FILE PIC X(4). PROCEDURE DIVISION. 000-MAIN-ROUTINE. OPEN INPUT BINARY-FILE, OUTPUT BINARY-OUT. READ BINARY-FILE AT END MOVE “YES” TO EOF-BINARY-FILE. PERFORM UNTIL EOF-BINARY-FILE = “YES” MOVE BINARY-INP-REC TO BINARY-OUT-REC WRITE BINARY-OUT-REC READ BINARY-FILE AT END MOVE “YES” TO EOF-BINARY-FILE END-PERFORM. CLOSE BINARY-FILE, BINARY-OUT. STOP RUN.

Copyright Microsoft Corporation 1989.