|
-
Sep 27th, 2001, 10:39 AM
#1
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.
-
Sep 27th, 2001, 12:27 PM
#2
Addicted Member
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:
Private Sub Form_Load()
GetIni "C:\myini.ini"
End Sub
Private Sub GetIni(FileString As String)
Dim TextString As String
Dim arraydatareadini() As Integer
Dim IndexCount As Integer
Dim EqualPos As Integer
Open FileString For Input As #1
Do While Not EOF(1)
Line Input #1, TextString
If InStr(1, TextString, "[DataToRead]") > 0 Then 'find ini key
Do While TextString <> "" And Not EOF(1) 'check for no more values or end of file
Line Input #1, TextString 'get the value
TextString = Trim(TextString) 'trim spaces off string
IndexCount = IndexCount + 1 'increment counter
ReDim Preserve arraydatareadini(IndexCount) 'redim array by one
EqualPos = InStr(1, TextString, "=") 'find where the = is in the string
'start at one past the equal, extract the value, convert to an integer, and assign to array
arraydatareadini(IndexCount) = CInt(Mid(TextString, EqualPos + 1, Len(TextString) - EqualPos))
Loop
End If
Loop
Close #1
'Print the contents of your new array
For x = 1 To UBound(arraydatareadini)
Debug.Print arraydatareadini(x)
Next
End Sub
good luck
If you can think it....you can code it....
-
Sep 27th, 2001, 12:47 PM
#3
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
-
Sep 27th, 2001, 01:43 PM
#4
Addicted Member
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....
-
Sep 27th, 2001, 05:00 PM
#5
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
-
Sep 27th, 2001, 06:50 PM
#6
Addicted Member
:)
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....
-
Sep 28th, 2001, 08:46 AM
#7
Addicted Member
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:
Public Sub ParseFile(FileName As String, Delimiter As String)
' **************************************************************
' * Programmer Name : Steve (Sibby) Komoll
' * Date : 5/12/01
' **************************************************************
' * Comments : Parses any separated values file
' *
' *
' **************************************************************
Dim SingleLine As String
Dim StartPos As Integer
Dim EndPos As Integer
Dim ParsedString As String
Open FileName For Input As #1
While Not EOF(1)
StartPos = 1 'initialize the starting position
Line Input #1, SingleLine
Do While StartPos <= Len(SingleLine)
'find the delimiter position
EndPos = InStr(StartPos, SingleLine, Delimiter)
'if delimiter is not at end of string, move end position past string length
If EndPos = 0 Then EndPos = Len(SingleLine) + 1
'get the value string
ParsedString = Mid(SingleLine, StartPos, EndPos - StartPos)
Debug.Print ParsedString ' <== do what you need with the value here
'set the next starting position 1 past the current delimiter
StartPos = EndPos + 1
Loop
Wend
Close #1
End Sub
Good luck man! Lemme know how it turns out!
If you can think it....you can code it....
-
Sep 28th, 2001, 09:01 AM
#8
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.
-
Sep 28th, 2001, 10:26 AM
#9
Addicted Member
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:
Dim DataToRead(1) As String
Private Sub Form_Load()
DataToRead(0) = "1000,20000,3000,40000,50000,"
DataToRead(1) = "60000,7000,80000,9000,1000,"
ParseFile
End Sub
Public Sub ParseFile()
Dim SingleLine As String
Dim StartPos As Integer
Dim EndPos As Integer
Dim ParsedString As String
For x = 0 To UBound(DataToRead)
StartPos = 1 'initialize the starting position
SingleLine = StringArray(x)
Do While StartPos <= Len(SingleLine)
'find the delimiter position
EndPos = InStr(StartPos, SingleLine, ",")
'if delimiter is not at end of string, move end position past string length
If EndPos = 0 Then EndPos = Len(SingleLine) + 1
'get the value string
ParsedString = Mid(SingleLine, StartPos, EndPos - StartPos)
Combo1.AddItem ParsedString '<== add the items to your combobox here
'set the next starting position 1 past the current delimiter
StartPos = EndPos + 1
Loop
Next
End Sub
If you can think it....you can code it....
-
Sep 28th, 2001, 10:38 AM
#10
Addicted Member
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.
-
Sep 28th, 2001, 10:45 AM
#11
Thanks Sibby,
You really helped out a lot. I will call on you again soon.
-
Sep 28th, 2001, 11:05 AM
#12
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
|