Task

Load the entire contents of some text file as a single string variable.

If applicable, discuss: encoding selection, the possibility of memory-mapping.

Of course, in practice one should avoid reading an entire file at once
if the file is large and the task can be accomplished incrementally instead
(in which case check File IO);
this is for those cases where having the entire file is actually what is wanted.




Declare Function MultiByteToWideChar Lib "kernel32.dll" ( _
     ByVal CodePage As Long, _
     ByVal dwFlags As Long, _
     ByVal lpMultiByteStr As Long, _
     ByVal cchMultiByte As Long, _
     ByVal lpWideCharStr As Long, _
     ByVal cchWideChar As Long) As Long
Const CP_UTF8 As Long = 65001
 
Sub Main()
Dim fn As Integer
Dim i As Long
Dim b() As Byte
Dim s As String
 
  fn = FreeFile()
  Open "c:\test.txt" For Binary Access Read As #fn
  ReDim b(0 To (LOF(fn) - 1))
  Get #fn, 1, b()
 
  If b(0) = &HFF And b(1) = &HFE Then
  'UTF-16, little-endian
    ReDim b(0 To (LOF(fn) - 3))
    Get #fn, 3, b()
    s = b()
  ElseIf b(0) = &HEF And b(1) = &HBB And b(2) = &HBF Then
  'UTF-8
    i = MultiByteToWideChar(CP_UTF8, 0&, VarPtr(b(3)), LOF(fn) - 3, StrPtr(s), 0)
    s = Space$(i)
    i = MultiByteToWideChar(CP_UTF8, 0&, VarPtr(b(3)), LOF(fn) - 3, StrPtr(s), Len(s))
  Else
  'assume ANSI
    s = StrConv(b(), vbUnicode)
  End If
  Close #fn
  Debug.Print s
End Sub