-1

我正在尝试编写 COBOL 程序以顺序读取平面文件并将其写入输出文件,我一次只能读取一条记录,无法读取下一条记录我该怎么办?

这是我的代码:

PROCEDURE DIVISION.                                           
    OPEN INPUT FILEX.                                         
    PERFORM READ-PARA THRU END-PARA UNTIL END-OF-FILE = 'Y'.  
    CLOSE FILEX.                                              
    STOP RUN.                                                 
READ-PARA.                                                    
    READ FILEX                                                
      AT END                                                  
    MOVE 'Y' TO END-OF-FILE                                   
    DISPLAY OFFCODE1                                          
    DISPLAY AGCODE1                                           
    DISPLAY POLNO1                                            
    DISPLAY EFFDATE1                                          
    DISPLAY EXPDATE                                           
    DISPLAY REPCODE                                           
    DISPLAY POLHOLDER1                                        
    DISPLAY LOCATION1                                         
    GO TO END-PARA.                                           
END-PARA.   

我,已经尝试使用范围终止符,仍然无法循环'我得到 S001 ABEND 这是我的代码:

IDENTIFICATION DIVISION.             
PROGRAM-ID. SIMPLE.                  
ENVIRONMENT DIVISION.                
INPUT-OUTPUT SECTION.                
FILE-CONTROL.                        
 SELECT FILEX ASSIGN TO SYSUT1   
 FILE STATUS IS FS.              
DATA DIVISION.                       
FILE SECTION.                        
FD  FILEX.                           
01  FILEXREC.                        
02 OFFCODE1 PIC X(3).            
02 FILLER PIC X.                 
02 AGCODE1 PIC X(3).             
02 FILLER PIC X.                 
02 POLNO1 PIC X(6).              
02 FILLER PIC X.                 
02 EFFDATE1 PIC X(8).            
02 FILLER PIC X.                                        
02 EXPDATE PIC X(8).                                    
02 FILLER PIC X.                                        
02 REPCODE PIC X(1)                                     
02 FILLER PIC X.                                        
02 POLHOLDER1 PIC X(8).                                 
02 FILLER PIC X.                                        
02 LOCATION1 PIC X(9).                                  
02 FILLER PIC X(87).                                    
WORKING-STORAGE SECTION.                                    
77  FS PIC 9(2).                                            
01  WS-INDICATORS.                                          
 10  WS-EOF-IND                    PIC X(01) VALUE 'N'.  
     88  WS-END-OF-FILE                      VALUE 'Y'.  
PROCEDURE DIVISION.                                         
 OPEN INPUT FILEX.                                       
 PERFORM READ-PARA THRU END-PARA UNTIL WS-END-OF-FILE.   
 CLOSE FILEX. 
 STOP RUN.                         
READ-PARA.                            
 READ FILEX                        
   AT END                          
      MOVE 'Y' TO WS-EOF-IND.      
   DISPLAY OFFCODE1                
   DISPLAY AGCODE1                 
   DISPLAY POLNO1                  
   DISPLAY EFFDATE1                
   DISPLAY EXPDATE                 
   DISPLAY REPCODE                 
   DISPLAY POLHOLDER1              
   DISPLAY LOCATION1               
   IF WS-END-OF-FILE               
      GO TO END-PARA.            
  END-PARA.                             
 EXIT.                       

我甚至在此尝试的另一种方法仅适用于一条记录,在运行代码时再次获得 S001 ABEND。这是代码:

   IDENTIFICATION DIVISION.                  
   PROGRAM-ID. ASSIGNMENT.                   
   ENVIRONMENT DIVISION.                     
   INPUT-OUTPUT SECTION.                     
   FILE-CONTROL.                             
   SELECT FILEX ASSIGN TO SYSUT1         
   DATA DIVISION.                            
   FILE SECTION.                             
   FD  FILEX.                                
   LABEL RECORDS ARE STANDARD            
   RECORD CONTAINS  140  CHARACTERS      
   BLOCK CONTAINS  00  RECORDS.          
     01  FILEXREC.                             
      02 OFFCODE1 PIC 9(3).                 
      02 FILLER PIC X.                      
      02 AGCODE1 PIC X(3).                  
      02 FILLER PIC X.                      
      02 POLNO1 PIC X(6).                   
      02 FILLER PIC X.                                      
      02 EFFDATE1 PIC X(8).                                 
      02 FILLER PIC X.                                      
      02 EXPDATE1 PIC X(8).                                 
      02 FILLER PIC X.                                      
      02 REPCODE1 PIC X(1).                                 
      02 FILLER PIC X.                                      
      02 POLHOLDER1 PIC X(8).                               
      02 FILLER PIC X.                                      
      02 LOCATION1 PIC X(9).                                
      02 FILLER PIC X(26).                                  
  WORKING-STORAGE SECTION.                                  
   01  WS-INDICATORS.                                        
   10  WS-EOF-IND                    PIC X(01) VALUE 'N'.
    88  WS-END-OF-FILE                      VALUE 'Y'.

   01  TEMP1.                                                
    02 OFFCODE2 PIC 9(3).                                 
    02 FILLER PIC X.                                      
    02 AGCODE2 PIC X(3).                                  
    02 FILLER PIC X.                                      
    02 POLNO2 PIC X(6).                                   
    02 FILLER PIC X.                                      
    02 EFFDATE2 PIC X(8).                                 
    02 FILLER PIC X.                                      
    02 EXPDATE2 PIC X(8).                                 
    02 FILLER PIC X.                                      
    02 REPCODE2 PIC X(1).                                 
    02 FILLER PIC X.                                      
    02 POLHOLDER2 PIC X(8).                               
    02 FILLER PIC X.                                      
    02 LOCATION2 PIC X(9).                                
    02 FILLER PIC X(26).                                  
 PROCEDURE DIVISION.                                       
 OPEN INPUT FILEX.                                     
 PERFORM READ-PARA THRU END-PARA UNTIL WS-END-OF-FILE. 
CLOSE FILEX.                                          
STOP RUN.                        
READ-PARA.                           
READ FILEX                       
     INTO TEMP1                  
  AT END                         
     MOVE 'Y' TO WS-EOF-IND.     
  DISPLAY OFFCODE1               
  DISPLAY AGCODE1                
  DISPLAY POLNO1                 
  DISPLAY EFFDATE1               
  DISPLAY EXPDATE1               
  DISPLAY REPCODE1               
  DISPLAY POLHOLDER1             
  DISPLAY LOCATION1              
        IF WS-END-OF-FILE        
           GO TO END-PARA.       
  END-PARA.                      
      EXIT. 
4

2 回答 2

8

你真的应该使用你的 END- 终止符... END-PERFORM、END-IF、END-READ 等。

至于你的问题,如果我猜的话,我会说你不是只读取第一条记录,而是读取所有记录并只显示最后一条记录。您的 READ 语句有一个 AT END,一切都已完成,但它没有一个 NOT AT END 来告诉它如何处理已成功读取的记录。我通常这样编写我的 READ 语句:

READ FILE
  AT END
    SET FILE-EOF TO TRUE
  NOT AT END
    PERFORM PROCESS-RECORD
END-READ

把它包装成这样的表演,效果很好:

SET FILE-NOT-EOF TO TRUE
PERFORM UNTIL FILE-EOF
  READ FILE
    AT END
      SET FILE-EOF TO TRUE
    NOT AT END
      PERFORM PROCESS-RECORD
  END-READ
END-PERFORM

祝你好运,希望它对你有用。编写可靠的 COBOL 可能非常困难。

于 2012-07-10T12:37:41.987 回答
1

我猜你还没等到:-)

在一个地方文件是 140,但定义只有 79。

您使用文件状态,但不检查它。您的文件可能没有成功打开,如果您检查了 FS 字段,您会发现这一点(狡猾的名称,但可能是从 IBM 示例中复制的)。

当您读取并获得文件结尾时,您设置了标志,但仍像您有记录一样处理。根据“事物”,这会给您带来错误的数据,或者让您异常终止。

GO TO END-PARA 没有意义,因为 END-PARA 无论如何都会紧随其后。

希望你回去查看 NealB 提供的代码,并在几个月前让事情顺利进行......

于 2013-01-23T19:38:15.187 回答