Skip to main content
Version: 20 R7 BETA

TEXT TO DOCUMENT

TEXT TO DOCUMENT ( fileName ; text {; charSet {; breakMode}} )

ParameterTypeDescription
fileNameTextDocument name or Pathname to document
textTextText to store in the document
charSetText, IntegerName or Number of character set
breakModeIntegerProcessing 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:

ConstantTypeValueComment
Document unchangedInteger0No processing
Document with CRInteger3Line breaks are converted to CR (carriage return), the default Classic Mac OS format.
Document with CRLFInteger2Line breaks are converted to CRLF (carriage return + line feed), the default Windows format.
Document with LFInteger4Line breaks are converted to LF (line feed), the default Unix and macOS format.
Document with native formatInteger1(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 number1237
Thread safe