COBOL程序按顺序读取平面文件并将其写入输出文件,无法在所有循环中读取数据无限

时间:2012-07-10 11:20:01

标签: cobol

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

这是我的代码:

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. 

2 个答案:

答案 0 :(得分: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可能非常困难。

答案 1 :(得分:1)

我猜你还没有等待: - )

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

您使用文件状态,但不检查它。您的文件可能无法成功打开,如果您检查了FS字段(狡猾的名称,但可能复制了IBM示例),您可能会发现该文件。

当你阅读并获得文件结束时,你设置了标志,但仍然处理好像你有一个记录。这取决于“事物”,这会给你带来错误的数据,或者让你出现异常。

GO TO END-PARA没有意义,因为无论如何END-PARA会立即发生。

希望你回过头来看看NealB提供的代码并在几个月前搞定了......