Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get information from a text file to use in my program? 5

Status
Not open for further replies.

Tonyn2002

Technical User
Dec 19, 2001
11
US
I can't seem to get this to work for a school project.

Example of what I am after

Included in the text file would be these settings (kind of like a configuration settings)

[settings]
taxes=.07
lastpath=where ever a file was last saved
lastlogon=who ever last logged on

[options]
font=arial
startwithwindows=either on or off (1/0)
timelastupdated=time

[schedule]
lastrun=1/13/2002 9:20:02 AM (makes a backup of database at certain times)
minutes=0



Now, I would need to be able to change some of these items within the program - such as lastlogon. Lastlogon would obviously sho who last logged into the computer, and would change every tome someone new logged in.


Any help would be greatly appreciated. I am overdoing it on this project to get a good grade in class!
 
You can do several things to read and write the data. You could write code yourself using the OPEN STATEMENT:

Dim A as Byte, FileData as string
A=FreeFile
Open "myfile.ini" for input as #A
Do While Eof(A)
Line input #A, FileData
etc....

Or you could use the FileSystemObject. Also, the GetPrivateProfileString and SetPrivateProfileString API was designed to handle files like you have [ini format].

If you are not sure how to use API functions, do a search on the word Declare. Use the API Wizzard add-in that ships with VB to get the syntax of the APIs.
 
thanks for the quick reply. I am not too sure on the API functions, but will do the search as you suggested.

Thanks again

and if anyone has anything to add that would help me out - feel free :)
 
Ok, still looking into this

But can someone do me a (hopefully) quick favor....

Is there any way you could maybe write code for just one input from the ini file such as setting the tax rate

assuming the file is named vintagevideo.ini
and included in that file is TAXRATE = .07

That is of course the code would not be too terribly long. I can seem to get the ini file to be loaded in, but can't set my tax value to what is in that ini file don't know why. At the moment I am using the Open command which is loading it, but like I said I can't for the life of me set "sngLocalTaxRate as Single" to the .07...?

Yeah, I am a newby - but learning (and a lot of thanks for those that have helped me)
 
i am not sure if this is the way that you want it to be
you may want to use a temp file... first copy whatever you want to a temp file
then do some changes and copy the rest back to the temp file. then rename the file and kill the temp file


Dim strInFileName, strOutFileName As String
Dim strLogEntry, strLogID As String
Dim strEntryDate, strEntryTime As String
Dim intInFileNum1, intOutFileNum As Integer

Dim intCounter As Integer
Dim strCurDate As String
Dim strCurTime As String
Dim strMessage As String
strInFileName = App.Path & "\" & "LogFile" & ".log"
strOutFileName = App.Path & "\" & "LogFile" & ".tmp"

intInFileNum1 = FreeFile
Open strInFileName For Input As intInFileNum1

intOutFileNum = FreeFile
Open strOutFileName For Append As intOutFileNum

Having a do while loop to read the data from file1 to file2
Line Input #intInFileNum1, strLogEntry ' Read line into variable.
[Do the changes you want]
end do while loop
Wend
Close #intInFileNum1 ' Close file.
Close #intOutFileNum ' Close file
Kill strInFileName ' Delete the File
Name strOutFileName As strInFileName ' Rename the Temp File to the existing file

End Sub

Not sure if this is the way you want it
but good luck...
 
That actually looks like a good approach - didn't think of that one actually.

Thanks I will give that one a try :)
 
Tony,

Here's an example of code to read/write from an ini file (as you specified above) Sorry, I didn't have time to clean up the code..

<CODE>

<--API declarations-->
Public Declare Function WritePrivateProfileString Lib &quot;kernel32&quot; Alias &quot;WritePrivateProfileStringA&quot; (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long

Public Declare Function GetPrivateProfileString Lib &quot;kernel32&quot; Alias &quot;GetPrivateProfileStringA&quot; (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

Public Declare Function GetPrivateProfileSection Lib &quot;kernel32&quot; Alias&quot;GetPrivateProfileSectionA&quot; (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Declare Function GetPrivateProfileSectionNames Lib &quot;kernel32&quot; Alias &quot;GetPrivateProfileSectionNamesA&quot; (ByVal ReturnString As String, ByVal nSize As Long, ByVal FileName As String) As Long

<--Some functions -- might be usefull-->

Private Function GetIniSectionNames(ByRef Sections() As String, ByVal iniLocation As String) As Long

Dim Buffer As String * 32767
Dim i As Long, pos1 As Long, pos2 As Long, PLen As Long
Dim Aantal As Long

Buffer = &quot;&quot;
Call GetPrivateProfileSectionNames(Buffer, 32767, iniLocation)
i = 0
pos1 = 1: pos2 = 1
Do While pos1 < 32767
ReDim Preserve Sections(i)
pos1 = pos2
pos2 = InStr(pos1 + 1, Buffer, Chr(0))
PLen = pos2 - pos1
If Not PLen <= 0 Then
Sections(i) = Mid(Buffer, pos1, PLen)
pos2 = pos2 + 1
i = i + 1
Else
pos1 = 32768
End If
Loop
GetIniSectionNames = i
End Function

Public Function GetForms(ByRef Forms() As String, ByRef AppTitle As String, ByVal FileName As String) As Long
Dim nForms() As String
Dim i As Long
Dim Buffer As String * 255

Call GetIniSectionNames(nForms, FileName)
Buffer = &quot;&quot;
Call GetPrivateProfileString(&quot;FileInfo&quot;, &quot;AppTitle&quot;, &quot;&quot;, Buffer, 255, FileName)
AppTitle = Trim(Buffer)
AppTitle = Mid(AppTitle, 1, Len(AppTitle) - 1)
For i = 1 To UBound(nForms) - 1
ReDim Preserve Forms(i - 1)
Forms(i - 1) = nForms(i)
Next i
GetForms = i - 1
End Function

Public Function GetControls(ByRef Controls() As UControl, ByVal FormName As String, ByVal FileName As String) As Long
Dim Buffer As String * 32767
Dim Length As Long
Dim pos1 As Long
Dim pos2 As Long
Dim i As Long

Length = GetPrivateProfileSection(FormName, Buffer, 32767, FileName)
pos1 = 1
pos2 = 1
i = 0
Do While pos1 < Length And pos2 < Length
pos1 = InStr(pos2, Buffer, &quot;=&quot;)
pos2 = InStr(pos1, Buffer, Chr(0))
ReDim Preserve Controls(i)
Controls(i).Naam = Mid(Buffer, pos1 + 1, pos2 - pos1 - 1)
i = i + 1
Loop
End Function

</CODE>

Regards
Johpje
 
Just read the entire file into memory as a string.
Alter what you need (can use InStr for example to find) then write it all back. Give a man a program and tomorrow he will be hungry.
Teach a man to program and he will never hunger again.
--Sunr¿se

 
You gotta love this place! Thanks for all the help, I finally got it to work thanks to you all.

Really appreciate it, I really do. I now also have a better understaning of these API functions as well.

Now to package this one up and turn it in :)
 
FAQ222-1198 has an an old version of my INI class. There are other FAQs too, like FAQ222-968 or the more extensive FAQ222-39

Wil Mead
wmead@optonline.net

 
Sunrise... i like this


(Give a man a program and tomorrow he will be hungry.
Teach a man to program and he will never hunger again.
--Sunr¿se)

i will teach him how to cook next time so he will not be hungry...


hehe...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top