Hi Charles,
I am experimenting a bit with CreateFile and Readfile and try to compare with Oxygen's getfile function. I am able to load text with ReadFile into a buffer of type char* or zstring*, but it does not work with string or bstring. Is this possible in some way? There are some older postings dealing with ReadFile and strings, but they do not run with the latest version of Oxygen. Unfortunately Peter has deleted his messages, so it is a bit difficult to see the context.
Does getfile apply getmemory or freememory internally?
Roland
TestReadFile.o2bas:
$ filename "TestReadFile.exe"
'uses rtl32
'uses rtl64
uses corewin
uses console
function lof (string Fname) as int
'length of file
WIN32_FIND_DATA W32FD
sys hFile
int FSize
if len(Fname) = 0 then return 0
hFile=FindFirstFile(Fname, &W32FD)
if hFile != INVALID_HANDLE_VALUE then
FSize = W32FD.nFileSizeLow
FindClose(hFile)
return FSize
end if
return 0
end function
function LoadFile (string Fname, sys Buff) as int
' Assumes a valid existing file and Buff large enough to hold the content of the file
sys Read
sys hFile
int Fsize
Fsize = LOF(Fname)
hFile = CreateFile(Fname,GENERIC_READ,FILE_SHARE_READ,null,OPEN_EXISTING,0,0)
if hFile != INVALID_HANDLE_VALUE then
ReadFile (hFile,Buff,Fsize,&Read,null)
CloseHandle(hFile)
end if
function = Read
end function
cls
int FileSize
string fn = "TestReadFile.o2bas"
sys buf = getmemory (lof(fn) +1)
'zstring *buffer : @buffer = buf
char *buffer : @buffer = buf
FileSize = LoadFile(fn, @buffer)
print fn
printl "LoadFile loaded " FileSize " bytes into buffer"
string s=getfile(fn)
printl "Getfile loaded " len(s) " bytes into string"
printl "Press Enter to display contents of buffer"
waitkey
Cls
print buffer 's
freememory buf
printl "Enter..."
waitkey