Skip to main content
Question

CBL_OPEN_FILE Organization parameter for reading binary files

  • November 17, 2025
  • 1 reply
  • 20 views

M Carmen De Paz
Forum|alt.badge.img+2

Hi, we are working with MF visualcobol 8.0 for Eclipse in linux  and we want to read a big binary file (.pdf , .-jpg,...) from the linux server and insert it into a sqlserver bbdd in a windows server.

The program is compilede whit openesql

Asking for the use of the CBL_OPEN_FILE to the IA it say that we have to use this sintaxis, but in te MF documentation don´t presents parameter Organization

 

CALL "CBL_OPEN_FILE"

    USING BY VALUE 0              *> File handle (returned)

          BY REFERENCE FILE-NAME  *> Nombre del fichero

          BY VALUE 1              *> Mode: input

          BY VALUE 4              *> Organization: binary

          BY VALUE 0              *> Options

    RETURNING STATUS-CODE.

 

how must  we usei it ? 

1 reply

Chris Glazier
Forum|alt.badge.img+2

Hi,

Your AI’s syntax is completely wrong. You should reference the documentation instead.
Here is an example that will open and read the contents of a binary file such as a .jpg file.

 

      *----------------------------------------------------------------*
* Read Byte Stream File *
* *
* This program demonstrates how to use the Visual COBOL callable*
* library routines to open and read a file as a simple byte *
* stream. This program uses a data buffer size of 1000 and reads*
* the entire file in a single read so it assumes that the file *
* is not greater that 1000 bytes in size. The data buffer size *
* can be adjusted for larger files or the file could be read in *
* chunks using the file-offset parameter. *
* *
* The first call to CBL_READ_FILE with flags set to 128 will *
* return the file size into the file-offset parameter so we know*
* how many bytes to read. *
* *
* The data-buffer read will be moved to a dynamic length data *
* item using the file-size as the length so that trailing spaces*
* in the buffer will be discarded. *
*----------------------------------------------------------------*
identification division.
program-id. readbytestream.

environment division.
configuration section.

data division.
working-storage section.
78 BUFFER-SIZE value 1000.
01 file-content pic x dynamic.
01 data-buffer pic x(BUFFER-SIZE) value spaces.
01 filename pic x(256) value spaces.

01 access-mode pic x comp-x value 0.
78 access-read-only value 1.
78 access-write-only value 2.
78 access-read-write value 3.
78 access-greater-4GB value 64.

01 deny-mode pic x comp-x value 3.
78 deny-read-write value 0.
78 deny-write value 1.
78 deny-read value 2.
78 deny-none value 3.

01 device pic x comp-x value zeroes. *> always 0
01 file-handle pic x(4) comp-5 value zeroes.
01 file-offset pic x(8) comp-x value zeroes.
01 byte-count pic x(4) comp-x value zeroes.

01 flags pic x comp-x value zeroes.
78 read-filesize-only value 128.
78 read-data value 0.
procedure division.

move "myfile.jpg" to filename
compute access-mode = access-read-only + access-greater-4GB
move deny-write to deny-mode

call "CBL_OPEN_FILE" using filename
access-mode
deny-mode
device
file-handle
end-call
if return-code not = 0
display "error on open = " return-code
stop run
end-if

move read-filesize-only to flags
*> File-offset will contain the filesize
call "CBL_READ_FILE" using file-handle
file-offset
byte-count
flags
data-buffer
end-call
if return-code not = 0
display "error on get filesize = " return-code
stop run
end-if

move read-data to flags
move file-offset to byte-count *> read the entire file
*> buffer must be large enough
*> set the length of the dynamic length data-item that will
*> receive the contents of the file.
set length of file-content to byte-count
*> First byte in the file is at offset 0
move 0 to file-offset
call "CBL_READ_FILE" using file-handle
file-offset
byte-count
flags
data-buffer
end-call
if return-code not = 0
display "error on read file = " return-code
stop run
end-if

*> file-content will contain only the data read - extra bytes
*> in data-buffer
move data-buffer(1:byte-count) to file-content

call "CBL_CLOSE_FILE" using file-handle

goback.