ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reading Text file into an array (https://www.excelbanter.com/excel-programming/387662-reading-text-file-into-array.html)

[email protected]

Reading Text file into an array
 
I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?

TIA
Garry


RB Smissaert

Reading Text file into an array
 
Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.

RBS

wrote in message
oups.com...
I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?

TIA
Garry



Tom Ogilvy

Reading Text file into an array
 
the end of line marker in windows is two characters together. In VBA is is
represented with the constant: vbCrLf

But that would be the same as reading it in a line at a time which you say
you can do.
If you actually want to parse a delimited file, then Chip Pearson has sample
code for that:

http://www.cpearson.com/excel/imptext.htm import/export text files

--
Regards,
Tom Ogilvy




" wrote:

I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?

TIA
Garry



[email protected]

Reading Text file into an array
 
On Apr 18, 11:52 am, "RB Smissaert"
wrote:
Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.

RBS

wrote in message

oups.com...



I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?


TIA
Garry- Hide quoted text -


- Show quoted text -


I'll give that a shot. To load the array how do I locate the end of
the string in which I have placed the file info?


RB Smissaert

Reading Text file into an array
 
Get the file into a string variable with something like this:

Function OpenTextFileToString(strFile As String) As String

Dim hFile As Long

'obtain file handle, open file
'and load into a string buffer
hFile = FreeFile

Open strFile For Input As #hFile

OpenTextFileToString = Input$(LOF(hFile), hFile)

Close #hFile

End Function


Then you can do something like this:

Sub test()

Dim strString As String
Dim arr

strString = OpenTextFileToString("C:\test.txt")

arr = Split(strString, vbCrLf)

End Sub


Replace the vbCrLf with whatever your delimiter is.


RBS




wrote in message
ups.com...
On Apr 18, 11:52 am, "RB Smissaert"
wrote:
Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.

RBS

wrote in message

oups.com...



I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?


TIA
Garry- Hide quoted text -


- Show quoted text -


I'll give that a shot. To load the array how do I locate the end of
the string in which I have placed the file info?



[email protected]

Reading Text file into an array
 
On Apr 18, 1:25 pm, "RB Smissaert"
wrote:
Get the file into a string variable with something like this:

Function OpenTextFileToString(strFile As String) As String

Dim hFile As Long

'obtain file handle, open file
'and load into a string buffer
hFile = FreeFile

Open strFile For Input As #hFile

OpenTextFileToString = Input$(LOF(hFile), hFile)

Close #hFile

End Function

Then you can do something like this:

Sub test()

Dim strString As String
Dim arr

strString = OpenTextFileToString("C:\test.txt")

arr = Split(strString, vbCrLf)

End Sub

Replace the vbCrLf with whatever your delimiter is.

RBS

wrote in message

ups.com...



On Apr 18, 11:52 am, "RB Smissaert"
wrote:
Read your whole text file and put it in a string variable.
Then split the string with the Split function of whatever
delimiter you choose.


RBS


wrote in message


groups.com...


I want to read an ASCII text file that contains variable length lines
into an array. I can read the file a line at a time or a character at
a time but what I really need is to read the text between delimiters
as a unit. Is there an end of line marker (similar to the EOF
marker)? How do I capture the information between delimiters?


TIA
Garry- Hide quoted text -


- Show quoted text -


I'll give that a shot. To load the array how do I locate the end of
the string in which I have placed the file info?- Hide quoted text -


- Show quoted text -


Excellent. I used the tab (Chr(9)) which is the delimiter for this
ASCII file and it worked great. Thanks for the good advice.



All times are GMT +1. The time now is 05:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com