naallen
asked on
Reading Text files
I have a text file that look like this:
---begin file---------------------- ------
"text:Home" "0" "" "LINK:http://www.bae.ksu.edu/kelp/initialdoc.html,main" "Return to Homepage"
"separator:" "0" "" "" ""
"text:Training Details" "0" "" ""
"text:Training Overview" "2" "" "LINK:http://www.bae.ksu.edu/kelp/kelpinformation.htm,main"
"text:Training Application" "2" "" "LINK:http://www.bae.ksu.edu/kelp/app.html,main" "Electronic Application Form"
"TEXT:Training Schedule" "2" "" "LINK:http://www.bae.ksu.edu/kelp/classSchd.html,main" "View the Training Schedule"
----------End file--------
Here is the syntax of what you are looking at:
"Text:[Label]", "[placement]", "", "link:[Link],[target],"[hi nt]"
I need these paramaters separated as I read the text file and put into a ini file (I will supply the ini code), but I just need to know how to read the file so I can seperate it out.
Then I will list the item in a treeview control like this:
Level 0
Level 2
Does anyone have anyone have an idea how to do this? Is there a way to possibly use a form Java String Delimters? I have no idea. Also text file size may be a problem if anyone can address this issue.
---begin file----------------------
"text:Home" "0" "" "LINK:http://www.bae.ksu.edu/kelp/initialdoc.html,main" "Return to Homepage"
"separator:" "0" "" "" ""
"text:Training Details" "0" "" ""
"text:Training Overview" "2" "" "LINK:http://www.bae.ksu.edu/kelp/kelpinformation.htm,main"
"text:Training Application" "2" "" "LINK:http://www.bae.ksu.edu/kelp/app.html,main" "Electronic Application Form"
"TEXT:Training Schedule" "2" "" "LINK:http://www.bae.ksu.edu/kelp/classSchd.html,main" "View the Training Schedule"
----------End file--------
Here is the syntax of what you are looking at:
"Text:[Label]", "[placement]", "", "link:[Link],[target],"[hi
I need these paramaters separated as I read the text file and put into a ini file (I will supply the ini code), but I just need to know how to read the file so I can seperate it out.
Then I will list the item in a treeview control like this:
Level 0
Level 2
Does anyone have anyone have an idea how to do this? Is there a way to possibly use a form Java String Delimters? I have no idea. Also text file size may be a problem if anyone can address this issue.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Looks good to me. I'd completely forgotten about the line input # statement, and went and reinvented the wheel the other month. Hmmm.. Time for a re-write. ;)
Here's a nice little Parser for you, naallen:
Function GetWord(ByVal str As String, num As Integer)
' This gets a word. A word is either a single word seperated by spaces on both sides,
' or a collection of words inside quote marks.
' '"Hello fred"' counts as one word.
' 'th"ere"' counts as two. 'th' and 'ere'.
' 'word' counts as one word.
' Count your quotes carefully, as mistakes cannot be remedied once you have left the shop.
' You'll notice that there's no error catching. This is because there will be no errors.
' This code is 100% error free. Any changes made to it, however, may cause it to crash,
' burn and die horribly in an accident which would make Hiroshima look like a tea party.
Const quote = """"
Const char = " "
Dim aChar As String
Dim nextSpace As Long
Dim nextQuote As Long
Dim i As Integer
Dim aStart As Long
Dim aStop As Long
Dim newStart As Long
For i = 1 To num
aStart = newStart
'GetStartofnextword
Do
aStart = aStart + 1
aChar = Mid(str, aStart, 1)
Loop While aChar = char
If aChar = quote Then
aStart = aStart + 1
nextQuote = InStr(aStart, str, quote)
aStop = nextQuote - 1
newStart = nextQuote + 1
Else
nextSpace = InStr(aStart, str, char)
nextQuote = InStr(aStart, str, quote)
If nextQuote = 0 Or nextSpace < nextQuote Then
aStop = nextSpace - 1
Else
aStop = nextQuote - 1
End If
newStart = aStop
End If
If aStop = -1 Then
Exit For
End If
Next i
If num > i Or num < 1 Then
GetWord = Empty
Else
If aStop = -1 Then
GetWord = Mid$(str, aStart)
Else
GetWord = Mid$(str, aStart, aStop - (aStart - 1))
End If
End If
End Function
Function GetWord(ByVal str As String, num As Integer)
' This gets a word. A word is either a single word seperated by spaces on both sides,
' or a collection of words inside quote marks.
' '"Hello fred"' counts as one word.
' 'th"ere"' counts as two. 'th' and 'ere'.
' 'word' counts as one word.
' Count your quotes carefully, as mistakes cannot be remedied once you have left the shop.
' You'll notice that there's no error catching. This is because there will be no errors.
' This code is 100% error free. Any changes made to it, however, may cause it to crash,
' burn and die horribly in an accident which would make Hiroshima look like a tea party.
Const quote = """"
Const char = " "
Dim aChar As String
Dim nextSpace As Long
Dim nextQuote As Long
Dim i As Integer
Dim aStart As Long
Dim aStop As Long
Dim newStart As Long
For i = 1 To num
aStart = newStart
'GetStartofnextword
Do
aStart = aStart + 1
aChar = Mid(str, aStart, 1)
Loop While aChar = char
If aChar = quote Then
aStart = aStart + 1
nextQuote = InStr(aStart, str, quote)
aStop = nextQuote - 1
newStart = nextQuote + 1
Else
nextSpace = InStr(aStart, str, char)
nextQuote = InStr(aStart, str, quote)
If nextQuote = 0 Or nextSpace < nextQuote Then
aStop = nextSpace - 1
Else
aStop = nextQuote - 1
End If
newStart = aStop
End If
If aStop = -1 Then
Exit For
End If
Next i
If num > i Or num < 1 Then
GetWord = Empty
Else
If aStop = -1 Then
GetWord = Mid$(str, aStart)
Else
GetWord = Mid$(str, aStart, aStop - (aStart - 1))
End If
End If
End Function
Why would text file size be a problem?
Can't see it myself.
J.
Can't see it myself.
J.
ASKER
Wouldn't a very large file crash the program, I have heard that large files can do that.
Providing you're not using vb3 or vb4, file size _shouldn't be a problem. If you were reading the whole file into memory before parsing it (which is what I prefer to do), then it might cause a problem if you try to open a file which is too big (and I'm talking 20+Mb files here), but reading it line by line shouldn't cause any problems.
J.
J.