How to delete empty lines from text file?
;==========================================================================================================================================
;   How to delete empty lines from text file?
;
;   Detlev Dalitz.20040327.20090504.
;==========================================================================================================================================
;
;   If you have a text file, which lines are delimited by the DOS standard end-of-line
;   delimiter sequence @CR@LF, then there exist some methods to delete empty lines.
;
;------------------------------------------------------------------------------------------------------------------------------------------
;   Contents
;
;   0. Understand lines of text as a stream of characters.
;   1. Using FileRead and FileWrite.
;   2. Using Binary Buffer functions.
;   3. Using FileGet and FilePut.
;
;------------------------------------------------------------------------------------------------------------------------------------------
;   0. Understand lines of text as a stream of characters.
;
;   Example:
;   Look at these three lines,
;   they are the example lines,
;   to explain how line delimiters work.
;
;   What you see here in the document is:
;   three lines of text.
;
;   In a disk file these lines are organized as a stream of characters,
;   where the lines are 'delimited' or 'connected' by a sequence of two characters,
;   called 'Carriage Return' (@CR) and 'Linefeed' (@LF)
;   (in former times data transport was done by type writer telegraphs).
;
;   The text stream is stored as:
;   "Look at these three lines,@CR@LFthey are the example lines,@CR@LFto explain how line delimiters work.@CR@LF"
;
;
;   More simple view to the problem:
;
;   The text stream looks like:
;   "text@CR@LFtext@CR@LFtext@CR@LF"
;
;   If there is an empty line in the stream, it looks like:
;   "text@CR@LF@CR@LFtext@CR@LF"
;
;   You see, an empty line is referenced by two following @CRLF sequences.
;
;   If you replace two following @CRLF sequences by one sequence, then you delete one empty line.
;   "text@CR@LF@CR@LFtext@CRqLF" ==> "text@CR@LFtext@CR@LF"
;
;
;   Other view to the replacing problem:
;
;   Replacing of the inner sequence "@LF@CR" with nothing has the same result
;   as replacing two @CR@LF's with one @CR@LF.
;
;   "@CR@LF@CR@LF" ==> "@CR@LF"
;   "...======..." ==> "......"
;
;   I will use this technique in further examples to speed the replacing action a little bit.
;
;   But keep in mind:
;   This only works on DOS standard delimited text files,
;   where a line is delimited by a sequence of two special characters @CR@LF,
;   the 'CarriageReturn+Linefeed' sequence.
;
;------------------------------------------------------------------------------------------------------------------------------------------
;
;   1. Using FileRead and FileWrite.
;
;   Use the FileRead and FileWrite functions.,
;   Read line by line from input file,
;   skip empty lines,
;   and write the good lines to output file.

strFilenameIn = "input.txt"
strFilenameOut = "output.txt"

hdlFR = FileOpen (strFilename, "READ")
hdlFW = FileOpen (strFilename, "WRITE")

While @TRUE
   strLine = FileRead (hdlFR)
   If strLine == "" Then Continue
   If strLine == "*EOF*" Then Break
   FileWrite (hdlFW, strLine)
EndWhile

hdlFW = FileClose (hdlFW)
hdlFR = FileClose (hdlFR)
Exit


;------------------------------------------------------------------------------------------------------------------------------------------
;
;   2. Using Binary Buffer functions.
;
;   Instead of reading a text file line by line,
;   you may read the entire file into a binary buffer,
;   and use binary buffer replace function to eliminate
;   empty lines, then write the buffer back to the same
;   filename or to a new filename.
;
;   Following example changes the input file and saves it back to itself.

strFilename = "input.txt"
intFilesize = FileSize (strFilename)
If intFilesize > 0
   hdlBB = BinaryAlloc (intFilesize)
   intResult = BinaryRead (hdlBB, strFilename)
   intResult = BinaryReplace (hdlBB, StrCat (@LF, @CR), "", @TRUE)
   intResult = BinaryWrite (hdlBB, strFilename)
   hdlBB = BinaryFree (hdlBB)
EndIf
Exit


;------------------------------------------------------------------------------------------------------------------------------------------
;
;   3. Using FileGet and FilePut.
;
;   Instead of reading a text file line by line,
;   you may read the entire file into a string by FileGet(),
;   and use string replace function to eliminate
;   empty lines, then write the string back to the same
;   filename or to a new filename by FilePut().
;
;   Following example reads a text file and saves changed text to new output file.

strFilenameIn = "input.txt"
strFilenameOut = "output.txt"
strString = FileGet (strFilenameIn)
If strString != ""
   strString = StrReplace (strString, StrCat (@LF, @CR), ""))
   intResult = FilePut (strFilenameOut, strString)
EndIf
Exit


;   Almost the same as above, but condensed into one code line:

intResult = FilePut ("output.txt", StrReplace (FileGet ("input.txt"), StrCat (@LF, @CR), "")))
Exit

;------------------------------------------------------------------------------------------------------------------------------------------
;==========================================================================================================================================