资源预览内容
第1页 / 共32页
第2页 / 共32页
第3页 / 共32页
第4页 / 共32页
第5页 / 共32页
第6页 / 共32页
第7页 / 共32页
第8页 / 共32页
第9页 / 共32页
第10页 / 共32页
亲,该文档总共32页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
COBOL for Mainframe FundamentalsCh7 文件I/O处理文件处理nENVIRONMENT DIVISIONnINPUT-OUTPUT SECTION.FILE-CONTROL.nDATA DIVISIONnFILE SECTION.FDnPROCEDURE DIVISIONOPENCLOSEREADWRITEREWRITE :替换一个记录.DELETE :删除一个记录.START :定位到关系文件或索引文件中的一个记录,顺序访问从该位置开 始.ENVIRONMENT DIVISIONENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT optional file-name ASSIGN TO ddname ORGINAZATION IS SEQUENTIAL|INDEXED|RELATIVE ACCESS MODE IS SEQUENTIAL|RANDOM|DYNAMIC RECORD KEY IS identifier-1 ALTERNATE RECORD KEY IS identifier-2 FILE STATUS IS identifier-3FILE STATUS 是两位数字,标志文件的状态, 例如:00 成功 10 读到文件尾 30 I/O 错误 可以找到一个数组,包含26种状态的相信信息.例子ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INFILE ASSIGN TO FILE1 FILE STATUS IS FS1. SELECT OUTFILE ASSIGN TO FILE2 FILE STATUS IS FS2. INFILE 在FILE SECTION里定义 FILE1 是 JCL 里用到的ddname FS1 在WORKING-STORAGE SECTION里定义DATA DIVISIONDATA DIVISION. FILE SECTION. FD file-name RECORD MODE IS F|V BLOCK CONTAINS n RECORDS RECORD CONTAINS n CHARACTERS *下面定义的是该文件的记录缓冲器(Record Buffer),是内存与外部 *文件的唯一通道和桥梁。它的结构必须和文件中的单条记录吻合。 01 data-item-name. 05. 05.例子DATA DIVISION FILE SECTION. FD INFILE. 01 INREC. 05 USERID PIC X(10). 05 FILLER PIC X. 05 PWD PIC X(10). 01 OUTREC. 05 USERID PIC X(10). 05 FILLER PIC X. 05 RESULTPIC X(10). . WORKING-STORAGE SECTION. 01 WK-AREA PIC X(21). 01 FS1PIC 9(2). 01 FS2PIC 9(2).OPEN 语句n语法: OPEN INPUT|OUTPUT|I-O|EXTENDfile-name-1, file-name-2.n例子: OPEN INPUT INFILE1 INFILE2. OPEN OUTPUT OUTFILE.n什么时候用 INPUT, OUTPUT, 或I-O?CLOSE 语句n语法: CLOSE file-name-1, file-name-2, WITH LOCK,如果选用,则一个程序中同一个文件只能被打开一次.n例子: CLOSE INFILE1 INFILE2. CLOSE OUTFILE.READ 语句n语法: READ file-name-1 NEXT RECORD INTO identifier-1 AT END statement-1 NOT AT END statement-2 END-READn例子:READ INFILE AT END MOVE Y TO FLAG END-READ. READ INFILE INTO WK-AREA.WRITE 语句n语法: WRITE date-item-1 FROM identifier-1 BEFORE|AFTER ADVANCING n LINE|LINES|PAGE END-WRITEn例子:WRITE OUTREC. WRITE OUTREC FROM WK-AREA. WRITE OUTREC AFTER ADVANCING 2 LINES.REWRITE 语句n如果设备支持记录替换,则用于替换一个记录。之前必须有一次成功 读取.n语法: REWRITE date-item-1 FROM identifier-1 END-REWRITEn例子: OPEN I-O INFILE. (only when the file is opened as I-O,you can use rewrite in the following) . PERFORM UNTIL FLAG = Y READ INFILE AT END MOVE Y TO FLAG END-READ IF FS1 NOT = 00 AND FS1 NOT = 10 (should be FS1 = 00 ) THEN MOVE SPACES TO PWD REWRITE INREC IF FS1 NOT = 00 THEN DISPLAY REWRITE ERROR END-IF END-IF END-PERFORM文件排序与合并n所有的排序和合并文件都是临时文件. 这些临时文件在FILE SECTION 由SD 定义。n排序与合并的输出可以写到一个文件里,也可以只是在程序中 处理。ENVIRONMENT DIVISIONENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT sort-file ASSIGN TO assign-name.DATA DIVISIONDATA DIVISION. FILE SECTION. SD sort-file RECORD CONTAINS n1 TO n2 CHARACTERS 01 sort-rec. 05 05PROCEDURE DIVISIONnSORT 语句nMERGE 语句nRELEASE 语句 (类似与真实文件的 WRITE )nRETURN 语句 (类似于真实文件的 READ)SORT 语句SORT sort-file ON ASCENDING | DESCENDING KEY identifier-1 WITH DUPLICATE IS ORDERINPUT PROCEDURE IS proc-name-1 THRU proc-name-2USING file-name-1 OUTPUT PROCEDURE IS proc-name-3 THRU proc-name-4GIVING file-name-2 RELEASE 语句n语法: RELEASE sort-rec FROM identifierRETURN 语句n语法: RETURN sort-file RECORD INTO identifier AT END statement-1 NOT AT END statement-2 END-RETURNMERGE 语句MERGE sort-file ON ASCENDING | DESCENDING KEY identifier-1 USING file-name-1, file-name-2 OUTPUT PROCEDURE IS proc-name-3 THRU proc-name-4GIVING file-name-2 例子 1FD SalesFile. 01 SalesRec.02 FILLERPIC X(10). SD WorkFile. 01 WorkRec.02 WSalesmanNumPIC 9(5).02 FILLER PIC X(5). FD SortedSalesFile. 01 SortedSalesRec.02 SalesmanNumPIC 9(5).02 ItemTypePIC X.02 QtySoldPIC 9(4). PROCEDURE DIVISION. Begin.SORT WorkFile ON ASCENDING KEY WSalesmanNumUSING SalesFileGIVING SortedSalesFile. . 输入程序n例子 FD SalesFile. 01 SalesRec.88 EndOfSales VALUE HIGH-VALUES.02 FILLERPIC 9(5).02 FILLERPIC X.88 HatRecord VALUE H.02 FILLERPIC X(4). SD WorkFile. 01 WorkRec.02 WSalesmanNumPIC 9(5).02 FILLERPIC X(5). FD SortedSalesFile. 01 SortedSalesRec.02 SalesmanNumPIC 9(5).02 ItemTypePIC X.02 QtySoldPIC 9(4). PROCEDURE DIVISION. Begin.SORT WorkFile ON ASCENDING KEY WSalesmanNumINPUT PROCEDURE IS SelectHatSalesGIVING SortedSalesFile.SelectHatSales.OPEN INPUT SalesFileREAD SalesFileAT END SET EndOfSales TO TRUEEND-READPERFORM UNTIL EndOfSalesIF HatRecord RELEASE WorkRec FROM SalesRecEND-IFREAD SalesFileAT END SET EndOfSales TO TRUEEND-READEND-PERFORMCLOSE SalesFile.输出程序n例子 FD SalesFile. 01 SalesRec PIC X(10). SD WorkFile. 01 WorkRec.88 EndOfWorkFile VALUE HIGH-VALUES.02 WSalesmanNumPIC 9(5).02 FILLERPIC X.02 WQtySold PIC X(4). FD Sales
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号