Reading a Text File in C# Struct - How to read the Occur Clause of Cobol in Struct in C#.net RRS feed

  • Question

  • I am reading a text file which a COBOL program reads successfully. I am writing the replacement code in c#.net using Struct. Here is reference that i am using. https://www.codeproject.com/Articles/7357/How-to-copy-a-String-into-a-struct-using-C

    Here is the cobol

      05  NUM           PIC X(6)
      05  FLAG          PIC XX
      05  IP            PIC X(12)

    Here is C#.net

        public struct ORDER
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)] public string NUM;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)] public string FLAG;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 12)] public string CUSIP;

    I am able to read the variables correctly so far to an x position starting from the begining of the 1st line of the text file.

    My output is also matching; but this block of Cobol program is having OCCUR clause and I am trying to figure out; how i can add this to the ORDER struct which i have shared above and read the value as i am able to read the fields NUM, FLAG and CUSIP.

      05  AN-AREA.
          10  COST        OCCURS 5 TIMES.
              15 TYPE     PIC XX.
              15 ISO      PIC XXX.
              15 CODE     PIC X(6).
              15 IND-X
                          PIC XX.
              15 IND
                          PIC 99.
              15 AMT      PIC X(20).


    Friday, December 20, 2019 12:01 AM

All replies

  • Maybe read the next structure:

    public struct TABLE


       [MarshalAs( UnmanagedType.ByValArray, SizeConst = 5 )]

       public ORDER[] Orders;


    Friday, December 20, 2019 6:31 AM
  • Hello,


     Porting programs from language to language requires Syntax exchange. Some

    Keywords are fairly easy but then there are those that are not. COBOL keyword

    OCCURS is an array builder used in data tables. Here is the reference;



     Hope this helps :)

    Saturday, December 21, 2019 5:17 AM