Results 1 to 12 of 12

Thread: Reading .INI files

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60

    Reading .INI files

    Doers anybody know of a way to extract just the section headers from a .ini file which is in the format:

    [section header 1]
    data=0
    data=9

    [section header 2]
    data=8
    data=5

    thank You

  2. #2
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Reading .INI files

    Use the ReadPrivateProfileString() API.

  3. #3
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Reading .INI files

    Quote Originally Posted by R.a.B.B.i.T
    Use the ReadPrivateProfileString() API.

    Actually I think it is called GetPrivateProfileString()


    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  4. #4
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Reading .INI files

    Ah, ok. I dodn't have API-guide on this computer, so I wasn't sure; but either way, use that...

  5. #5
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Reading .INI files

    Quote Originally Posted by R.a.B.B.i.T
    Ah, ok. I dodn't have API-guide on this computer, so I wasn't sure; but either way, use that...

    Really? In that case here is a good one to have a look at

    ftp://ftp.cs.virginia.edu/pub/lcc-win32/win32hlp.exe

    Quite big at 12.8 Megabytes though, but worth it (Also, check out MSDN too)

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  6. #6
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Reading .INI files

    i dont like how msdn uses examples..to complex for such easy things!

  7. #7
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Reading .INI files

    Quote Originally Posted by sciguyryan
    Really? In that case here is a good one to have a look at

    ftp://ftp.cs.virginia.edu/pub/lcc-win32/win32hlp.exe

    Quite big at 12.8 Megabytes though, but worth it (Also, check out MSDN too)

    Cheers,

    RyanJ
    MSDN confirmed that GetPrivateProfileString() is an API call, so that's what I went with. I also prefer allapi.net's API-Guide.

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60

    Re: Reading .INI files

    Thanks for the help. I've looked at the api-guide and I have decided to try using GetPrivateProfileSectionNames(). It seems to work but I have another question which I think is probably quite funamental in C++.

    How do I retrieve each individual null terminated string from the returned buffer containing each section name? My intention is to put them into a listbox.

    Thanks

    Wallace

  9. #9
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Reading .INI files

    I don't have any C++ code for that at the moment, but I have some VB6 code (I really don't feel like porting) if you want.

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60

    Re: Reading .INI files

    yeah the VB code would be good thanks, might give me a shove in the right direction!

    Wallace

  11. #11
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Reading .INI files

    VB Code:
    1. Public Function GetStuff(sFile As String, appname As String, Key As String) As String
    2.     Dim sDefault As String
    3.     Dim lSize As Integer
    4.     Dim l As Long
    5.     Dim sUser As String
    6.    
    7.     On Error GoTo GetStuff_Error
    8.    
    9.     sUser = Space$(128)
    10.     lSize = Len(sUser)
    11.     sFile = App.Path & "\" & sFile & ".ini"
    12.     sDefault = ""
    13.     l = GetPrivateProfileString(appname, Key, sDefault, sUser, lSize, sFile)
    14.     sUser = Mid(sUser, 1, InStr(sUser, Chr(0)) - 1)
    15.     GetStuff = sUser
    16.    
    17.     On Error GoTo 0
    18.     Exit Function
    19.    
    20. GetStuff_Error:
    21.     AddC vbRed, "GetStuff() Call Error:" & _
    22.          vbNewLine & "Error: " & Err.Number & _
    23.          vbNewLine & "Description: " & Err.Description & _
    24.          vbNewLine & "Source: " & Err.Source
    25. End Function

    Use:
    VB Code:
    1. Dim k As String
    2. k = GetStuff("somefile", "someheader", "somefield")

    AddC() is a function I wrote to add text to a RTB, so just ignore that bit.

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2004
    Posts
    60

    Re: Reading .INI files

    Thank you very much for the help. I'll let you know how i get on

    wallace

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