ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

TEMPLATE Keyword on File Definitions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TEMPLATE Keyword on File Definitions

    Hello all,
    I have a bit of code that is bugging me.

    If I have the following code:

    Code:
    DCL-F file1 DISK(*EXT) TEMPLATE; // file1 contains field field1
    DCL-S message LIKE(field1);
    When I try to compile, I get two *RNF7030 errors saying "The name or indicator field1 is not defined. " and "The name or indicator message is not defined. "
    However, if I use this:

    Code:
    DCL-DS ds1 EXTNAME('FILE1') TEMPLATE END-DS; // file1 contains field field1
    DCL-S message LIKE(field1);
    or:

    Code:
    DCL-F file1 DISK(*EXT); // file1 contains field field1
    DCL-S message LIKE(field1);
    the code compiles without error.

    Can anyone explain to me why I can't define variables from a file definition template and my first example fails to compile?

    Thanks!

  • #2
    When you define a global file, the compiler normally generates I specs for the fields in the file, so that you can say do a READ to the file without specifying a data structure in the result field. The definitions for the fields in the file come from those I specs.

    There are some files that don't get I specs generated:
    • Files defined with the TEMPLATE keyword.
    • Files defined with the QUALIFIED keyword, which means the record formats are qualified, MYFILE.MYFMT.
    • Files defined with the LIKEFILE keyword, which means that the record formats are automatically qualified.
    • Files defined in subprocedures.
    If the compiler doesn't generate I specs for the file, then the fields in the file aren't available to the compiler unless you also define an externally-described data structure.

    Comment


    • #3
      Code:
      Dcl-DS T_File1 Extname('FILE1') Qualified template;
      End-DS T_File1;
      
      Dcl-S Message Like(T_File1.Field1);

      Comment


      • #4
        Thanks Barbara Morris for the explanation, and Rocky, that's what I ended up doing.

        I have another question regarding the use of the Template keyword. I'm working on another program right now, and I'd like to pass a file in as a parameter, so I have something like this:

        Code:
              DCL-F inFileTemp DISK(1000) USAGE(*INPUT) TEMPLATE;
              DCL-PR MyProcedure;
                 inFile LIKEFILE(inFileTemp);
               END-PR;
        
               DCL-PI DigSO021;
                 inFile LIKEFILE(inFileTemp);
               END-PI;
        However, my program also needs to know the file name. Unfortunately, I can't use a file information data structure (INFDS) on a file passed as a parameter (that I know of). Do I just need to have the calling program pass the filename in as a parameter, or are there any other options?

        Thanks again!

        Comment


        • #5
          You're right that you can't define an INFDS for the file parameter within the procedure. But you could also pass the INFDS as an additional parameter passed by reference. The INFDS parameter would get updated by any I/O operations to the file done within the procedure.

          Comment


          • #6
            Thanks again Barbara Morris! For my current project, all I need is the file name, and that wouldn't change with any I/O operation, so there doesn't appear to be any real advantage to passing the INFDS as a parameter as opposed to just a CHAR. However, knowing that an INFDSs passed by reference get updated is super helpful, and I will definitely make use of that in the future.

            Comment

            Working...
            X