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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to strip the DOS EOF (0x1A) character from the end of a file

Program Source Code

How to strip the DOS EOF (0x1A) character from the end of a file

by  rgbean  Posted    (Edited  )
I've found that most programs that don't like the EOF character at the end of a text file, also don't like a trailing CRLF. If you don't have this problem, kill that part of the code. If you want to "see" it happen, undelete the * on the WAITs.
Code:
*!* STRIPEOF.PRG
PARAMETERS qcFileName

* Open the file with unbuffered read/write access
lnhandle = FOPEN(qcFileName,12)

* Test for possible file opening error
IF lnhandle  = -1
   WAIT WINDOW "Error Opening File: " ;
       + ALLTRIM(qcFileName)TIMEOUT 10
   RETURN
ENDIF

l_nSize = FSEEK(lnHandle,0,2)   && Determine file size
l_nOrgSize = l_nSize && save so we know if we've stripped anything
= FSEEK(lnHandle, -3, 2) && Sneak a peak at the last 3 characters
l_cLastChars = FREAD(lnHandle,3)
l_nLastChar = INT(ASC(SUBSTR(l_cLastChars,3,1)))

IF l_nLastChar = 26 && 0x1A && ASCII eof [or CHR(26)]
   *WAIT WINDOW "Stripping EOF" TIMEOUT 5 NOWAIT
   l_nSize = l_nSize - 1
   l_cLastChars = LEFT(l_cLastChars, 2) && throw away last
ENDIF

IF RIGHT(l_cLastChars, 2) = CHR(13)+CHR(10) && CRLF
   *WAIT WINDOW "Stripping CRLF" TIMEOUT 5 NOWAIT
   l_nSize = l_nSize - 2
ENDIF

IF l_nSize <> l_nOrgSize
   = FCHSIZE(lnHandle, l_nSize) && make it shorter
ENDIF

= FCLOSE(lnHandle)

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