Visual Basic 6 function "file_get_contents"

Go back

Below you'll find the source for the Visual Basic 6 function file_get_contents.

Attribute VB_Name = "modFileGetContents"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

''
' Reads entire file into a string
' Almost the same syntax as the PHP function 'file_get_contents'
' See also: http://www.php.net/manual/en/function.file-get-contents.php
' @param    String  filename        The file to read
' @return   String                  The contents of the file
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function file_get_contents(filename As String) As String
    Dim thedata As String, ff As Integer
    ' Note, length of 'TheData' will determine how many bytes are read.
    ff = FreeFile
    Open filename For Binary Access Read Shared As #ff
        thedata = Space(LOF(ff))
        Get #ff, 1, thedata
    Close #ff
    
    file_get_contents = thedata
End Function