VB:Tutorials:Save Any Sized Strings

From GDWiki

Jump to: navigation, search

The following functions will load and save any sized strings.

Private Sub Form_Load()
    Dim Free as Integer
 
    Free = FreeFile
 
    Open App.Path & "/Test.tst" For Binary As Free
        Call Put_String(Free, "Testing")
    Close Free
 
    Open App.Path & "/Test.tst" For Binary As Free
        MsgBox (Get_String(Free))
    Close Free
End Sub

Putting the string into a file:

Public Sub Put_String(FileNum As Integer, Str As String)
    Dim position As Long
    Dim letter As Byte
    Dim length As Integer
    Dim one_letter_string As String
 
    length = Len(Str)
    Put FileNum, , length 
 
    For position = 1 To length 
        one_letter_string = Mid$( Str, position, 1)
        letter = Asc$(one_letter_string)
        Put #FileNum, , letter
    Next position
 
End Sub

Retrieving the string from the file:

Public Function Get_String(FileNum As Integer) As String
    Dim position As Long
    Dim char As Byte
    Dim length As Integer
 
    Get FileNum, , length 
 
    For position = 0 To length - 1
        Get FileNum, , char
        Get_String = Get_String & Chr(char)
    Next position
 
End Function

Thats about it :D


- Chris Fesko "cdfbr"

Added June 2, 2005

[edit] External Links

Categories