ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Writing records on a flat file

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

  • Writing records on a flat file

    Hi Every1,

    I have a flat file and a physical file.We need to write all the records from PF to the flat file but length shud be variable.
    For ex -
    Physical file -
    Name Designation Age
    AAAA XYZZ 28
    BB CC 9

    The records in the flat file should come like this -

    AAAAXYZZ28
    BBCC9

    Please let me know if its even possible.

    Thanks in advance.

  • #2
    Re: Writing records on a flat file

    Your PF would have to be setup as one big character field. Then you could do the following:

    newrec = %trim(name) + %trim(designation) + %trim(age);

    If your age is a numeric field, you may need to substitute %trim(%char(age))

    Comment


    • #3
      Re: Writing records on a flat file

      Originally posted by Poch@hont@s
      We need to write all the records from PF to the flat file but length shud be variable.
      .
      .
      .
      Please let me know if its even possible.
      If you can find any parameter of the CRTPF command that indicates "variable length", then it's possible. All parameters can be seen prompting the command and then pressing F9='All parameters'. I've never seen a capability for creating a physical 'flat' file that has variable-length records.

      In general, if you need to write variable-length records to a 'flat file', you should create a text streamfile in a directory under the /root file system, and write your records to the streamfile.
      Tom

      There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

      Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

      Comment


      • #4
        Re: Writing records on a flat file

        You could do all of the concatenation in an sql cursor.

        Code:
        exec sql
             declare mycurs cursor for
             select char_field1 || trim(digits(num_field2)) || char_fieldN
             from mylib/myfile;
        Then you could read from the cursor and write out to the IFS flat file. The SQL might get a little hairy, but at least the programming would be Q&E(Quick and Easy).

        Comment


        • #5
          Re: Writing records on a flat file

          What about?

          CPYF FROMFILE(physicalFile) TOFILE(flatFile) FMTOPT(*nochk)

          Comment


          • #6
            Re: Writing records on a flat file

            @rborrell:

            Your method does not remove the spaces...

            GLS
            The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

            Comment


            • #7
              Re: Writing records on a flat file

              Thanks for all the help..
              one quick ques though...
              Is vice versa possible?writing records from a variable length file to a physical file?

              As far as i cud think..it seems to be impossible...

              Comment


              • #8
                Re: Writing records on a flat file

                Originally posted by Poch@hont@s View Post
                Is vice versa possible?writing records from a variable length file to a physical file?
                To a physical file, yes. The physical file probably best would have a variable-length column in order to receive variable-length lines. A variable-length column simply requires a description registered with the database that it be variable-length; and if it's described, it's not considered to be a 'flat file'.

                A variable-length column isn't an absolute requirement. It could be fixed-length if the length of the column can hold the longest line (or if lines are allowed to continue to the next record.)

                Some of the discussion comes down to the meaning of 'flat file'. There are multiple possible definitions. It's possible that it means any file that has no hierarchical structure and possibly also one with no relational structure.

                If you want a discussion about it, you probably should explain what you mean by 'flat file' in your original question. It could also help if you explained what a 'variable' length means to you and what purpose it serves.
                Tom

                There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

                Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

                Comment


                • #9
                  Re: Writing records on a flat file

                  It's possible, but there would need to be a character defined as a field separator.

                  I've used commas, tabs, carets, and pipes to delineate where one field ends and the next begins.

                  .CSV files are very common, but you have to deal with the possibility of embedded commas.

                  Comment

                  Working...
                  X