Results 1 to 21 of 21

Thread: An overly-specific question concerning INI's. *RESOLVED*

  1. #1

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628

    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.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    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.

  3. #3

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    Yes, I kind of assumed that. But how would I do it?

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    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.

  5. #5

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    Well, I still need the answer but I haven't gotten it yet. Could anybody help me out?

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Vanguard-MnC

    I don't understand, why hasn't any of this helped?

  7. #7
    Fanatic Member laserman's Avatar
    Join Date
    Jan 2002
    Location
    Wales U.K
    Posts
    775
    This will start it for you

    make sure your text file is in the same directory as your project

    VB Code:
    1. 'Declarations section
    2.  
    3. 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
    4.  
    5. Private Sub Command1_Click()
    6. Dim Ret As String
    7.     Dim NC As Long
    8.    
    9.     Ret = String(255, 0)
    10.     'Retrieve the string
    11.     NC = GetPrivateProfileString("UserScores", "Vanguard ", "", Ret, 255, App.Path & "\scores.txt")
    12.    
    13.    'NC is the number of characters copied to the buffer
    14.     If NC <> 0 Then Ret = Left$(Ret, NC)
    15.  
    16.    
    17.    List1.AddItem Ret
    18.  
    19.     End Sub

  8. #8
    Fanatic Member laserman's Avatar
    Join Date
    Jan 2002
    Location
    Wales U.K
    Posts
    775
    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

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    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:
    1. 'Example by Robin ([email protected])
    2. 'Visit his site at [url]http://members.fortunecity.com/rbnwares1[/url]
    3. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    4. 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
    5. Private Sub Form_Load()
    6.     ' We will create a new section in the INI-file
    7.     ' It's output will be:
    8.     '
    9.     ' [SectionName]
    10.     ' Key=Value
    11.     '
    12.     ' Note that used this ONLY if you are creating a new
    13.     ' section on the INI file, unless you wanted to erase
    14.     ' its existing keys.
    15.     Call WritePrivateProfileSection("SectionName", "Key=Value", App.Path & "\sample.ini")
    16.     Dim szBuf As String * 255
    17.     Call GetPrivateProfileSection("SectionName", szBuf, 255, App.Path & "\sample.ini")
    18.     MsgBox szBuf
    19. End Sub
    Last edited by randem; Jan 26th, 2004 at 08:05 PM.

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Try this

    VB Code:
    1. Option Explicit
    2. 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
    3.  
    4. Private Sub Form_Load()
    5.    
    6.     Dim strBuf As String * 255
    7.     Dim strScores() As String
    8.     Dim strPlayer() As String
    9.     Dim lngIndex As Long
    10.    
    11.     'Get the data in the section
    12.     strBuf = String$(255, 0)
    13.     Call GetPrivateProfileSection("UserScores", strBuf, 255, App.Path & "\test.ini")
    14.     strBuf = Left$(strBuf, Len(strBuf))
    15.     'Split it up
    16.     strScores = Split(strBuf, vbNullChar)
    17.    
    18.     ' Load the scores into an invisible listbox
    19.     For lngIndex = 1 To UBound(strScores)
    20.         ' Split the score from the player name
    21.         strPlayer = Split(strScores(lngIndex), "=")
    22.         ' There are trailing nulls in the file, so ignore them
    23.         If UBound(strPlayer) = 1 Then
    24.             List1.AddItem Format$(strPlayer(1), "00000")
    25.         End If
    26.     Next
    27.    
    28.     MsgBox "Highest score is " & List1.List(List1.ListCount - 1)

  11. #11

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    Ah, thanks. I got it with Martin's code. I knew everything I needed to do except getting all the keys. Thanks again!

  12. #12

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    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?

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Dim strBuf As String * 255
    2.     Dim strScores() As String
    3.     Dim strPlayer() As String
    4.     Dim lngIndex As Long
    5.    
    6.     'Get the data in the section
    7.     strBuf = String$(255, 0)
    8.     Call GetPrivateProfileSection("UserScores", strBuf, 255, App.Path & "\test.ini")
    9.     strBuf = Left$(strBuf, Len(strBuf))
    10.     'Split it up
    11.     strScores = Split(strBuf, vbNullChar)
    12.    
    13.     ' Load the scores into an invisible listbox
    14.     For lngIndex = 1 To UBound(strScores)
    15.         ' Split the score from the player name
    16.         strPlayer = Split(strScores(lngIndex), "=")
    17.         ' There are trailing nulls in the file, so ignore them
    18.         If UBound(strPlayer) = 1 Then
    19.             List1.AddItem Format$(strPlayer(1), "00000") & "::" & strPlayer(0)
    20.         End If
    21.     Next
    22.    
    23.     ' this reuses the strPlarer string
    24.     strPlayer = Split(List1.List(List1.ListCount - 1), "::")
    25.     MsgBox "Highest score is " & strPlayer(0) & " his name is " & strPlayer(1)

  14. #14

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    Ah, thanks. You're such a great guy.

  15. #15

  16. #16
    Addicted Member Beengie's Avatar
    Join Date
    Nov 2003
    Location
    Central Valley, CA
    Posts
    243
    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!

  17. #17
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Beengie

    To make a string of 255 chr$(0).

  18. #18

  19. #19
    Addicted Member Beengie's Avatar
    Join Date
    Nov 2003
    Location
    Central Valley, CA
    Posts
    243
    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!

  20. #20
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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.

  21. #21
    Addicted Member Beengie's Avatar
    Join Date
    Nov 2003
    Location
    Central Valley, CA
    Posts
    243
    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
  •  



Click Here to Expand Forum to Full Width