|
-
Oct 11th, 2011, 11:32 PM
#1
[RESOLVED] Input values from text file
Hi,
How would I achieve the some outcome here but with a text file instead of an ini file?
This is what I have tried but the input into the program does not work.
vb Code:
Dim start As String
Public Sub WData(settings As String)
ff = FreeFile
start = frmMain.chkStartup.Value
cap = frmMain.chkStartup.Caption
Open settings For Output As #ff
Print #ff, "[Load]" & vbCrLf & cap & " = " & start
Close #ff
End Sub
Public Sub RData(settings As String)
ff = FreeFile
Dim tmpString() As String
Open settings For Input As #ff
Do Until EOF(ff)
Input #ff, entry
tmpString = Split(entry, " ")
If UBound(tmpString) = 0 Or UBound(tmpString) = 1 Then frmMain.chkStartup.Value = entry
Loop
Close #ff
End Sub
Thanks,
Nightwalker
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Oct 12th, 2011, 01:33 AM
#2
Re: Input values from text file
I assume you still want the format of the file to 'look' like a .ini file (?)
If this is the case, something like this might do
Code:
Option Explicit
Private Type Settings
Section As String
KeyData() As String
End Type
Private uSettings() As Settings
Private Sub RData(strSettingsFile As String)
Dim intFile As Integer
Dim intSet As Integer
Dim intKey As Integer
Dim intI As Integer
Dim intJ As Integer
Dim intK As Integer
Dim intPos As Integer
Dim strData As String
Dim strRecs() As String
Dim strD() As String
intSet = -1
intFile = FreeFile
Open strSettingsFile For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
strRecs = Split(strData, vbNewLine)
ReDim uSettings(UBound(strRecs))
For intI = 0 To UBound(strRecs)
If strRecs(intI) <> vbNullString Then
If Mid$(strRecs(intI), 1, 1) = "[" Then
If intKey > 0 Then ReDim Preserve uSettings(intSet).KeyData(intKey - 1)
intKey = 0
intSet = intSet + 1
If intSet > UBound(uSettings) Then ReDim Preserve uSettings(UBound(uSettings) + 100)
intPos = InStr(2, strRecs(intI), "]")
uSettings(intSet).Section = Mid$(strRecs(intI), 2, intPos - 2)
ReDim uSettings(intSet).KeyData(9)
Else
If intKey > UBound(uSettings(intSet).KeyData) Then ReDim Preserve uSettings(intI).KeyData(UBound(uSettings(intI).KeyData) + 100)
uSettings(intSet).KeyData(intKey) = strRecs(intI)
intKey = intKey + 1
End If
End If
Next intI
If intKey > 0 Then ReDim Preserve uSettings(intSet).KeyData(intKey - 1)
ReDim Preserve uSettings(intSet)
For intI = 0 To UBound(uSettings)
Debug.Print "Section: "; uSettings(intI).Section
For intJ = 0 To UBound(uSettings(intI).KeyData)
strD = Split(uSettings(intI).KeyData(intJ), "=")
Debug.Print "Key: '"; strD(0); "' Data: "; strD(1)
Next intJ
Next intI
End Sub
Private Sub Form_Load()
Call RData("C:\Mobs.ini")
End Sub
The data file might look like this
Code:
[Section 1]
Number= 1
Value1= 1
Value2= 2
Value3= 3
[Section 2]
Number=2
Value1= 10
[Section 3]
Number= 3
Value5= 30
Value20= 10
Value44= 2.3
and the ouptut like this
Code:
Section: Section 1
Key: 'Number' Data: 1
Key: 'Value1' Data: 1
Key: 'Value2' Data: 2
Key: 'Value3' Data: 3
Section: Section 2
Key: 'Number' Data: 2
Key: 'Value1' Data: 10
Section: Section 3
Key: 'Number' Data: 3
Key: 'Value5' Data: 30
Key: 'Value20' Data: 10
Key: 'Value44' Data: 2.3
EDIT: Of course, you'll have to put some validation in to make sure that the sections end with a ']' and there's at least one key - data pair per section. (Just in case the user 'plays' with the file and messes it up !)
Last edited by Doogle; Oct 12th, 2011 at 01:56 AM.
-
Oct 12th, 2011, 01:47 AM
#3
Re: Input values from text file
Thanks! Although, when I run the code I receive an object required error on this line.
vb Code:
uSettings(intSet).Section = Mid$(strRecs(intI), 2, intPos - 2)
What does that line refer to?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Oct 12th, 2011, 02:04 AM
#4
Re: Input values from text file
Wierd ! Did you try it with the Data I used ? Have you copied and pasted all the code (including that in the Declarations Section) correctly ? Do all your Sections end with a "]" ? Is there at least one Key - Data pair in each section ?
EDIT: That line assigns the Section Name to the 'Section' value in the intSet'th element of the UDT Array. strRecs(inti) should hold something like "[Section 1]" (or if you're using your data "[Load]")
Last edited by Doogle; Oct 12th, 2011 at 02:14 AM.
-
Oct 12th, 2011, 02:18 AM
#5
Re: Input values from text file
Never mind! I moved this part of the code to the top of the module and it solved the problem.
vb Code:
Private uSettings() As Settings Dim start As String Private Type Settings Section As String KeyData() As String End Type
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
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
|