View previous topic :: View next topic |
Author |
Message |
Blader I post too much
Reputation: 2
Joined: 19 Jan 2007 Posts: 2049
|
Posted: Thu Jan 03, 2008 4:45 pm Post subject: [VB6] Reading Certain Parts of Text Files |
|
|
I already know how to read text files and save them, but how would you read a certain part of it and put that somewhere like a list?
If I had a file with this format:
Name-Homepage
Such as
Blader-cheatengine.org
Blader2-cheatengine2.org
How would I read the Blader, write it to a list, then read the cheatengine.org and save it to another list?
How would you save it like that too?
Thanks
_________________
|
|
Back to top |
|
 |
dnsi0 I post too much
Reputation: 0
Joined: 04 Jan 2007 Posts: 2674
|
Posted: Thu Jan 03, 2008 5:28 pm Post subject: |
|
|
input #1, temp
asd=instr("Blader")
list1.additem asd
asd=instr("www.cheatengine.org")
list2.additem asd
|
|
Back to top |
|
 |
Blader I post too much
Reputation: 2
Joined: 19 Jan 2007 Posts: 2049
|
Posted: Thu Jan 03, 2008 5:44 pm Post subject: |
|
|
That was just an example, I want to read all lines and seperate it like that
I wouldn't know if it was Blader, since people are going to edit the file...
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Jan 04, 2008 5:57 am Post subject: |
|
|
You can read each line and add it to a list like this:
Code: | Private Sub Command1_Click()
Dim strFilePath As String
Dim strLineText As String
Dim dwFile As Long
strFilePath = App.Path & "/stuff.txt"
dwFile = FreeFile()
Open strFilePath For Input As #dwFile
Do While Not EOF(dwFile)
Input #dwFile, strLineText
List1.AddItem strLineText
Loop
Close #dwFile
End Sub |
If you want to search the file for a certain name and only add that you can do something like:
Code: | Private Sub Command1_Click()
LocateName "Name1"
End Sub
Private Function LocateName(strName As String)
Dim strFilePath As String
Dim strLineText As String
Dim dwFile As Long
strFilePath = App.Path & "/stuff.txt"
dwFile = FreeFile()
Open strFilePath For Input As #dwFile
Do While Not EOF(dwFile)
Input #dwFile, strLineText
If Split(strLineText, "-")(0) = strName Then
List1.AddItem strLineText
End If
Loop
Close #dwFile
End Function |
My test file "stuff.txt" was this:
Code: | Name0-www.site.com/0
Name1-www.site.com/1
Name2-www.site.com/2
Name3-www.site.com/3
Name4-www.site.com/4
Name5-www.site.com/5
Name6-www.site.com/6
Name7-www.site.com/7
Name8-www.site.com/8
Name9-www.site.com/9p |
The line of code:
Code: | If Split(strLineText, "-")(0) = strName Then |
Will split the read line at the first instance of - and only keep the text before it. Mind you this will not be a secure method if a user uses the hyphen in their name. It will cause the function to mess up. I suggest looking for a better method to do this, such as storing your data in a different method instead of a flat text file. Perhaps XML. You can do this a lot more secure in XML.
_________________
- Retired. |
|
Back to top |
|
 |
Trow Grandmaster Cheater
Reputation: 2
Joined: 17 Aug 2006 Posts: 957
|
Posted: Fri Jan 04, 2008 6:46 am Post subject: |
|
|
how do you go about using XML in VB6? MSXML wrapper?
_________________
Get kidnapped often. |
|
Back to top |
|
 |
Blader I post too much
Reputation: 2
Joined: 19 Jan 2007 Posts: 2049
|
Posted: Fri Jan 04, 2008 9:42 am Post subject: |
|
|
@Wiccaan - Where would the split go?
I tried this:
Code: | Dim strFilePath As String
Dim strLineText As String
Dim dwFile As Long
Dim strName As String
strFilePath = App.Path & "\Test.txt"
dwFile = FreeFile()
Open strFilePath For Input As #dwFile
Do While Not EOF(dwFile)
Input #dwFile, strLineText
If Split(strLineText, "-")(0) = strName Then
List1.AddItem strName
End If
Loop
Close #dwFile |
But it doesn't work.
Also, how would I split both?
I need to have the name on one list, and the website on another one
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Jan 04, 2008 6:33 pm Post subject: |
|
|
blland wrote: | how do you go about using XML in VB6? MSXML wrapper? |
The UFH engine I wrote uses XML parsing for the "plugins" which you can find the source of here:
http://home.comcast.net/~wiccaan/UFH/UFH_1_0_61.rar
@Blader: The code I posted should work, I tested it before posting it and it worked fine for me. As for getting the website also, just use something like this:
Code: | If Split(strLineText, "-")(0) = strName Then
List1.AddItem Split(strLineText, "-")(0) '// Adds Name To List 1
List2.AddItem Split(strLineText, "-")(1) '// Adds Site To List 2
End If |
_________________
- Retired. |
|
Back to top |
|
 |
Blader I post too much
Reputation: 2
Joined: 19 Jan 2007 Posts: 2049
|
Posted: Fri Jan 04, 2008 7:36 pm Post subject: |
|
|
For yours, you said it only works when you want to look for something specific, I'm not, I'm just adding all of them. The first one doesn't have a split, so that's why I don't know what to do to make it work (Not good at parsing stuff)
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Jan 04, 2008 10:32 pm Post subject: |
|
|
Blader wrote: | For yours, you said it only works when you want to look for something specific, I'm not, I'm just adding all of them. The first one doesn't have a split, so that's why I don't know what to do to make it work (Not good at parsing stuff) |
Possibly a case issue? Try this in the if-then instead:
Code: | If LCase(Split(strLineText, "-")(0)) = LCase(strName) Then |
Again, I don't suggest you use this method to store data, I highly suggest moving into XML or another form of data storage that can prevent parsing issues such as what you are having now. Having a set base skeleton via XML keeps things easy to manage and parse. For example:
Code: | <Entry>
<Name>Site Name</Name>
<Site>Site URL</Site>
</Entry> |
This way you could have any character you want in the name and such and as long as the XML parser is written well and correctly, it should work fine. VB comes with an XML parser extension (msxml6.dll) which works great for this. See the above link to the UFH engine I wrote which makes use of XML files. It should push you in the right direction on using them instead of a normal text file.
_________________
- Retired. |
|
Back to top |
|
 |
Blader I post too much
Reputation: 2
Joined: 19 Jan 2007 Posts: 2049
|
Posted: Sat Jan 05, 2008 10:05 am Post subject: |
|
|
Hmm, I might consider changing into XML parsing...
But, will users be able to edit it?
I might go into .ini instead, is that a little easier than text files, or is it pretty much the same
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Jan 05, 2008 1:00 pm Post subject: |
|
|
Blader wrote: | Hmm, I might consider changing into XML parsing...
But, will users be able to edit it?
I might go into .ini instead, is that a little easier than text files, or is it pretty much the same |
A user can edit any file you give them. No matter the format. Ini files could be another method. Theres plenty of different file types you could use, I just suggest XML as it's more of a standard to holding information now-adays.
_________________
- Retired. |
|
Back to top |
|
 |
|