Results 1 to 11 of 11

Thread: INI vs. TXT

  1. #1

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    INI vs. TXT

    What is the purpose of an ini file? I know what it does and that programs use it to store settings, but why do they use ini files rather than just regular text files? don't you access the data the same way? reading line by line? what are the pro's and con's of an ini file and is it better? is there a certain format that must be followed? thanks,
    - gabe

  2. #2
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    There is a format that you should follow, but it doesn't matter (I think) if it INI or TXT file. The .INI extention stands for 'Initialize'. That way you could easy find this file. Format for INI file is really simple. Something like this:

    [DATA]
    Database=C:\mydb.mdb

    [SCREEN]
    width=100
    height=100


    If you use this format for your program you don't have to read all file line by line. Use GetPrivateProfileString API function, and you will be able to find any variable just by seaching [SCREEN] header, for example, for 'width' variable. That makes it work very fast.

  3. #3

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    API

    You wouldn't happen to know the API string would you? I've never touched API in my life, so I really don't know how to work it.

  4. #4
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  5. #5
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    place this into module:

    VB Code:
    1. Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    2.  
    3. Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    4.  
    5. Function ReadInI(strSectionHeader As String, strVariableName As String, strFileName As String) As String
    6.     Dim strReturn As String
    7.     '// reads INI file
    8.     strReturn = String(255, Chr(0))
    9.     ReadInI = Left$(strReturn, _
    10.     GetPrivateProfileString(strSectionHeader, ByVal strVariableName, "=", strReturn, Len(strReturn), strFileName))
    11. End Function
    12.  
    13. Function WriteINI(strSectionHeader As String, strVariableName As String, strValue As String, strFileName As String) As String
    14.     '// writes to the INI file
    15.     WriteINI = WritePrivateProfileString(strSectionHeader, strVariableName, strValue, strFileName)
    16. End Function

    Place on form
    VB Code:
    1. 'WRITE
    2. Private sub Command1_Click()
    3. Dim ret
    4.  
    5. ret=WriteINI("SCREEN","width","200","C:\start.ini")
    6. End Sub
    7.  
    8. 'READ
    9. Private Sub Command2_Click()
    10. Dim strWidth
    11.  
    12. strWidth=ReadINI("SCREEN","width","C:\start.ini")
    13. MsgBox "Width: " & strWidth
    14. End Sub

  6. #6

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    Thanks

    Thanks, that helped.

  7. #7
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: INI vs. TXT

    I've never used INI files either. Why not just store this information in a DB? What is the real advantage of INI files?

  8. #8
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: INI vs. TXT

    Quote Originally Posted by blakemckenna
    I've never used INI files either. Why not just store this information in a DB? What is the real advantage of INI files?
    With ini or other text based files, you don't need a database. You can carry your settings to another PC. Ini files are usually only used to store an app's settings, not user data. An exception would be, say, to store scores for a game.

    Of interest to some may be a little used or documented API call - WritePrivateProfileStruct - which lets you write your data in the form of a binary (hex) string to an ini file, and additionally adds a checksum to the end of the string. Its companion - GetPrivateProfileStruct - checks this value for file corruption. The only problem is you can't edit it with notepad. As you might expect from the "...Struct", it's intended for UDTs.

    VB Code:
    1. Option Explicit
    2.  
    3. 'Form level code.
    4. 'Just put a 3 command button array (cmdPPStruct) on a form.
    5. 'Note: YOU should add code to check the return values of the calls for errors :-)
    6.  
    7. Private Declare Function WritePrivateProfileStruct Lib "kernel32.dll" Alias "WritePrivateProfileStructA" _
    8.    (ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
    9.    ByVal uSizeStruct As Long, ByVal szFile As String) As Long
    10.  
    11. Private Declare Function GetPrivateProfileStruct Lib "kernel32.dll" Alias "GetPrivateProfileStructA" _
    12.    (ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
    13.    ByVal uSizeStruct As Long, ByVal szFile As String) As Long
    14.  
    15. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" _
    16.    (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    17.  
    18. Private Type MyUDT
    19.    Username As String * 20
    20.    Password As String * 20
    21. End Type
    22.  
    23. Private Sub cmdPPStruct_Click(index As Integer)
    24.    Dim lngRetval As Long
    25.    Dim strUDT As String
    26.    Dim MyNewUDT As MyUDT
    27.    
    28.    Select Case index
    29.    Case 0 'Write it...
    30.      
    31.       MyNewUDT.Username = "MySillyUserName"
    32.       MyNewUDT.Password = "MyStrangePassword"
    33.      
    34.       Call WritePrivateProfileStruct("WritePrivateProfileStruct", "Key", _
    35.          MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
    36.  
    37.    Case 1 'Read it...
    38.  
    39. 'No need to erase MyNewUDT. It's recreated whenever cmdPPStruct is clicked...
    40.      
    41.       Call GetPrivateProfileStruct("WritePrivateProfileStruct", "Key", _
    42.          MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
    43.      
    44.          strUDT = MyNewUDT.Username & vbCrLf & MyNewUDT.Password & vbCrLf
    45.    
    46. 'Display it...
    47.       MsgBox strUDT
    48.    
    49.    Case 2 'Delete the section...
    50.    
    51.       Call WritePrivateProfileSection("WritePrivateProfileStruct", vbNullString, App.Path & "\INITest.ini")
    52.    
    53.    End Select
    54. End Sub
    Open the file with notepad and you'll see:
    VB Code:
    1. [WritePrivateProfileStruct]
    2. Key=4D7953696C6C79557365724E616D6520202020204D79537472616E676550617373776F7264202020E0

    Despite googling for these APIs I couldn't find a single example, so just remember - you saw it on VB Forums first (unless someone knows differently ?)

    -----------------------------------------------------------------

    BTW, I've seen several posts that say there isn't an API to delete entire sections (including the keys and values) from INI files. Not quite true:

    VB Code:
    1. 'Delete a value from the specified key.
    2.   WritePrivateProfileString(Section, Key, "", Filename)
    3.  
    4. 'Delete a key (and value) from the specified section.
    5.   WritePrivateProfileString(Section, Key, vbNullstring, Filename)
    6.  
    7. 'Delete an entire section from an INI file, including all subkeys and values.
    8.   WritePrivateProfileString(Section, vbNullString, vbNullString, Filename)
    9. 'or
    10.   WritePrivateProfileSection(Section, vbNullstring, Filename)
    Last edited by schoolbusdriver; Nov 7th, 2006 at 02:22 PM.

  9. #9
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: INI vs. TXT

    thanks schoolbus....that makes sense now!

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

    Re: INI vs. TXT

    blakemckenna,

    Think of it this way... If you stored your info in a database, how do you tell your program where the database is to retrieve the settings? That is why you need a different medium to store settings.

  11. #11
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: INI vs. TXT

    Another option of storing data is in the Registry, you can use the GetSetting and SaveSetting functions.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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