Highlighted
Absent Member.
Absent Member.
3132 views

Read one specific record from indexed file

Jump to solution

Hello all,

I know its a simple quesiton, but I couldn't find any solution on the WEB.

I have an indexed file that i can entirely read with the code below:

*********************************************

program-id. Program1 as "apagar.Program1".
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
DECIMAL-POINT IS COMMA
CONSOLE IS CRT.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT CADBCO ASSIGN TO DISK
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CHAVE-BCO
ALTERNATE RECORD KEY IS CHAVE-BCO-1
WITH DUPLICATES.


DATA DIVISION.
FILE SECTION.

FD CADBCO BLOCK CONTAINS 32 RECORDS
RECORD CONTAINS 32 CHARACTERS
VALUE OF FILE-ID IS "C:/TMP/visual_files/cadbco"
DATA RECORD IS REG-BCO.
01 REG-BCO.
05 CHAVE-BCO PIC X(02).
05 FILLER REDEFINES CHAVE-BCO.
10 CODBCO-BCO PIC 9(03) COMP-3.
05 CHAVE-BCO-1 PIC X(30).
05 FILLER REDEFINES CHAVE-BCO-1.
10 ENDERE-BCO PIC X(30).


working-storage section.

*********************************************
procedure division.

*********************************************
OPEN I-O CADBCO.
MOVE ZEROS TO CODBCO-BCO.

START CADBCO KEY IS NOT LESS THAN CHAVE-BCO.


READ_AGAIN.

read cadbco NEXT.
DISPLAY REG-BCO.
STOP "".
GO TO READ_AGAIN.

goback.

end program Program1.

*********************************************

But, when i try to read a single specific record (with CODBCO-BCO = 114, for example) it fails with invalid key. Im sure the record exists.

Im trying to read with the code below:

*********************************************

program-id. Program1 as "apagar.Program1".
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
DECIMAL-POINT IS COMMA
CONSOLE IS CRT.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

SELECT CADBCO ASSIGN TO DISK
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CHAVE-BCO
ALTERNATE RECORD KEY IS CHAVE-BCO-1
WITH DUPLICATES.


DATA DIVISION.
FILE SECTION.

FD CADBCO BLOCK CONTAINS 32 RECORDS
RECORD CONTAINS 32 CHARACTERS
VALUE OF FILE-ID IS "C:/TMP/visual_files/cadbco"
DATA RECORD IS REG-BCO.
01 REG-BCO.
05 CHAVE-BCO PIC X(02).
05 FILLER REDEFINES CHAVE-BCO.
10 CODBCO-BCO PIC 9(03) COMP-3.
05 CHAVE-BCO-1 PIC X(30).
05 FILLER REDEFINES CHAVE-BCO-1.
10 ENDERE-BCO PIC X(30).


working-storage section.
*********************************************
procedure division.

*********************************************
OPEN I-O CADBCO.
MOVE 114 TO CODBCO-BCO.

read cadbco INVALID KEY DISPLAY "IT FAILS" AT LINE 10.

DISPLAY REG-BCO.
STOP "".

goback.

end program Program1.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

The value is being stored as if it were a signed COMP-3 instead of an unsigned COMP-3.

Unsigned COMP-3:

PIC 9(3) COMP-3  value 114.

are stored with an F as the sign byte so the two bytes would be 11 4F

signed COMP-3:

PIC S9(3) COMP-3 value 114.

are stored with a C as the positive sign byte so the two bytes would be 11 4C

If you change the definition of:

10 CODBCO-BCO PIC 9(03) COMP-3.

to:

10 CODBCO-BCO PIC S9(03) COMP-3.

then it should read it OK.

Are all the values in your file positive numbers?

View solution in original post

0 Likes
6 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

What you are doing should work OK, if the data is being stored as you believe it is.

I would think that because of the COMP-3 key which is redefining a PIC X(2) (not sure why you are doing this as the COMP-3 item can be a key directly) that perhaps the key values are not what you believe they are.

I would suggest that you perform the read next through the file while stepping through the code in the debugger so that you can query the key values being returned to see if the 114 is really on the file.

You could also display CODBCO-BCO after each read to see if the 114 actually exists.

If you have a full licensed copy of Visual COBOL then you could also download the Data Tools addpack from the supportline product downloads page which would let you open your indexed file directly in an editor.

If I change your program so that it writes some records out and then reads the record back by key it works fine...

           open output cadbco
           
           move all "1" to reg-bco
           move 110 to codbco-bco
           write reg-bco invalid key display "bad".
           move all "2" to reg-bco
           move 111 to codbco-bco
           write reg-bco invalid key display "bad".
           move all "3" to reg-bco
           move 114 to codbco-bco
           write reg-bco invalid key display "bad".
           move all "4" to reg-bco
           move 116 to codbco-bco
           write reg-bco invalid key display "bad".
           
           close cadbco
           OPEN I-O CADBCO.
           MOVE 114 TO CODBCO-BCO.
           read cadbco INVALID KEY DISPLAY "IT FAILS" AT LINE 10.

           DISPLAY REG-BCO.
           STOP " ".
           goback.
0 Likes
Highlighted
Absent Member.
Absent Member.

Ive done some tests, and it looks like the COMP-3 stored value differs from the 114 compressed value (with COMP-3 too).

The indexed file was build using a very old COBOL (20+ years). Did the COMP-3 calculation changed during these years? Is it possible to "use a old COMP-3"?

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Can you give me an example of how the data is being stored for the key with the 114 value?

What are the actual hex bytes being returned in the CHAVE-BCO PIC X(02) field for this?

0 Likes
Highlighted
Absent Member.
Absent Member.

The first line is the 114 compressed with COMP-3.

The second line is the stored value in the indexed file.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

The value is being stored as if it were a signed COMP-3 instead of an unsigned COMP-3.

Unsigned COMP-3:

PIC 9(3) COMP-3  value 114.

are stored with an F as the sign byte so the two bytes would be 11 4F

signed COMP-3:

PIC S9(3) COMP-3 value 114.

are stored with a C as the positive sign byte so the two bytes would be 11 4C

If you change the definition of:

10 CODBCO-BCO PIC 9(03) COMP-3.

to:

10 CODBCO-BCO PIC S9(03) COMP-3.

then it should read it OK.

Are all the values in your file positive numbers?

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

Thanks chris, I changed the definition like you said and everything works fine now.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.