Help Reading Lines in a Text File
I have a TXT file that I need to import through the app, but for some reason I need to open it in WordPad first and then save it before importing. I assume this is due to Line Breaks. Cause if I first open it in notepad there are no line breaks, but if I open it with a word, the lines are split.
Does anyone know why this is happening and how I can avoid having to manually open the file and save it with the dictionary panel? The app is written in vb 6 (Yikes!).
Thanks for any help
a source to share
This is a line ending issue. Your code (and notepad) wants to see Carriage Return (CR) / Line Feed (LF) pairs, and this is probably CR only (Macintosh) or LF only (Unix) file. Wordpad is more forgiving, and after saving, it appears to (haven't tested it) saves CR / LF pairs for you.
You can modify your code in the application to look for any of the endings and handle them easily: just stop looking for vbCrLf as a pair and look for either the end of the string. My own strategy is to scan CR or LF and consume all CR / LF characters that follow: this clears up blank lines as well.
a source to share
The file probably only has a carriage return (CR) or line feed (LF) character at the end of each line.
On Windows, you need both the CR and LF character at the end of each line. This can be easily done in VB6 using the vbCRLF constant.
On the other hand, if you are reading a file, you can determine which one is missing and manually add it when you read the file (i.e. using the replace function to convert CR to CRLF or LF to CRLF).
a source to share
If these files are not very large and performance is critical, reading them line by line can be easily done using the ADODB.Stream object.
Not only does this handle multiple line separators (Stream.LineSeparator = adCR, adCRLF, or adLF), it can also be used to handle files containing Unicode (UTF-16), UTF-8, ANSI system code page, and an alternative "ANSI" for other locales.
For example, if you have a text file containing "ANSI" from the Russian locale, you can set Stream.Charset = "koi8-r" and read the data with the correct translation into Unicode (UTF-16) VB6: / p>
Dim Stm As ADODB.Stream
Dim Line As String
Dim Counter As Long
Set Stm = New ADODB.Stream
With Stm
.Open
.LoadFromFile "russian.txt"
.Type = adTypeText
.Charset = "koi8-r"
.LineSeparator = adLF
Do Until .EOS
Line = .ReadText(adReadLine) 'Text is in Unicode now.
Counter = Counter + 1
Loop
.Close
End With
Charset defaults to "unicode" (UTF-16), but to read or write an ANSI stream with the default codepage, you can set it instead of "ascii".
HKCR \ MIME \ Database \ Charset contains available values.
a source to share