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
pietlinden@hotmail.com - 26 Sep 2007 19:30 GMT
> 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
[quoted text clipped - 3 lines]
>
> Daniel P
Use the Mid$ function to break the file into 132-character chunks.
Marshall Barton - 26 Sep 2007 20:27 GMT
>I have the following code to read through a text file
>
[quoted text clipped - 27 lines]
>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.

Signature
Marsh
MVP [MS Access]
Stuart McCall - 26 Sep 2007 20:47 GMT
> Hello,
>
[quoted text clipped - 35 lines]
>
> 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.