Problem fopen with filename in Unicode

New to FreeBASIC? Post your questions here.
Post Reply
nungd
Posts: 1
Joined: Jan 24, 2023 2:20

Problem fopen with filename in Unicode

Post by nungd »

Dear all,

I have problem in my project so fopen can not work with filename in Unicode format, How can i do or you have other way

my code:

hFile = fopen("myfilename in unicode" ,"rt+")
If hFile = ( -1) Then
Return
End If

Thank you in advanced.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Problem fopen with filename in Unicode

Post by dodicat »

Using the C runtime and block saving/loading freebasic strings.

Code: Select all

#include "crt.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 savefilestring(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 loadfilestring( 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

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 * 30 filename=Wchr(&h0414, &h043e, &h0431, &h0440, &h043E)+".txt"
Print "Filename ";filename

savefilestring("abcde",filename)
Dim As String g

loadfilestring(g,filename)
print "File content:"
Print "'";g;"'"
_wremove(@filename)
Print "Does file exist? ";exists(filename)
Sleep
 
Post Reply