Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
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? |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
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? |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Reading from a text file | Excel Discussion (Misc queries) | |||
Reading a text file ? | Excel Worksheet Functions | |||
Reading a text file | Excel Discussion (Misc queries) | |||
Help reading in a text file | Excel Programming | |||
Reading .csv file into array | Excel Programming |