Parse text file

G

Guest

Hello,

I have the following code to read through a text file

Dim FileNumber As Integer
Dim sFile As String

FileNumber = FreeFile
Open strFile For Input As #FileNumber 'open the file

Dim strLine As String
Do Until EOF(FileNumber) 'loop through each line in the
file
Line Input #FileNumber, strLine 'store the line as a variable
ReadTxt = ReadTxt & strLine
Loop
Close #FileNumber 'close the file

Then I do the following

arrstrTxt = Split(ReadTxt, vbKeyReturn)

For i = LBound(arrstrTxt) To UBound(arrstrTxt)
Debug.Print "Line: " & i & vbCrLf & arrstrTxt(i)
Next i

The problem being that it only returns 21 lines when in fact, when
viewed/printed it actually has 157. I have tried variation in the split
function (chr(10), chr(13), vbcrlf,...) to no avail.

I analyzed the file a realized that each line has 132 characters. Is there
a way to read through a file 132 characters at a time? or any mistakes with
my prior code that would explain my problem?

Thank you for your help,

Daniel P
 
P

pietlinden

I analyzed the file a realized that each line has 132 characters. Is there
a way to read through a file 132 characters at a time? or any mistakes with
my prior code that would explain my problem?

Thank you for your help,

Daniel P


Use the Mid$ function to break the file into 132-character chunks.
 
M

Marshall Barton

Daniel said:
I have the following code to read through a text file

Dim FileNumber As Integer
Dim sFile As String

FileNumber = FreeFile
Open strFile For Input As #FileNumber 'open the file

Dim strLine As String
Do Until EOF(FileNumber) 'loop through each line in the
file
Line Input #FileNumber, strLine 'store the line as a variable
ReadTxt = ReadTxt & strLine
Loop
Close #FileNumber 'close the file

Then I do the following

arrstrTxt = Split(ReadTxt, vbKeyReturn)

For i = LBound(arrstrTxt) To UBound(arrstrTxt)
Debug.Print "Line: " & i & vbCrLf & arrstrTxt(i)
Next i

The problem being that it only returns 21 lines when in fact, when
viewed/printed it actually has 157. I have tried variation in the split
function (chr(10), chr(13), vbcrlf,...) to no avail.

I analyzed the file a realized that each line has 132 characters. Is there
a way to read through a file 132 characters at a time? or any mistakes with
my prior code that would explain my problem?


You should explore using the Get statement instead of Line
Input.
 
S

Stuart McCall

Daniel said:
Hello,

I have the following code to read through a text file

Dim FileNumber As Integer
Dim sFile As String

FileNumber = FreeFile
Open strFile For Input As #FileNumber 'open the file

Dim strLine As String
Do Until EOF(FileNumber) 'loop through each line in the
file
Line Input #FileNumber, strLine 'store the line as a variable
ReadTxt = ReadTxt & strLine
Loop
Close #FileNumber 'close the file

Then I do the following

arrstrTxt = Split(ReadTxt, vbKeyReturn)

For i = LBound(arrstrTxt) To UBound(arrstrTxt)
Debug.Print "Line: " & i & vbCrLf & arrstrTxt(i)
Next i

The problem being that it only returns 21 lines when in fact, when
viewed/printed it actually has 157. I have tried variation in the split
function (chr(10), chr(13), vbcrlf,...) to no avail.

I analyzed the file a realized that each line has 132 characters. Is
there
a way to read through a file 132 characters at a time? or any mistakes
with
my prior code that would explain my problem?

Thank you for your help,

Daniel P

First of all you can ditch all that time-wasting Line Input stuff and grab
the whole file in one go:

Dim FileNumber As Integer
Dim sFile As String

FileNumber = FreeFile
Open strFile For Binary Access Read As FileNumber
sFile = Space(LOF(FileNumber))
Get #FileNumber, , sFile
Close FileNumber

Now you have the entire contents of the file in sFile, which means you can
process it all in RAM (much faster). Not only that but the embedded
end-of-line characters are still there, whereas Line Input strips them out.

Now if the file was created under Windows, the EOL chars will be vbCrLf.
However if it was created under UNIX, they will be vbCr (no linefeed). Lets
assume its a Windows file. Now you can use the Split function to get each
"line" into an array:

arrstrTxt = Split(sFile, vbCrLf)

I can see you already know how to loop over the array, so I'll leave it
there. Let me (us) know if you need further help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top