Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip CHR(26) or CRLF from the end of any files

Program source code

Strip CHR(26) or CRLF from the end of any files

by  dbMark  Posted    (Edited  )
Many programs today cannot handle a file which ends with character 26 (hexadecimal 0A), the end of file (EOF) marker commonly used in legacy DOS applications. In other cases you may want to strip off the final carriage return and line feed (CRLF) characters.

This routine will strip the last matching character(s) away. It does not hurt to run it more than once. If you run the program against the file and there are not matching characters at the end of the file, it will say no action was taken.

It has been enhanced to allow you to enter EOF or CRLF instead of those characters. Sample ways to call it:
DO StripChr WITH "A:\abc.txt",CHR(26))
DO StripChr WITH "A:\abc.txt","EOF")
DO StripChr WITH "A:\abc.txt",CHR(13)+CHR(10))
DO StripChr WITH "A:\abc.txt","CRLF")
DO StripChr WITH "A:\abc.txt","designed by Mark")

Note: This code to truncate the file and remove the last character(s) should be tested first on test files or duplicate copies until you are sure it works as you expect!

dbMark
Code:
PROCEDURE StripChr
PARAMETER cFile, cChr
DO CASE
CASE TYPE("cChr")<>"C" .OR. LEN(cChr)=0
   ? "Search characters not specified correctly."
CASE TYPE("cFile")<>"C" .OR. LEN(cFile)=0
   ? "File name not specified correctly."
CASE .NOT. FILE(cFile)
   ? "File not found."
OTHERWISE
   PRIVATE cMatch, nLen, cDesc, nHandle, fEnd, fChr, xVal
   cMatch=cChr
   IF UPPER(cMatch)="EOF"
      cMatch=CHR(26)
   ENDIF
   IF UPPER(cMatch)="CRLF"
      cMatch=CHR(13)+CHR(10)
   ENDIF
   nLen=LEN(cMatch)
   cDesc="Character"+IIF(nLen=1,"","s")
   nHandle=FOPEN(cFile,"RW")
   IF nHandle=0
      ? cFile+" could not be opened low level."
   ELSE
      ? "Processing "+cFile+"..."
      fEnd=FSEEK(nHandle,0,2)  && this is EOF
      xVal=FSEEK(nHandle,0-nLen,1)  && move to char(s) 
      fChr=FREAD(nHandle,nLen)  && get character(s)
      IF fChr=cMatch
         ? cDesc+" found at end of file. Removing..."
         fEnd=FSEEK(nHandle,0-nLen,1)  && move to char(s)
         xVal=FWRITE(nHandle,"",0)  && write nothing
      ELSE
         ? cDesc+" not found at end of file. No action taken."
      ENDIF
      xVal=FCLOSE(nHandle)  && truncates file after writing nothing
      ? cFile+" has been closed."
   ENDIF
ENDCASE
RETURN
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top