See Also: File I/O Commands, Append_Output, Direct_Output, ReadLn, Set_Channel_Position, Write, Write_Hex, Channel command component, Sequential File I/O
To Write the values of one or more variables to a sequential file, device, or text field as a line of data.
WriteLn [Channel ChannelNum] [value ... value]
Channel ChannelNum
Output Channel to Write data to. If omitted, the channel will be that specified in the last executed Append_Output, Direct_Output, output, Write, or WriteLn command in which channel was specified or implied.
Value
Optional, may be of any type. Additional values may be on the command line, separated by spaces, without limit. If no argument is passed, WriteLn outputs only an end-of-line.
WriteLn Writes data from values, if any, to the output data file or device named in the last executed Direct_Output command, and then outputs an end of line character. WriteLn Writes to the output file or device at whatever point the preceding WriteLn or Write command left off.
Direct_Output "inventry.txt"
Number nTtankLevel
Move 1250 to nTtankLevel
WriteLn nTankLevel
In this example, the value 1250 is output to Output File INVENTRY.TXT. The point in a file at which writing after an Append_Output command is done can also be changed by use of the Set_Channel_Position command. WriteLn-ing before the end of a file overwrites existing data. There is no insert mode for sequential output.
WriteLn does not add any delimiters, embedders, or other formatting characters to the output, not even spaces between variables. You must provide any such characters in arguments to the WriteLn command. Suppose, for example, that you wished to output two fields from Database File car, and that the field contents needed to be embedded in quotation marks (") and each field separated from the other by a comma. This command would do so, adding an end of line character at the end:
WriteLn Channel 2 '"' Car.Make '","' Car.Model '"'
Here, single quotation marks (') are used to make literal constants out of the combinations of embedding and delimiting characters required in the output. If the value of Car.Make were CADILLAC, and of Car.Model, EL DORADO, the output would be:
"CADILLAC","EL DORADO"
In long and numerous commands, and with data which itself contains characters used as embedders, you can Write, and read, your commands more easily if you place the required characters into DataFlex variables, and use the variables to insert the required characters into the output.
String sTerm sInter
Move '"' to sTerm
Move '","' to sInter
WriteLn sTerm car.make sInter Car.Model sTerm
This example would output exactly the same string as the example above.
It is unnecessary ever to use the Channel feature unless at some point you need more than one output destination, or more than one input source, at the same time in your program.
Since DataFlex 20.0 strings are UTF-8. Since WriteLn does not translate string content, it writes a UTF-8 encoded string to the file. If the exported file is expected to be OEM, convert the data using Utf8ToOem or Utf8ToAnsi.
When value is an expression that contains multiple data types concatenated using String concatenation, non-String data types must be cast as String.
For example:
Integer iCount
Move 5 to iCount
WriteLn ("The file contains "+ iCount + " lines")
will result in 0 being written out.
Integer iCount
Move 5 to iCount
WriteLn ("The file contains "+ (String(iCount)) + " lines")
will result in "The file contains 5 lines" being written out.
You must supply in the arguments to WriteLn all delimiters, embedders, and other formatting characters required by whatever program is to use the output. Refer to the documentation for the target program for its requirements, and how to adapt the requirements of your particular data to those requirements. Common data formats which you can emulate with WriteLn are ASCII, sequential, comma-delimited, mailmerge, line-delimited, printable, source, and text.
When used with no argument, WriteLn outputs only an end-of-line. If you find it convenient, you may intermingle Write and WriteLn commands.
// These commands:
Write sString
Write nNumber
Write dDate
WriteLn
// are exactly the same as this command:
WriteLn sString nNumber dDate
// and the same as these commands:
Write sString
Write nNumber
WriteLn dDate
The output file or device must be opened with the Direct_Output or Append_Output command prior to using WriteLn.