Stream I/O
1. LIST Directed I/O
1.1 Advantages and Disadvantages
- Advantages of LIST directed I/O
- It is easy to code and is a useful debugging tool
- Disadvantages of LIST directed I/O
- In GET LIST the data needs to be separated by space or comma, and therefore more space is required to represent the input data.
- In PUT LIST, data is presented at predefined tab position, and so we cannot format it to provide a more meaningful and pleasing report.
1.2 GET LIST
- For this type of input each data value in the stream must separated either by a blank or a comma.
- A Null field is indicated by the first non blank character or by two commas separated by an arbitrary number of blanks.
- e.g. If the statement is GET LIST (AB,C,D,E); the values keyed could be 100 90 80 70 90.
1.3 PUT LIST
- Values of the data list items are converted to character representations and transmitted to the data stream.
- A blank separates successive data values transmitted.
e.g. Lets consider the following:
PUT LIST (50,’ABC’,123,127);
PUT LIST(23,86,87);
Tab position 1 25 49 73 97 121
1st Line 50 ABC 123 127 23
2nd Line 86 87
- Use SKIP(n) option to move the line feed to the nth line after the current line.
e.g. PUT SKIP LIST(123);
- Use PAGE option cuases the data to be printed on the top of a new page.
e.g. PUT PAGE LIST(‘IGSI’);
- Use LINE option to print the data on a specific line number.
e.g. PUT LINE(10) LIST (A,B);
2. EDIT Directed I/O
2.1 Advantages
- No Blanks are needed to separate the data items in the input stream
- No punctuation marks are needed to indicate the type of input data.
Example: If we have to supply the following stream input:
‘ 435928 ’ ‘DAVID P. GOLDSMITH’ 10.55 41.3 103.21
Only 37 positions are needed if edit directed input as compared to 47 positions for list directed input.
2.2 STRING Option
l Instead of getting data form an external device(a file) the data can also be read from a character string. e.g.
GET STRING(NAME) EDIT(FIRST,MIDDLE,LAST)(A(15),A(1),A(20));
l It can be used to effect character to arithmetic and arithmetic to character string conversion.
DCL EMP# CHAR(7);
DCL HOURS FIXED DEC(3,1);
DCL NAME CHAR(20);
DCL RATE FIXED DEC(4,2);
DCL RECORD CHAR(80);
PUT STRING (RECORD) EDIT(NAME,EMP#,HOURS*RATE)(A(20),A(7),F(7,2));
l Sometimes records of different formats appear in the same file where each record contains a code that identifies the particular format;
GET EDIT (INFO)(A(80));
SELECT(SUBSTR(INFO,1,1));
WHEN(‘ 1’ )
GET STRING(INFO)
EDIT(CUST#,DATE,AMT)(X(1),A(6),X(3),A(6),X(1),F(7,2));
WHEN(‘ 2’ )
GET STRING(INFO) EDIT(CUST#,AMT,INVOICE#)(X(1),A(6),X(1),F(7,2),X(1),A(8));
OTHERWISE
CALL ERROR_IN_INPUT;
END;
2.3 Conversion of external data to internal data format
Arithmetic data items have the format F (fixed point) or E (floating point). Following data types may be input or output using the F, E or P format items.
- FIXED BINARY
- FIXED DECIMAL
- FLOAT BINARY
- FLOAT DECIMAL
e.g. DCL HOURS FIXED DEC(3);
GET EDIT(HOURS) (A(3)); /* provided HOURS contain only the digits 0 through 9 */
2.4 Control Items
l COLUMN:
l LINE:
l PAGE:
l SKIP:
3 DATA Directed I/O
3.1 Data directed input:
l Each item in the input is in the form of an assignment statement that specifies both the value and the variable to which it is to be assigned.
l Statements are separated by blanks or commas.
l Semicolon ends each group of items to be accessed by a single GET statement.
e.g. A= 12.3,B=57,C=‘ABCDEF’,D=‘ 1110 ’ B;
GET DATA(A,B,C,D);
l Maximum number of elements permitted in a list = 320;
l Data list may include the name that do not appear in the list, but the vice versa is not true.
l If the data list is an array, subscripted references to that array may appear in the stream. e.g.
DCL TABEL(50) FIXED DEC(5,2);
GET DATA (TABLE);
Input stream could be: TABLE(3)=7.95, TABLE(4)=8.43,TABLE(7) = 50;
3.2 DATA DIRECTED OUTPUT:
l Each data item is placed in the stream in the form of assignment statements.
l Last item output is followed by a semicolon.
l Leading zeros of arithmetic data are suppressed.
e.g. DCL A FIXED DEC(5) INIT(0);
DCL B FIXED DEC(5) INIT(0);
DCL C FIXED BIN(15) INIT(175);
PUT DATA(A,B,C);
Tab position 1 25 49
Output would look like: A=0 B= 0 C=175;