newbieforever wrote:Is FB really unable to handle such a simple task??? Can this be true???
I got it running, but unic.txt must delimit the filename with [brackets]:
Code: Select all
#Define winapimode 1 ' 0=use FB functions, 1=use WinAPI functions
#Include "file.bi"
#include "Windows.bi"
Sub savefile(filename As wstring, p As String)
Print "Opening [";filename;"]"
#if winapimode
Dim As any ptr n, pstr
Dim As Integer byteswritten
n=CreateFileW(filename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0)
If n<>-1 then ' Peek(any ptr, VarPtr(p))
WriteFile(n, @p[0], Len(p), @byteswritten, 0)
Print byteswritten; " bytes written"
CloseHandle(n)
Else
Print "Unable to open " + filename
End If
#else
Dim As Integer n
n=freefile
If Open(filename For Binary Access Write As #n)=0 then
Put #n,,p
Close
Else
Print "Unable to open " + filename
End If
#endif
End Sub
Function loadfile(file as wstring) as String
' If FileExists(file)=0 Then Print file;" not found":Sleep:end
Dim As String text="ERROR"
#if winapimode
Dim as any ptr f
Dim as integer fsize, bytesread
f=CreateFileW(file, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)
if f then
fsize=GetFileSize(f, 0)
text=String(fsize, Asc("x"))
ReadFile(f, @text[0], fsize, @bytesread, 0)
CloseHandle(f)
endif
#else
var f=freefile
Open file For Binary Access Read As #f
If Lof(f) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
#endif
return text
end Function
#if winapimode ' the filename:
' dim as wstring * 30 file= "مرحبا بكم.txt"
dim as wstring * 30 file= "Добро.txt"
Dim as integer posL, posR
dim as string tmp
dim as string unic =loadfile("unic.txt") 'loaded from disk
posL=Instr(unic, "["+chr(0)) 'the chr(0) gets ignored!
posR=Instr(unic, "]"+chr(0)) ' ">" would NOT work
Print "L=";posL;", Rx=";posR
' posR=posL+20 ' ** doesn't work: Instr(unic, ">"+chr(0))
tmp=Mid(unic, posL+2, posR-posL-3)+chr(0)+chr(0)
asm pushad ' copies name from tmp to file
asm mov esi, [tmp]
asm lea edi, [file]
asm mov ecx, 30
asm rep movsb
asm popad
' MessageBoxW(0, @tmp[0], "tmp:", MB_OK)
' MessageBoxW(0, @file[0], "File:", MB_OK)
' Print unic
Print "L=";posL;", R=";posR
' file=tmp
#else
dim as wstring * 10 file= "Abcde.txt"
#endif
print "The filename is ["; file; "]"
dim as string binaryfile="Hello World, how are you?" ' "1234"+chr(0)+"5"+chr(206)+"6789"
savefile(file,binaryfile) 'saved to disk
dim as string s =loadfile(file) 'loaded from disk
print s
' kill file 'erase the file
sleep
There seems to be a bug or undefined behaviour in Instr(). While FB claims that its strings are
not zero-delimited C strings, the behaviour of Instr is different:
Instr(unic, ">"+chr(0)) would always return the first occurrence of ">", whether followed by a zero byte or not. And by chance, the Unicode representation of Добро.txt does contain some early matches for 3E hex - otherwise I would never had noticed this behaviour. It works with [square brackets] because there aren't any in Добро.txt ...
Note there
must be more elegant solutions to do this, but I am not expert enough in this language.