> Le 31 août 2017 à 00:53, Cannon Smith via 4D_Tech <4d_tech@lists.4d.com> a 
> écrit :
> 
> I’m having trouble with saving Unicode characters to a file. For example, the 
> following code saves a file to disk:
> 
> C_TEXT($tText;$tFilepath)
> 
> $tText:=“©” //Copyright symbol
> $tFilepath:=System folder(Desktop)+"test.txt"
> TEXT TO DOCUMENT($tFilepath;$tText;UTF8 text without length)
> 
> When I open the file again, it only contains a “?” character.
> 
> If I use:
> 
> TEXT TO DOCUMENT($tFilepath;$tText)
> 
> it saves correctly, but then the file has a BOM character at the beginning 
> which is wreaking havoc with other systems that aren’t expecting it.
> 
> Does anyone know what I should be doing here? Thanks.

Hi Cannon, 
in addition to others… 

Before v14 introduced the "text to document/document to text" pair of commands, 
I had these wrappers:

****

//FS_textToDocument (path_t;text_t {;charset_t)
//writes text $2 in document $1
//creates $1 if it doesn't exist
//$3 optional charSet, default utf-8
//cf FS_documentToText
C_TEXT($1)
C_TEXT($2)
C_TEXT($3)

C_BLOB($data_x)
C_LONGINT($params_l)
C_TEXT($doc_t)
C_TEXT($text_t)
C_TEXT($charSet_t)
C_TEXT($nmc_t)

If (False)
C_TEXT(FS_textToDocument ;$1)
C_TEXT(FS_textToDocument ;$2)
C_TEXT(FS_textToDocument ;$3)
End if

$nmc_t:=Current method name
$params_l:=Count parameters
Case of
: (Not(Asserted($params_l>1;$nmc_t+" 2 params expected")))
: (Not(Asserted(Length($1)>0;$nmc_t+" $1 document path empty")))
Else
$doc_t:=$1
$text_t:=$2
$charSet_t:="utf-8"
If ($params_l>2)
$charSet_t:=$3
End if
CONVERT FROM TEXT($text_t;$charSet_t;$data_x)
BLOB TO DOCUMENT($doc_t;$data_x)
End case
//_

****

//FS_documentToText (path_t {;charSet_t) -> txt
//reads $1 document and returns $0 text
//$2 optional charSet, default utf-8
//cf FS_textToDocument
C_TEXT($0)
C_TEXT($1)
C_TEXT($2)

C_BLOB($data_x)
C_TEXT($doc_t)
C_LONGINT($lineEnd_l)
C_LONGINT($params_l)
C_TEXT($charSet_t)
C_TEXT($nmc_t)

If (False)
C_TEXT(FS_documentToText ;$0)
C_TEXT(FS_documentToText ;$1)
C_TEXT(FS_documentToText ;$2)
End if

$nmc_t:=Current method name
$params_l:=Count parameters
Case of
: (Not(Asserted($params_l>0;$nmc_t+" $1 document path expected")))
: (Not(Asserted(Test path name($1)=Is a document;$nmc_t+" $1 not a document 
path")))
Else
$doc_t:=$1
$charSet_t:="utf-8"
If ($params_l>1)
$charSet_t:=$2
End if
DOCUMENT TO BLOB($doc_t;$data_x)
$0:=Convert to text($data_x;$charSet_t)
End case
//_

****

Another caveat with TEXT TO DOCUMENT is the default line breaks management, 
platform dependent (from doc: "Document with native format (Default)...Line 
breaks are converted to the native format of the operating system: CR (carriage 
return) under OS X, CRLF (carriage return + line feed) under Windows"). 
Constant 'Document unchanged' is your friend if you don't want to be surprised… 

-- 
Arnaud de Montard 

**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to