TEXT TO DOCUMENT
TEXT TO DOCUMENT ( fileName ; text {; charSet {; breakMode}} )
Parameter | Type | Description | |
---|---|---|---|
fileName | Text | → | Document name or Pathname to document |
text | Text | → | Text to store in the document |
charSet | Text, Integer | → | Name or Number of character set |
breakMode | Integer | → | Processing mode for line breaks |
Description
The TEXT TO DOCUMENT command lets you write the text directly to a disk file.
In fileName, pass the name or pathname of the file to be written. If this file does not exist, it is created. When this file already exists on the disk, its prior contents are erased, except if it is already open, in which case, its contents are locked and an error is generated. In fileName, you can pass:
- just the file name, for example "myFile.txt": in this case, the file is placed next to the structure file of the application.
- a pathname relative to the structure file of the application, for example "\\docs\\myFile.txt" on Windows or "/docs/myFile.txt" on macOS.
- an absolute pathname, for example "c:\\app\\docs\\myFile.txt" on Windows or "MacHD/docs/myFile.txt" on macOS.
If you want the user to be able to indicate the name or location of the document, use the Open document or Create document commands, as well as the Document system variable.
Note: By default, documents generated by this command do not have an extension. You must pass an extension in fileName.
In text, pass the text to write to the disk. It can be a literal constant ("my text"), or a 4D text field or variable.
In charSet, you pass the character set to be used to write the document. You can pass a string containing the standard set name (for example “ISO-8859-1” or “UTF-8”) or its MIBEnum ID (longint). For more information about the list of character sets supported by 4D, refer to the description of the CONVERT FROM TEXT command. If a Byte Order Mark (BOM) exists for the character set, 4D inserts it into the document, unless the character set used contains the suffix "-no-bom" (e.g. "UTF-8-no-bom"). If you do not specify a character set, by default 4D uses the "UTF-8" character set without BOM (except in compatibility mode, see Compatibility page).
In breakMode, you can pass a longint indicating the processing to apply to end-of-line characters before saving them in the file. You can pass one of the following constants, found in the "System Documents" theme:
Constant | Type | Value | Comment |
---|---|---|---|
Document unchanged | Integer | 0 | No processing |
Document with CR | Integer | 3 | Line breaks are converted to CR (carriage return), the default Classic Mac OS format. |
Document with CRLF | Integer | 2 | Line breaks are converted to CRLF (carriage return + line feed), the default Windows format. |
Document with LF | Integer | 4 | Line breaks are converted to LF (line feed), the default Unix and macOS format. |
Document with native format | Integer | 1 | (Default) Line breaks are converted to the native format of the operating system: LF (line feed) under macOS, CRLF (carriage return + line feed) under Windows |
By default, when you omit the breakMode parameter, line breaks are processed in native mode (1).
Compatibility Note: Compatibility options are available for EOL and BOM management. See Compatibility page.
Note: This command does not modify the OK variable. In case of failure, an error is generated that you can intercept using a method installed by the ON ERR CALL command.
Example 1
Here are some typical examples of using this command:
TEXT TO DOCUMENT("myTest.txt";"This is a test")
TEXT TO DOCUMENT("myTest.xml";"This is a test")
Example 2
Example allowing the user to indicate the location of the file to create:
$MyTextVar:="This is a test"
ON ERR CALL("IO ERROR HANDLER")
$vhDocRef :=Create document("")
// Store document with the ".txt" extension
// In this case, the .txt extension is always added to the name; it is not possible to change it
If(OK=1) // If document has been created successfully
CLOSE DOCUMENT($vhDocRef) //Closes the document
TEXT TO DOCUMENT(Document;$MyTextVar )
// We write the document
Else
// Error management
End if
See also
Document to text
System Documents
Properties
Command number | 1237 |
Thread safe | ✓ |