Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
Dim InpFile As String
Dim OutFile As String
Dim hFile As Long
Dim fileStruct As OFSTRUCT
Dim FSize As Long
Dim BytesRead As Long
Dim BytesWritten As Long
Dim hMem As Long
Dim lpMem As Long
Dim r As Long
Dim bts As Byte
InpFile = "input.txt"
OutFile = "OUTPUT.txt"
'Get the size of the file to be read
FSize = FileLen(InpFile)
If FSize > 0 Then
'Allocate a block of memory equal to the size of the input file.
'hMem = GlobalAlloc(GMEM_ZEROINIT, FSize)
hMem = GlobalAlloc(GPTR, FSize)
If hMem <> 0 Then
lpMem = GlobalLock(hMem)
'Read the file into memory
hFile = OpenFile(InpFile, fileStruct, OF_READ Or _
OF_SHARE_DENY_NONE)
BytesRead = hread(hFile, ByVal lpMem, FSize)
MsgBox Format(BytesRead) & " bytes read into memory"
r = lclose(hFile)
'Write the file back to disk to verify the file was
'read correctly
hFile = OpenFile(OutFile, fileStruct, OF_CREATE Or _
OF_WRITE Or OF_SHARE_DENY_NONE)
BytesWritten = hwrite(hFile, ByVal lpMem, FSize)
MsgBox Format(BytesWritten) & " bytes written to output file"
r = lclose(hFile)
'Free resources
r = GlobalUnlock(hMem)
r = GlobalFree(hMem)
Else
MsgBox "Not enough memory to store file"
End If
Else
MsgBox "Input file is zero bytes in length"
End If
End
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Dim FilePath As String
Dim FileNumber As Long
Dim FileData As String
FilePath = "C:\Full\Path\To\File.txt"
FileNumber = FreeFile
Open FilePath For Binary Access Read As FileNumber
' Use following if non-unicode data
FileData = StrConv(InputB(LOF(FileNum
' Use following if unicode data
'FileData = InputB(LOF(FileNumber), FileNumber)
Close FileNumber
Kevin