Channel Command Component
See Also: File I/O Commands
Purpose
To enable concurrent opening of more than one output destination or input source, and to enable switching from one channel to another as an application executes.
Syntax
Channel {channelNumber}
Where {channelNumber} is the number of the channel.
Values
- 0 - 9
What It Does
The channel command component is an option in the following commands:
- append_output
- close_input
- close_output
- direct_input
- direct_output
- read
- read_block
- readln
- write
- Writeln
Use the New_Seq_Channel function to obtain a new channel, and the Seq_Release_Channel function to release a channel that is no longer used. See the section Sequential File Input and Output to read more about file input and output (I/O) in DataFlex.
It is recommended to always use channels when doing any file I/O to make your code more reusable.
Example
Use seq_chnl.pkg
Procedure WriteTwoFiles
Integer iChOut1 iChOut2
String sFileOut1 sFileOut2
// specify the file name of the first output file
Move "c:\temp\OutFile1.txt" to sFileOut1
// specify the file name of the second output file
Move "c:\temp\OutFile2.txt" to sFileOut2
// obtain 2 open channels
Move (Seq_New_Channel()) to iChOut1
Move (Seq_New_Channel()) to iChOut2
Direct_output channel iChOut1 sFileOut1 // open output channel
Direct_output channel iChOut2 sFileOut2 // open output channel
Writeln channel iChOut1 "This is line 1 of output file 1"
Writeln channel iChOut2 "This is line 1 of output file 2"
Writeln channel iChOut1 "This is line 2 of output file 1"
Writeln channel iChOut2 "This is line 2 of output file 2"
Writeln channel iChOut1 "This is line 3 of output file 1"
Writeln channel iChOut2 "This is line 3 of output file 2"
Close_Output channel iChOut1 // close output channel
Close_Output channel iChOut2 // close output channel
// release both channels for reuse
Send Seq_Release_Channel iChOut1
Send Seq_Release_Channel iChOut2
End_Procedure
This example writes 3 lines to 2 different output files. Using the channel command component, the code is able to alternate writing between the 2 files.
Notes
Whenever a channel is specified and subsequent lines of code do any I/O without specifying a channel, the last specified channel will be used. This is a common cause of coding errors.
Example
Writeln channel iChOut1 "This is line 1 of output file 1"
Writeln "This is line 1 of output file 2"
If the above change was made to the example in the What It Does section, both lines would be written to the file open in channel iChOut1.