Need an easy way to Delete a line in a text document.

walkeryan

Member
I just need an easy way to delete a single line in a .txt. Heres my problem: I have twp procedures that run simultaneously but one locks a certain database table even though I have shared lock or no-lock status. So since the second proc needs this table it will not run until the first one completes. I've tried wrapping them with DO TRANSACTIONS: with no luck. So my solution is to write the values I need to a text file and then line by line read the text document and delete the lines as the procedures process them. If anyone has a better solution to my first problem then I wont need an answer to my second, any thoughts/advice would be appreciate, thanks!
 

kolonuk

Member
It sounds like there is another process in the programs that is getting a lock... Or another completely different program! every for each and find should have a lock specified, preferably no-lock for viewing...

anyway, you could do this...

Code:
DEF VAR w AS CHAR NO-UNDO.
DEF STREAM newfile.
 
OUTPUT STREAM newfile TO VALUE("newfile.txt").
INPUT THRU VALUE("quoter oldfile.txt").
 
REPEAT
:
    IMPORT w.
    IF NOT (w BEGINS "ignore this line")
    THEN PUT STREAM newfile UNFORMATTED w SKIP.
END.
 
OUTPUT STREAM newfile CLOSE.

Basically reads one file in, checks the line, then writes it out to the new file. Then you neew to remove the old file, and rename the new file... Only works for ascii text though - binary requires a different stragegy... The quoter wraps quotes (") round the text, so that you get line by line input...
 

ABLsaurusRex

New Member
Much simpler would be to load the document into an editor widget, search for the line and delete it, and save the file.

As regards the first problem, you should always read no-lock, when you want to change it, re-read with exclusive-lock, make the change, finish the transaction and re-read with no-lock.

There are some specialized uses for share-locks, but most if the time they should not be used.
 

RKR

Member
Much simpler would be to load the document into an editor widget, search for the line and delete it, and save the file.

As regards the first problem, you should always read no-lock, when you want to change it, re-read with exclusive-lock, make the change, finish the transaction and re-read with no-lock.

There are some specialized uses for share-locks, but most if the time they should not be used.

Or even easier load the file into a temp-table and process each record.
But the best way is to read each each record with NO-LOCK (do not use share-lock) and only lock the record exclusively when you need to delete one record.

FOR EACH <table> NO-LOCK:
... process record.
RUN deleteRecord (INPUT ROWID(<table>).
END.

PROCEDURE deleteRecord:
DEFINE INPUT PARAMETER irRowidTable AS ROWID NO-UNDO.

DEFINE BUFFER b<table> FOR <table>.

FOR FIRST b<table> WHERE ROWID(b<table>) = irRowidTable EXCLUSIVE-LOCK:
DELETE b<table>.
END.
END.

By using a buffer in the internal procedure deleteRecord you scope the transaction and the locking of the record to the internal procedure.
 
Top