Code: Select all
TFile.s="filetest.txt"
If ReadFile(0,tfile,#PB_UTF8)
String.s=ReadString(0)
Debug Len(String)
SetClipboardText(string)
Else
Debug "no file"
EndIf

Code: Select all
TFile.s="filetest.txt"
If ReadFile(0,tfile,#PB_UTF8)
String.s=ReadString(0)
Debug Len(String)
SetClipboardText(string)
Else
Debug "no file"
EndIf
Code: Select all
Format = ReadStringFormat(id_file)
Thank's for the reply, if i use readstringforma() it give me 24, Ascii, if i set #pb_ascii len() retrieve the correct size, but if i write this string with writestring() command it writhe wrong string:AZJIO wrote: Sun Mar 10, 2024 12:51 pmBe sure to use ReadStringformat() to move the pointer to the end of the BOM mark.Code: Select all
Format = ReadStringFormat(id_file)
Code: Select all
WriteStringFormat(#File, #PB_UTF8)
I tried various combination, but if i get right length it write bad string in file, please test yurself, write this string to file, with this string readed from file not direct from string;
Code: Select all
Three figures stand behind it –scientists, presumably– but you cannot make out their faces.</nr>
Code: Select all
Three figures stand behind it –scientists, presumably– but you cannot make out their faces.</nr>
ee figures stand behind it –scientists, presumably– but you cannot make out their faces.</nr>
ee figures stand behind it –scientists, presumably– but you cannot make out their faces.</nr>
Code: Select all
Procedure.s ReadFileToVar(Path$)
Protected id_file, Format, Text$
id_file = ReadFile(#PB_Any, Path$)
If id_file
Format = ReadStringFormat(id_file)
Text$ = ReadString(id_file, Format | #PB_File_IgnoreEOL)
; Text$ = ReadString(id_file, #PB_UTF8 | #PB_File_IgnoreEOL)
CloseFile(id_file)
EndIf
ProcedureReturn Text$
EndProcedure
Procedure SaveVarToFile(Path$, Text$, Format = #PB_UTF8)
Protected Result = #False
Protected id_file = CreateFile(#PB_Any, Path$, Format)
If id_file
If WriteStringFormat(id_file, Format) And WriteString(id_file, Text$, Format)
Result = #True
EndIf
CloseFile(id_file)
EndIf
ProcedureReturn Result
EndProcedure
Path$ = GetTemporaryDirectory()
Text$ = ReadFileToVar(Path$ + "file.txt") ; The file should be UTF8 + BOM
Debug SaveVarToFile(Path$ + "NewFile.txt", Text$)
RunProgram(Path$ + "file.txt")
RunProgram(Path$ + "NewFile.txt")
AZJIO wrote: Sun Mar 10, 2024 1:44 pm
tried your code, same result, written string are different from original
Code: Select all
TFile.s="filetest.txt"
If ReadFile(0, tfile, #PB_UTF8)
String.s = ReadString(0)
Debug Len(String)
Debug String
SetClipboardText(string)
CloseFile(0)
If CreateFile(0, "filetest2.txt", #PB_UTF8)
WriteStringN(0, String)
CloseFile(0)
EndIf
Else
Debug "no file"
EndIf
Don't you see that in the first file you don't have a BOM mark? Your first file is wrong.le_magn wrote: Sun Mar 10, 2024 1:54 pm tried your code, same result, written string are different from original
Code: Select all
Debug SaveVarToFile(Path$ + "NewFile.txt", Text$, #PB_Ascii)
Code: Select all
EnableExplicit
Global g_Format
; https://www.purebasic.fr/english/viewtopic.php?p=478874
XIncludeFile "AutoDetectTextEncoding_Trim.pbi"
; Чтение файла в гаджет
Procedure.s OpenFileToGadget(FilePath$)
Protected length, oFile, bytes, *mem, Text$
oFile = ReadFile(#PB_Any, FilePath$)
If oFile
g_Format = ReadStringFormat(oFile)
length = Lof(oFile)
*mem = AllocateMemory(length)
If *mem
bytes = ReadData(oFile, *mem, length)
If bytes
If g_Format = #PB_Ascii
g_Format = dte::detectTextEncodingInBuffer(*mem, bytes, 0)
If g_Format = #PB_Ascii
Text$ = PeekS(*mem, bytes, #PB_Ascii)
Else
Text$ = PeekS(*mem, bytes, #PB_UTF8) ; если UTF8 без BOM
EndIf
Else
; тут не уверен, PeekS() поддерживает #PB_Unicode,
; а ReadStringFormat() может дать #PB_UTF16BE, #PB_UTF32, #PB_UTF32BE
; хотя эти форматы не популярны скорее не встретятся, и надо сделать на них игнор
Text$ = PeekS(*mem, bytes, g_Format)
EndIf
EndIf
FreeMemory(*mem)
EndIf
CloseFile(oFile)
EndIf
ProcedureReturn Text$
EndProcedure
Define FilePath$ = "C:\2023.01\1.txt"
Debug OpenFileToGadget(FilePath$)