Results 1 to 12 of 12

Thread: Ini File

  1. #1
    shuj66
    Guest

    Ini File

    Hi,
    I have an ini file in the following format

    [DataToRead]
    1=1000
    2=2000
    3=3000
    x=4000
    etc

    I also have an array called arraydatareadini

    How do I read the data from the ini file and store it in the above array.

    Also, the amount of data is not fixed - ie more could be added so x is changes.

    Thank you.

  2. #2
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    Here you go....

    If you're storing off user settings, I prefer to access the registry, due to it's simplicity. But I whipped up the following funtion that should work for you:

    VB Code:
    1. Private Sub Form_Load()
    2.     GetIni "C:\myini.ini"
    3. End Sub
    4.  
    5. Private Sub GetIni(FileString As String)
    6.     Dim TextString As String
    7.     Dim arraydatareadini() As Integer
    8.     Dim IndexCount As Integer
    9.     Dim EqualPos As Integer
    10.  
    11.     Open FileString For Input As #1
    12.         Do While Not EOF(1)
    13.             Line Input #1, TextString
    14.             If InStr(1, TextString, "[DataToRead]") > 0 Then 'find ini key
    15.                 Do While TextString <> "" And Not EOF(1) 'check for no more values or end of file
    16.                     Line Input #1, TextString 'get the value
    17.                     TextString = Trim(TextString) 'trim spaces off string
    18.                     IndexCount = IndexCount + 1 'increment counter
    19.                     ReDim Preserve arraydatareadini(IndexCount) 'redim array by one
    20.                     EqualPos = InStr(1, TextString, "=") 'find where the = is in the string
    21.                    
    22.                     'start at one past the equal, extract the value, convert to an integer, and assign to array
    23.                     arraydatareadini(IndexCount) = CInt(Mid(TextString, EqualPos + 1, Len(TextString) - EqualPos))
    24.                 Loop
    25.             End If
    26.         Loop
    27.     Close #1
    28.    
    29.     'Print the contents of your new array
    30.     For x = 1 To UBound(arraydatareadini)
    31.         Debug.Print arraydatareadini(x)
    32.     Next
    33. End Sub

    good luck
    If you can think it....you can code it....

  3. #3
    shuj66
    Guest
    Thanks Sibby,

    That was a big help.

    I have another issue with this same project that has to do with enter new data into this same ini file with the use of a combo box.

    Could you sent me your email, then perhaps I could email you what I have so far.

    Thanks

  4. #4
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    Sure...

    You could upload it (zip file) here and I could take a look at it...that way others later on can solve thier problem similar to yours by following your progress....

    If not, you could email me at [email protected]. I may not have time today, depending on how involved your questions/problems are....but I will respond.....

    Glad I could help
    If you can think it....you can code it....

  5. #5
    shuj66
    Guest
    Right now, the code I have below just display all the number in one line ie

    55550,5567,5677,5667,5677, etc

    How do I get it to display each one individually ie
    55550
    5567
    5677 etc

    Also, I need to get rid of the comma at the end of the list

    I am getting this info from a variable called getdataini which grabbed the data from an ini file.

    Any Suggestions

    My code:

    Private Sub Form_Load()
    Dim icount As Integer
    Dim y As Integer
    Dim X As Integer
    Dim group As Integer
    Call GetValues
    Dim comboCount As Integer

    icount = CInt(Len(arraygetdata) / 4)
    y = 1
    For Xcount = 1 To icount
    ComboBox.AddItem Mid(arraygetdata, 6)
    ComboBox.AddItem arraygetdata
    y = y + 5
    Next X

    If (txtValueToAdd.Text <> "") Then
    cmdAdd.Enabled = True
    cmdDelete.Enabled = True
    Else
    cmdAdd.Enabled = False
    cmdDelete.Enabled = False
    End If

    End Sub

  6. #6
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    :)

    I've wrote a function that I use all the time for parsing CSV (comma separated value) files. Unfortunatly it's it work, and I am not. I'll be sure to post that when I get in tomorrow morning.

    So in the meantime, work on another part of your app and I'll help you ASAP.

    ::Sibby::
    If you can think it....you can code it....

  7. #7
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    Thumbs up As promised...

    This function should work for you. Depending on your program, you could hardcode the delimiter if all your files use commas. This function will work whether you have a comma at the end of your string or not.

    VB Code:
    1. Public Sub ParseFile(FileName As String, Delimiter As String)
    2. ' **************************************************************
    3. ' * Programmer Name : Steve (Sibby) Komoll
    4. ' * Date            : 5/12/01
    5. ' **************************************************************
    6. ' * Comments         : Parses any separated values file
    7. ' *
    8. ' *
    9. ' **************************************************************
    10.    
    11.     Dim SingleLine As String
    12.     Dim StartPos As Integer
    13.     Dim EndPos As Integer
    14.     Dim ParsedString As String
    15.    
    16.     Open FileName For Input As #1
    17.         While Not EOF(1)
    18.             StartPos = 1 'initialize the starting position
    19.             Line Input #1, SingleLine
    20.             Do While StartPos <= Len(SingleLine)
    21.                 'find the delimiter position
    22.                 EndPos = InStr(StartPos, SingleLine, Delimiter)
    23.                 'if delimiter is not at end of string, move end position past string length
    24.                 If EndPos = 0 Then EndPos = Len(SingleLine) + 1
    25.                 'get the value string
    26.                 ParsedString = Mid(SingleLine, StartPos, EndPos - StartPos)
    27.                 Debug.Print ParsedString ' <== do what you need with the value here
    28.                 'set the next starting position 1 past the current delimiter
    29.                 StartPos = EndPos + 1
    30.             Loop
    31.         Wend
    32.     Close #1
    33. End Sub

    Good luck man! Lemme know how it turns out!
    If you can think it....you can code it....

  8. #8
    shuj66
    Guest
    Thanks Sibby,

    Actually the numbers are stored in a variable say called datatoread.

    So, right now I am trying to load these numbers in a drop down combo box. since this variable has it listed as
    xxxxx,xxxx,xxxx,xxxxx,xxxx, with the code I have it is all apearing on the same line. However I need it to display each number individually. Also, some of them are five digits while others are four. So, I need it to seperate it by the commas.

    How would I incorporate the code you sent me with the code I have.

    Thanks again sibby.

  9. #9
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144
    OK, just think about it logically. Instead of reading from a file, I substituted you array. In the example below, I'm filling it manually, but your will already be filled. Also, the delimiter is now hardcoded as a comma.

    Unless you are using the "DataToRead" array in multiple places, it would be more efficient to read the file, then load it to your listbox, as in the first example I gave. Right now you are reading it from a file, then loading to an array, then loading to the listbox. One step you could do without to speed up your process. But again, I don't know how your program works and maybe you're using that array somewhere else before you are processing it here....

    VB Code:
    1. Dim DataToRead(1) As String
    2.  
    3. Private Sub Form_Load()
    4.     DataToRead(0) = "1000,20000,3000,40000,50000,"
    5.     DataToRead(1) = "60000,7000,80000,9000,1000,"
    6.     ParseFile
    7. End Sub
    8.  
    9. Public Sub ParseFile()
    10.    
    11.     Dim SingleLine As String
    12.     Dim StartPos As Integer
    13.     Dim EndPos As Integer
    14.     Dim ParsedString As String
    15.    
    16.     For x = 0 To UBound(DataToRead)
    17.         StartPos = 1 'initialize the starting position
    18.         SingleLine = StringArray(x)
    19.         Do While StartPos <= Len(SingleLine)
    20.             'find the delimiter position
    21.             EndPos = InStr(StartPos, SingleLine, ",")
    22.             'if delimiter is not at end of string, move end position past string length
    23.             If EndPos = 0 Then EndPos = Len(SingleLine) + 1
    24.             'get the value string
    25.             ParsedString = Mid(SingleLine, StartPos, EndPos - StartPos)
    26.             Combo1.AddItem ParsedString '<== add the items to your combobox here
    27.             'set the next starting position 1 past the current delimiter
    28.             StartPos = EndPos + 1
    29.         Loop
    30.     Next
    31. End Sub
    If you can think it....you can code it....

  10. #10
    Addicted Member flavorjatin's Avatar
    Join Date
    Sep 2001
    Location
    India
    Posts
    154
    hi,

    You can use API's for this purpose.
    READPRIVATEPROFILESTRING
    writePRIVATEPROFILESTRING

    After reading the data by first API (especially for this purpose), you can manipulate according to your requirements.

    Byeeeee.
    Best Regards.

  11. #11
    shuj66
    Guest
    Thanks Sibby,

    You really helped out a lot. I will call on you again soon.

  12. #12
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144
    No problem shuj66,

    I've got alot of help off of this forum in the past, just glad I could give something back....
    If you can think it....you can code it....

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