How to read text file by replacing a character as line break
I have a text file which I can import (read) if it is presented as separate lines. However sometimes I get the file with all the lines next to each other as if there is no line break. So the Line Read and While Not EOF command reads it as a single line whereas there may have been over 10,000 lines. I noticed that if I open this file in notepad it shows it as a single line without any breaks (but with a small box after each line which should have been a line break) but if I open it in TextPad it shows all the separate lines.
Now, since I may get both types of files, I need the application to be able to:
1. check if they are separate lines or a single line
2. break the Line Read text into separte lines and then process further.
I am attaching 2 separate attachments (withoutbreak.txt and withbreak.txt). Both these files look identical in TextPad but different if opened in notepad. A "*" is what should indicate end of line, so if you can guide me how to use it to indicate a line break and still continue with While Not EOF and Line Read to read the lines as separate lines, it'd be great!
Re: How to read text file by replacing a character as line break
Try this for Withoutbreak.txt file:
Code:
Option Explicit
Private Sub Command1_Click()
Dim strTemp As String
Dim strContent() As String
'~~~ Read the entire line
Open "c:\withoutbreak.txt" For Input As #1
Line Input #1, strTemp
Close #1
strContent = Split(strTemp, vbLf) '~~~ Split the string
'~~~ Display the lines or do whatever you want
Dim i As Long
For i = LBound(strContent) To UBound(strContent)
Debug.Print strContent(i)
Next
End Sub
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
Re: How to read text file by replacing a character as line break
as you do not show what code you are currently using....
vb Code:
open "somefile.txt" for input as 1
strfile = input(lof(1), #1) ' read entire file into string
close 1
if instr(strfile, vbnewline) = 0 then strfile = replace(strfile, vblf, vbnewline) ' if no new lines replace linefeed character with new line
' if you want the file contents in an array
arrfile = split(strfile, vbnewline)
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: How to read text file by replacing a character as line break
The other way is to remove the vbCr rather than add the vbLf and then have to remove it with Split
Code:
Dim intFile As Integer
Dim strData As String
Dim strFile As String
Dim strRecords() As String
intFile = FreeFile
strFile = "C:\somefile.txt"
Open strFile For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
If InStr(strData, vbCrLf) <> 0 Then strData = Replace(strData, vbCr, "")
strRecords = Split(strData, vbLf)
It's as broad as it is long !
Last edited by Doogle; Sep 13th, 2010 at 07:18 AM.
Re: How to read text file by replacing a character as line break
Replace() takes more time on large text. This should be quicker:
Code:
Open "C:\Temp\SomeFile.txt" for Input as #1
sData = Input(LOF(1), 1)
Close #1
If Instr(sData, vbCrLf) Then
sLines = Split(sData, vbCrLf)
Else
sLines = Split(sData, vbLf)
End If
Or
Code:
If Instr(sData, vbCrLf) Then sLineBreal = vbCrLf Else sLineBreak = vbLf
sLines = Split(sData, sLineBreak)
Don't forget to use [CODE]your code here[/CODE] when posting code
If your question was answered please use Thread Tools to mark your thread [RESOLVED]