How to write/read to/from file? 
Author Message
 How to write/read to/from file?

Dear all, if I had a file like this:

sound = true
autostart = true
minimise = false
interval = 30
break = 5

and so on...

How would I read these settings? And write back the value if they're changed
by the user running the program? I'm currently using As Input and As Output
but that's only sequential and the file would have to be like
true,true,false,30,5 which is not looking very pro.

Thanks,
        Jules

*There is not enough darkness to extinguish the light of a small candle*



Sat, 04 Aug 2001 03:00:00 GMT  
 How to write/read to/from file?
Jules,

You might want to format the file more like an .INI file with a section
header, as follows:

[Settings]
sound = true
autostart = true
minimise = false
interval = 30
break = 5

Then you could use the API functions WritePrivateProfileString,
WritePrivateProfileInt (for integer values), and their corresponding "get"
functions, GetPrivateProfileString and GetPrivateProfileInt.  These will
allow reading and writing the file with a minimum of effort, and the file
will have the format you desire.  You also won't need to read through the
file to find just one entry to be read or modified.  You specify the
section, key, and value, and the API does the work to read or update the
file.

Vinnie Murdico
Software with Brains, Inc.
http://www.softwarewithbrains.com

Quote:

>Dear all, if I had a file like this:

>sound = true
>autostart = true
>minimise = false
>interval = 30
>break = 5

>and so on...

>How would I read these settings? And write back the value if they're
changed
>by the user running the program? I'm currently using As Input and As Output
>but that's only sequential and the file would have to be like
>true,true,false,30,5 which is not looking very pro.

>Thanks,
>        Jules

>*There is not enough darkness to extinguish the light of a small candle*



Sat, 04 Aug 2001 03:00:00 GMT  
 How to write/read to/from file?
The Trim function only trims spaces.  After you trim the spaces there is
still a Null character on the end of the Result string.  Use the following
code for the cmdRead_Click() sub:
----------------------------------------------------------------------------
---------------------------------
Dim Result As String
Dim Interval As Long
Result = Space$(10)
Call GetPrivateProfileString("Test", "autostart", _
"false", Result, 10, App.Path + "\settings.ini")
Interval = GetPrivateProfileInt("Test", "interval", _
10, App.Path + "\settings.ini")
Result = Trim(Result)

' Added the next 3 lines to trim the Null character
If Right(Result, 1) = Chr(0) Then
Result = Left(Result, Len(Result) - 1)
End If

If Result = "true" Then
MsgBox Result
ElseIf Result = "false" Then
MsgBox Result
End If
----------------------------------------------------------------------------
---------------------------------

I hope this helps!

Best Wishes,
Jesse Olson


Quote:
>Dear all, I've written this test program to write and read from
settings.ini
>in the local directory. It does write, but doesn't read properly, I mean
>When I click Read button, The MsgBox doesn't come up so that means Result
is
>not "true" or "false", how come?

>Option Explicit

>Private Sub cmdRead_Click()
>    Dim Result As String
>    Dim Interval As Long
>    Result = Space$(10)
>    Call GetPrivateProfileString("Test", "autostart", "false", Result, 10,
>App.Path + "\settings.ini")
>    Interval = GetPrivateProfileInt("Test", "interval", 10, App.Path +
>"\settings.ini")
>    Result = Trim(Result)
>    If Result = "true" Then
>        MsgBox Result
>    ElseIf Result = "false" Then
>        MsgBox Result
>    End If
>End Sub

>Private Sub cmdWrite_Click()
>    Call WritePrivateProfileString("Test", "autostart", "true", App.Path +
>"\settings.ini")
>    Call WritePrivateProfileString("Test", "interval", "30", App.Path +
>"\settings.ini")
>End Sub

>Please help.

>Thanks,
>            Jules

>*You cannot strengthen the weak by weakening the strong*



Sat, 04 Aug 2001 03:00:00 GMT  
 How to write/read to/from file?
Dear all, I've written this test program to write and read from settings.ini
in the local directory. It does write, but doesn't read properly, I mean
When I click Read button, The MsgBox doesn't come up so that means Result is
not "true" or "false", how come?

Option Explicit

Private Sub cmdRead_Click()
    Dim Result As String
    Dim Interval As Long
    Result = Space$(10)
    Call GetPrivateProfileString("Test", "autostart", "false", Result, 10,
App.Path + "\settings.ini")
    Interval = GetPrivateProfileInt("Test", "interval", 10, App.Path +
"\settings.ini")
    Result = Trim(Result)
    If Result = "true" Then
        MsgBox Result
    ElseIf Result = "false" Then
        MsgBox Result
    End If
End Sub

Private Sub cmdWrite_Click()
    Call WritePrivateProfileString("Test", "autostart", "true", App.Path +
"\settings.ini")
    Call WritePrivateProfileString("Test", "interval", "30", App.Path +
"\settings.ini")
End Sub

Please help.

Thanks,
            Jules

*You cannot strengthen the weak by weakening the strong*



Sun, 05 Aug 2001 03:00:00 GMT  
 How to write/read to/from file?
If it is just one file that you are working with, why not storing the values
in the registry. It works very well. Look for SaveSettings and GetSettings
functions of VB.

Good luck!



Sun, 05 Aug 2001 03:00:00 GMT  
 How to write/read to/from file?
You could use the registry, but of course who cares what it looks like as
long as it works! Remember there a millions of ways of doing the same thing.
Quote:

>Dear all, if I had a file like this:

>sound = true
>autostart = true
>minimise = false
>interval = 30
>break = 5

>and so on...

>How would I read these settings? And write back the value if they're
changed
>by the user running the program? I'm currently using As Input and As Output
>but that's only sequential and the file would have to be like
>true,true,false,30,5 which is not looking very pro.

>Thanks,
>        Jules

>*There is not enough darkness to extinguish the light of a small candle*



Sun, 05 Aug 2001 03:00:00 GMT  
 How to write/read to/from file?
Also compare just with one value:

If Result = "true" Then
    MsgBox Result
Else
    MsgBox Result
End If

Ini files can be edited with a text editor and you can put any value in a
key. So just check for one value and assume any other values as False.

--
Eduardo Morcillo

http://www.geocities.com/SiliconValley/Foothills/9940/index_sp.html



Sun, 05 Aug 2001 03:00:00 GMT  
 How to write/read to/from file?
You shouldn't be using Call with the GetPrivateProfileString function.
Instead, you need to get the returned value. This value will contain
the number characters returned into your Result variable. Then use the
Left function to remove the excess characters:

        Dim RtnLen as Long

        RtnLen = GetPrivateProfileString("Test", "autostart", _
                "false", Result, 10, App.Path + "\settings.ini")
        Result = Left(Result, RtnLen)

Quote:

>Dear all, I've written this test program to write and read from settings.ini
>in the local directory. It does write, but doesn't read properly, I mean
>When I click Read button, The MsgBox doesn't come up so that means Result is
>not "true" or "false", how come?

>Option Explicit

>Private Sub cmdRead_Click()
>    Dim Result As String
>    Dim Interval As Long
>    Result = Space$(10)
>    Call GetPrivateProfileString("Test", "autostart", "false", Result, 10,
>App.Path + "\settings.ini")
>    Interval = GetPrivateProfileInt("Test", "interval", 10, App.Path +
>"\settings.ini")
>    Result = Trim(Result)
>    If Result = "true" Then
>        MsgBox Result
>    ElseIf Result = "false" Then
>        MsgBox Result
>    End If
>End Sub

>Private Sub cmdWrite_Click()
>    Call WritePrivateProfileString("Test", "autostart", "true", App.Path +
>"\settings.ini")
>    Call WritePrivateProfileString("Test", "interval", "30", App.Path +
>"\settings.ini")
>End Sub

>Please help.

>Thanks,
>            Jules

>*You cannot strengthen the weak by weakening the strong*



Sun, 05 Aug 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. How to write/read to/from file?

2. How to write/read to/from file?

3. How to write/read to/from file?

4. Writing a VBS file which reads and writes to the registry

5. Reading from a text file and writing toa text file from Vis Bas 6.0

6. reading binary file and writing into a random access file

7. How to read one file and write certain fields to another file

8. VB -- Long File Name , Fast Binary File Read / Write , interface with DLL

9. overlapping during Read file and write file

10. Creating/Reading/Writing files and directories

11. reading and writing files

12. Read/Write from list of queries to text file

 

 
Powered by phpBB® Forum Software