You could try the barebones ushort pointer to handle non ascii text.
This ensures you can capture wchr(0) if needed.
You can overload (the subs) to handle ascii.
The C runtime is needed to block load block save and handle non ascii filenames.
Code: Select all
#include "crt.bi"
#include "file.bi"
Function filelength(filename As wstring Ptr)As Long
Dim As wstring * 4 k="r"
Var fp=_wfopen(filename,@k)
If fp = 0 Then Print "Error opening file":Sleep:End
fseek(fp, 0, SEEK_END)
Var length=ftell(fp)
fclose(fp)
Return(length)
End Function
Sub savefile overload(content As String ,filename As wstring Ptr)
Dim As wstring * 4 k="wb"
Var fp= _wfopen(filename,@k)
If fp = 0 Then Print "Error opening file":Sleep:End
fwrite(@content[0], 1, Len(content), fp)
fclose(fp)
End Sub
Sub loadfile overload( content As String,filename As wString Ptr)
Var l=Filelength(filename)
content=String(l,0)
Dim As wstring * 4 k="rb"
Var fp= _wfopen(filename,@k)
If fp = 0 Then Print "Error loading file ";filename:Sleep:End
fread(@content[0], 1,l, fp)
fclose(fp)
End Sub
Sub savefile(content As ushort ptr ,filename As wstring Ptr)
Dim As wstring * 4 k="wb"
Var fp= _wfopen(filename,@k)
If fp = 0 Then Print "Error opening file":Sleep:End
fwrite(content, 2, Len(*cast(wstring ptr,content)), fp)
fclose(fp)
End Sub
Sub loadfile(content As ushort ptr,filename As wString Ptr)
Var l=Filelength(filename)
Dim As wstring * 4 k="rb"
Var fp= _wfopen(filename,@k)
If fp = 0 Then Print "Error loading file ";filename:Sleep:End
fread(content, 2,l, fp)
fclose(fp)
End Sub
Function exists(filename As wString Ptr) As boolean
Dim As wstring * 4 k="r"
Var fp= _wfopen(filename,@k)
If fp=0 Then
Return false
Else
fclose(fp)
Return true
End If
End Function
dim as wstring * 20 zw=wchr(&h48,&h65,&h6c,&h6c,&h6f,&h20,&h57,&h6f,&h72,&h6c,&h64,&h21,&h20)
Dim As wstring * 40 filename=wchr(&h48,&h65,&h6c,&h6c,&h6f,&h20,&h57,&h6f,&h72,&h6c,&h64,&h21,&h20)+".txt"
Print "Filename ";filename
dim as wstring * 10000 w=zw
for n as long=1 to 7
w+=w
next
print "Wstring length ";len(w)
print "To file:"
print w
savefile(@w[0],filename)
print
print "filelen "; filelen(filename)
redim as ushort us(1 to filelen(filename)\2)
loadfile(@us(1),filename)
dim as wstring * 10000 ans
for n as long=1 to ubound(us)
ans+= wchr(us(n))
next
print
print "From file:"
print ans
print
print "Are loaded wstring and returned wstring equal? "; cbool(ans=w)
savefile("Goodbye from overloaded sub, press a key to exit","dummy.txt")
dim as string s
loadfile(s,"dummy.txt")
print s
_wremove(@filename)
kill "dummy.txt"
Print "Does file " ; filename; " exist? ";exists(filename)
Print "Does file dummy.txt exist? ";exists("dummy.txt")
Sleep