check for carriage return exists at the end of input file

Neha_Sodhi

New Member
Hi

I am having a text file and i need to add a validation in progress code to check whether a carriage return exists at the end of file or not.
Please suggest.

Thanks in Advance.
 
If you are importing the file and want to check. Compare the result of SEEK TO END with SEEK after an import repeat block, a last line (without CRLF) can then be detected and read using READKEY.

An other easy approach (but which does read the entire file into memory):

Code:
DEF VAR lcc AS LONGCHAR NO-UNDO.

COPY-LOB FROM FILE "test.txt" TO lcc.

MESSAGE lcc MATCHES "*~r~n" VIEW-AS ALERT-BOX.

If you just want the answer (with seek):

Code:
DEF VAR ipos AS INT64 NO-UNDO.
DEF VAR ii   AS INT NO-UNDO.

INPUT FROM "test.txt".

SEEK INPUT TO END.
ipos = SEEK( INPUT ).
SEEK INPUT TO ( ipos - 2 ).
READKEY. /* crlf is one readkey... */
ii = LASTKEY.
INPUT CLOSE.

MESSAGE ii = 13 VIEW-AS ALERT-BOX.
 
Hi Stefan

Thanks for Reply.

I have tried this but it doesn't work, the Readkey function is taking so much time in execution and in first approach it seems to detect carriage return in whole file and i want to check carriage return at the end of file.
 
You should not be READKEYing the entire file. You should only use readkey when IMPORT no longer works - and that is on the last line of the file if it has not been CRLF terminated.

Please explain what you mean with 'seem to detect carriage return in whole file'.
 
Back
Top