|
-
Jan 4th, 2004, 05:45 PM
#1
Thread Starter
Fanatic Member
An overly-specific question concerning INI's. *RESOLVED*
I have an INI file that stores a bunch of usernames and their scores in a game, like this:
Code:
[UserScores]
Vanguard[T]=61
Zmud=17
Reza@Azeroth=2
Wargamer=4
Son.Goten=5
JTSM=10
Elf[V]=24
...and sometime during the program is running, I need to know which user has the highest score. How do I go about doing this? I'll really appreciate it if someone would help me. Thanks.
Last edited by Vanguard-MnC; Jan 26th, 2004 at 10:09 PM.
-
Jan 4th, 2004, 06:06 PM
#2
You should be able to load it with the GetPrivateProfileString API and then you simply need to sort.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Jan 4th, 2004, 06:28 PM
#3
Thread Starter
Fanatic Member
Yes, I kind of assumed that. But how would I do it?
-
Jan 4th, 2004, 06:46 PM
#4
Vanguard-MnC,
If you want to cheat a little. Read each item and put it into a listbox with the sort property set to true.
Make sure that all number are at the front and are the same length with leading zeros.
-
Jan 26th, 2004, 06:09 PM
#5
Thread Starter
Fanatic Member
Well, I still need the answer but I haven't gotten it yet. Could anybody help me out?
-
Jan 26th, 2004, 06:36 PM
#6
Vanguard-MnC
I don't understand, why hasn't any of this helped?
-
Jan 26th, 2004, 07:23 PM
#7
Fanatic Member
This will start it for you
make sure your text file is in the same directory as your project
VB Code:
'Declarations section
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Sub Command1_Click()
Dim Ret As String
Dim NC As Long
Ret = String(255, 0)
'Retrieve the string
NC = GetPrivateProfileString("UserScores", "Vanguard ", "", Ret, 255, App.Path & "\scores.txt")
'NC is the number of characters copied to the buffer
If NC <> 0 Then Ret = Left$(Ret, NC)
List1.AddItem Ret
End Sub
-
Jan 26th, 2004, 07:27 PM
#8
Fanatic Member
I have called the ini file scores.txt change this to score.ini or whatever your inifile is called in the code i have posted.
cheers
-
Jan 26th, 2004, 07:29 PM
#9
Vanguard-MnC
What you want to do is to read the whole section not just the items. I assume you may not readily know the individual items, correct? Then you can separate them out and sort them. Here is some help from the people at AllAPI.
VB Code:
'Visit his site at [url]http://members.fortunecity.com/rbnwares1[/url]
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Sub Form_Load()
' We will create a new section in the INI-file
' It's output will be:
'
' [SectionName]
' Key=Value
'
' Note that used this ONLY if you are creating a new
' section on the INI file, unless you wanted to erase
' its existing keys.
Call WritePrivateProfileSection("SectionName", "Key=Value", App.Path & "\sample.ini")
Dim szBuf As String * 255
Call GetPrivateProfileSection("SectionName", szBuf, 255, App.Path & "\sample.ini")
MsgBox szBuf
End Sub
Last edited by randem; Jan 26th, 2004 at 08:05 PM.
-
Jan 26th, 2004, 07:55 PM
#10
Try this
VB Code:
Option Explicit
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Sub Form_Load()
Dim strBuf As String * 255
Dim strScores() As String
Dim strPlayer() As String
Dim lngIndex As Long
'Get the data in the section
strBuf = String$(255, 0)
Call GetPrivateProfileSection("UserScores", strBuf, 255, App.Path & "\test.ini")
strBuf = Left$(strBuf, Len(strBuf))
'Split it up
strScores = Split(strBuf, vbNullChar)
' Load the scores into an invisible listbox
For lngIndex = 1 To UBound(strScores)
' Split the score from the player name
strPlayer = Split(strScores(lngIndex), "=")
' There are trailing nulls in the file, so ignore them
If UBound(strPlayer) = 1 Then
List1.AddItem Format$(strPlayer(1), "00000")
End If
Next
MsgBox "Highest score is " & List1.List(List1.ListCount - 1)
-
Jan 26th, 2004, 08:21 PM
#11
Thread Starter
Fanatic Member
Ah, thanks. I got it with Martin's code. I knew everything I needed to do except getting all the keys. Thanks again!
-
Jan 26th, 2004, 09:59 PM
#12
Thread Starter
Fanatic Member
Hmm... I have another question. Sorry about that. If I wanted to get the user with the hightest score, how would I do that using Martin's code?
-
Jan 26th, 2004, 10:04 PM
#13
VB Code:
Dim strBuf As String * 255
Dim strScores() As String
Dim strPlayer() As String
Dim lngIndex As Long
'Get the data in the section
strBuf = String$(255, 0)
Call GetPrivateProfileSection("UserScores", strBuf, 255, App.Path & "\test.ini")
strBuf = Left$(strBuf, Len(strBuf))
'Split it up
strScores = Split(strBuf, vbNullChar)
' Load the scores into an invisible listbox
For lngIndex = 1 To UBound(strScores)
' Split the score from the player name
strPlayer = Split(strScores(lngIndex), "=")
' There are trailing nulls in the file, so ignore them
If UBound(strPlayer) = 1 Then
List1.AddItem Format$(strPlayer(1), "00000") & "::" & strPlayer(0)
End If
Next
' this reuses the strPlarer string
strPlayer = Split(List1.List(List1.ListCount - 1), "::")
MsgBox "Highest score is " & strPlayer(0) & " his name is " & strPlayer(1)
-
Jan 26th, 2004, 10:09 PM
#14
Thread Starter
Fanatic Member
Ah, thanks. You're such a great guy.
-
Jan 26th, 2004, 10:10 PM
#15
-
Jan 29th, 2004, 01:44 AM
#16
Addicted Member
I was wondering what...
String$(255, 0)
means
Also, is the 255 in the GetPrivateProfileSection function is the maximum size of the string to be returned?
I am having a hard time finding the answers to my ini queries
BeengieHappy.Vaue = (SharksScore > OpponentsScore)
Go Sharks!
-
Jan 29th, 2004, 01:55 AM
#17
Beengie
To make a string of 255 chr$(0).
-
Jan 29th, 2004, 11:08 AM
#18
Originally posted by Beengie
...Also, is the 255 in the GetPrivateProfileSection function is the maximum size of the string to be returned?...
Yes, but you can make it larger.
-
Jan 29th, 2004, 11:43 AM
#19
Addicted Member
When it makes the string of 255 chr$(0) for strBuf, is that stating the valid characters to be returned from the ini?
and...
How is the array structured in strBuf after the GetPrivateProfileSection function is run?
I found in MSDN that it was a single dimension array
does that mean that it would be like this?
[SectionName]
Key1=1
Key2=2
Key3=3
Key4=4
strBuf(0)=Key1
strBuf(1)=1
strBuf(2)=Key2
strBuf(3)=2
strBuf(4)=Key3
strBuf(5)=3
strBuf(6)=Key4
strBuf(7)=4
BeengieHappy.Vaue = (SharksScore > OpponentsScore)
Go Sharks!
-
Jan 29th, 2004, 01:15 PM
#20
String$(255, 0) just defines a buffer to hold the data.
The data in the buffer can be cansidered a string with the data in the string separated by nulls (vbNullChar). If you want to see what the string looks like put a breakpoint on the strScores = Split(strBuf, vbNullChar) line in my code and look at strBuf.
-
Jan 29th, 2004, 06:36 PM
#21
Addicted Member
Thank you MartinLiss and randem
BeengieHappy.Vaue = (SharksScore > OpponentsScore)
Go Sharks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|