process dynam xref(short) Identification division. Program-id. peektop2. * Copyright (c) 2011 by Steven H. Comstock Ver1 * COBOL program to dynamically obtain a file name and * display the first two lines in the file Environment division. Data division. Working-storage section. 01 File-stuff. 02 in-file pic s9(9) binary value 0. 02 rec-len pic s9(9) binary value 0. 02 norecs pic s9(9) binary value 2. 02 read-mode pic x(15) value z'rb,type=record'. 01 in-data pic x(1000). 01 messages. 02 Entry-message pic x(20) value 'In peektop2 - Ver1'. 02 pic x(2) value x'1500'. 02 Exit-message pic x(20) value 'Leaving peektop2'. 02 pic x(2) value x'1500'. 02 msg-open-fail. 03 pic x value x'15'. 03 pic x(52) value 'Cannot open for reading.'. 03 pic x(2) value x'1500'. 02 msg-open-good. 03 pic x value x'15'. 03 pic x(52) value 'File opened successfully.'. 03 pic x(2) value x'1500'. 02 Line-1. 03 pic x(27) value 'Enter the name of the file '. 03 pic x(11) value 'to display.'. 03 pic x(2) value x'1500'. 02 Line-2. 03 pic x(52) value 'For HFS files, specify the full path name.'. 03 pic x(2) value x'1500'. 02 Line-3. 03 pic x(52) value 'For z/OS files, specify the name with 2 '. 03 pic x(2) value x'1500'. 02 Line-4. 03 pic x(52) value 'leading slashes, a single quote, the fully qualified'.d' 03 pic x(2) value x'1500'. 02 Line-5. 03 pic x(52) value 'name, then a single closing quote, for example:'. 03 pic x(2) value x'1500'. 02 Line-6. 03 pic x(52) value ' //''stnt329.train.library(inputcs)'''. 03 pic x(2) value x'1500'. 02 Line-7. 03 pic x value x'15'. 03 pic x(19) value z'Resulting name: %s'. 02 Bytes-line. 03 pic x(12) value 'Bytes read ='. 03 no-bytes pic zz9. 03 pic x(2) value x'1500'. 02 Data-line. 03 pic x(9) value 'Data = %s'. 03 pic x(2) value x'1500'. 01 line-in pic x(120) value spaces. 01 in-pat pic x(5) value z'%s'. procedure division. mainline. call 'printf' using entry-message call 'printf' using Line-1 call 'printf' using Line-2 call 'printf' using Line-3 call 'printf' using Line-4 call 'printf' using Line-5 call 'printf' using Line-6 move all x'00' to line-in call 'scanf' using in-pat, line-in call 'printf' using Line-7, line-in call 'fopen' using line-in, read-mode returning in-file if in-file > 0 call 'printf' using msg-open-good perform read-rec norecs times call 'fclose' using by value in-file else call 'printf' using msg-open-fail end-if call 'printf' using exit-message goback. read-rec. move all x'00' to in-data call 'fread' using in-data, by value 1, length of in-data, in-file returning rec-len if rec-len = 0 continue else move rec-len to no-bytes call 'printf' using Bytes-line call 'printf' using Data-line, in-data end-if .