Thread: CR LF problem
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Robert Crandal[_3_] Robert Crandal[_3_] is offline
external usenet poster
 
Posts: 161
Default CR LF problem

I am using a user defined function, ReadTextFile(), to read
the contents of a text file into a string variable. The basic
code looks like this:

Dim s as string
s = ReadTextFile("C:\data.txt")

Now that the entire text file is stored in a string variable,
I would like to replace occurences of multiple carriage
returns (CR) or line feeds (LF) with a single carriage return.

In other words, I want all paragraphs to be single-spaced,
with only one CR and/or LF between paragraphs.

Finally, I need to write that single-space string to a new file,
and preserve the single-spaced format in the new file.

The biggest problem here, is that my text files come from
various sources, and each source uses different byte
representations of "carriage returns". I have noticed in some
files that a line break is represented by one CR (or
character 13 or hex "0D")....other files use CR-CR-LF
(or 13-13-10 or hex "0D-0D-0A") to represent a line break.
I think some files only use CR-LF ("0D-0A"), etc....

Does anyone know any good algorithms or functions that
can help replace any sequence of combinations of CR
and/or LF with just one CR LF line break?

Thank you

'-------------------------------------------------
' BTW, here is the ReadTextFile() function again:
Public Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()