chenwei
asked on
COBOL question: How to delete a record from a file?
Here is the program. I want to delete the record if the first CHAR = "D". How can I write the code?
************************** ********** ********** **
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE.
AUTHOR My Name.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FD-INPUT
ASSIGN TO "test.txt"
ACCESS SEQUENTIAL
ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FD-INPUT.
01 PRINT-REC PIC X(60).
WORKING-STORAGE SECTION.
01 EOF-FLAG PIC 9(1).
88 END-OF-IN-FILE VALUE 1.
PROCEDURE DIVISION.
MAIN-PARAGRAPH.
OPEN INPUT FD-INPUT
PERFORM WITH TEST AFTER UNTIL END-OF-IN-FILE
READ FD-INPUT
AT END SET END-OF-IN-FILE TO TRUE
NOT AT END
IF PRINT-REC (1:1) = "D"
PERFORM PRINT-DETAILS
END-IF
END-READ
END-PERFORM
CLOSE FD-INPUT
STOP RUN.
PRINT-DETAILS SECTION.
* print out the record first
DISPLAY PRINT-REC
* Then delete this record
DELETE ??????????(what should I write here?)
CONTINUE.
**************************
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE.
AUTHOR My Name.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FD-INPUT
ASSIGN TO "test.txt"
ACCESS SEQUENTIAL
ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FD-INPUT.
01 PRINT-REC PIC X(60).
WORKING-STORAGE SECTION.
01 EOF-FLAG PIC 9(1).
88 END-OF-IN-FILE VALUE 1.
PROCEDURE DIVISION.
MAIN-PARAGRAPH.
OPEN INPUT FD-INPUT
PERFORM WITH TEST AFTER UNTIL END-OF-IN-FILE
READ FD-INPUT
AT END SET END-OF-IN-FILE TO TRUE
NOT AT END
IF PRINT-REC (1:1) = "D"
PERFORM PRINT-DETAILS
END-IF
END-READ
END-PERFORM
CLOSE FD-INPUT
STOP RUN.
PRINT-DETAILS SECTION.
* print out the record first
DISPLAY PRINT-REC
* Then delete this record
DELETE ??????????(what should I write here?)
CONTINUE.
The file is sequential so you cant simply delete a record out of it. if you had create a vsam file it would be another matter. obviously you are running this code on a pc so we won't go into jcl and all the other lovely ways of working around the limitations of sequential files on a mainframe. what you can do is create another file (output) and then read the input and for each record write out to the output file. if the record is to be deleted do not write it and move on to the next record.
ASKER
Thanks.
If I define the input file as a relartive file, etc. a strcutured file. Is it possible to delete a record?
************************** ********** ********** *
FILE-CONTROL.
*-------------*
SELECT FD-EINGABE
ASSIGN TO "#DYNAMIC"
ORGANIZATION RELATIVE
ACCESS SEQUENTIAL
FILE STATUS IS WS-INPUT-FILE-STAT.
If I define the input file as a relartive file, etc. a strcutured file. Is it possible to delete a record?
**************************
FILE-CONTROL.
*-------------*
SELECT FD-EINGABE
ASSIGN TO "#DYNAMIC"
ORGANIZATION RELATIVE
ACCESS SEQUENTIAL
FILE STATUS IS WS-INPUT-FILE-STAT.
ASKER
Besides, I am working on TANDEM computer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Really a good help. Thanks!