Home > AS/400 Tips > iSeries administrator tips > Writing to a file from CL -- revisited
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES ADMINISTRATOR TIPS

Writing to a file from CL -- revisited


Steve Riedmueller
06.15.2005
Rating: -4.00- (out of 5) Hall of fame tip of the month winner


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


You Can View User Feedback To This Tip

In a previous tip, Who said you can't write to file from CL? Clive Griffiths published his WRTREC command/CL combination which allows one to write a record to a physical file from a CL program.

I have been using this code for a few years and it is very useful. The implementation seemed very complex for a simple idea. I came up with an alternative that runs five times faster according to my primitive testing. My implementation uses the QSH "echo" command, redirecting output to the physical file in "append" mode.

  
/*********************************************************/ 
/* PGM   - WRTRECCPP                                                 */ 
/* AUTHOR - CLIVE GRIFFITHS                                          */

/* MODIFIED BY - STEVE RIEDMUELLER 06/13/05                          */
/* DESC  - WRITE A RECORD TO SEPCIFIED FILE                          */ 
/* PARMS - &RECORD 100 CHARACTER - DATA TO BE WRITTEN TO FILE        */ 
/*       - &TOLIB 10 CHARACTER   - LIBRARY NAME OF FILE TO BE WRITTEN*/ 
/*       - &TOFILE 10 CHARACTER  - NAME OF FILE TO BE WRITTEN TO     */ 
/*********************************************************************/ 
                                                                        
PGM (&RECORD &TOFILE &TOLIB)                                            
 DCL &RECORD *CHAR 100                                                  
 DCL &TOFILE *CHAR 10                                                   
 DCL &TOLIB  *CHAR 10                                                   
 DCL &TARGET *CHAR 100                                                  
 DCL &CMD    *CHAR 100                                                  

 CHGVAR     VAR(&TARGET) VALUE('/qsys.lib/' *TCAT &TOLIB +  
             *TCAT '.lib/' *TCAT &TOFILE *TCAT +           
             '.file/' *TCAT &TOFILE *TCAT '.mbr')          
 CHGVAR     VAR(&CMD) VALUE('echo "' *TCAT &RECORD *TCAT +  
             '" >> ' *CAT &TARGET)                         
 QSH        CMD(&CMD)                                       
ENDPGM                                                     
  
  

USER FEEDBACK TO THIS TIP

  • I would like to add a correction to this tip.
    1. Qshell should be installed (ss1 33)
    2. Max rec length 512

      
      
      Code: CMD        PROMPT('Add a record to a file')               
                                                              
    PARM       KWD(DATA) TYPE(*CHAR) LEN(512) MIN(1) +        
                 CHOICE('Enter data to be written') +         
                 PROMPT(DATA)                                 
                                                              
    PARM       KWD(TOLIB) TYPE(*NAME) LEN(10) MIN(1) +        
                 CHOICE('Library name') PROMPT(TOLIB)         
                                                              
    PARM       KWD(TOFILE) TYPE(*NAME) LEN(10) MIN(1) +       
                 CHOICE('Target file name') PROMPT(TOFILE)    
                                                              
    PARM       KWD(TOMEMBER) TYPE(*NAME) LEN(10) MIN(1) +     
                 CHOICE('Member name') PROMPT(TOMEMBER)       
    
    -------------------------------------------------------------------------------------------------------
    
    PGM        PARM(&RECORD &LIB &FILE &MEMBER)               
    DCL        VAR(&RECORD) TYPE(*CHAR) LEN(512)              
    DCL        VAR(&LIB) TYPE(*CHAR) LEN(10)                  
    DCL        VAR(&FILE) TYPE(*CHAR) LEN(10)                 
    DCL        VAR(&MEMBER) TYPE(*CHAR) LEN(10)               
    DCL        VAR(&DATA) TYPE(*CHAR) LEN(100) VALUE(' ')     
    ADDENVVAR  ENVVAR(TM) VALUE(&RECORD) REPLACE(*YES)        
    CHGVAR     VAR(&DATA) VALUE('echo $TM >>' *BCAT +         
                 '/qsys.lib/' *TCAT &LIB *TCAT '.lib/' +      
                 *TCAT &FILE *TCAT '.file/' *TCAT &MEMBER +   
                 *TCAT '.mbr/')                               
    QSH        CMD(&DATA)                                     
    RMVENVVAR  ENVVAR(TM)                                     
    MONMSG     MSGID(CPFA981)                                 
    ENDPGM   
    — Avi Hecht
  • CL has been able to do a few forms of writing to files for many years. CL can call the Sort (QLGSORT) API and pass records into the API through the input buffer -- the API can send those to output files which can be copied/appended to other files. CL can call QM queries to effect SQL INSERTs to files.

    But in Version 5 of OS/400, CL got even closer through the QShell DB2 utility (see example #1). And with a little research, you can see that the DB2 utility is a symbolic link to program QZDFMDB2 (see example #2).

    Still not perfect, but pretty workable.

    #1
    QSH CMD('db2 "INSERT INTO QIWS.QCUSTCDT (CUSNUM, LSTNAM, INIT) VALUES(876543, ''Liotta'', ''T'')"')
    #2
    CALL QZDFMDB2 ('INSERT INTO TOML.QCUSTCDT (CUSNUM, LSTNAM, INIT) VALUES(876543, ''Liotta'', ''T'') ')

    — Tom Liotta

==================================
MORE INFORMATION ON THIS TOPIC
==================================

The Best Web Links: Tips, tutorials and more.

Visit the ITKnowledge Exchange and get answers to your systems management questions fast.

Read this Search400.com Featured Topic: Take control of your iSeries.

Ask the Experts yourself: Our systems management gurus are waiting to answer your technical questions.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
iSeries administrator tips
Linux for IBM i: Introducing Linux to IBM i people
Application modernization for the iSeries: Why bother?
Application modernization in the i world
Checking on System i disk space requires creating a new command: XRTVSYSSTS
High availability planning and testing for iSeries: Notes, gloats, and moats
Viewing netstat information
Step-by-step PTF download determination
Preventing adopted special privileges on i5/OS
Display documents on your iSeries -- quick and easy
System i modularization: How-to tips -- part 4

iSeries CL programming
Eight steps for creating program documentation using AS/400 utilities
DAYSPAST CLLE program for AS/400: Compares object creation date with today's date
Advanced Job Scheduler help
How do I retrieve the source for an output queue description to put in to a CL program?
Top 10 programmer tips YTD
Ways to put QSHELL to work in your day-to-day programming life
Code examples to determine future end of month based on any given date
A program to access stored text files
Slow system performance
Changing the font size

Performance
Top System i admin tips for 2006
Catholic Charities keeps track of homeless with iSeries
i5 error messages: What you need to know
IBM races for clock speed
System shutting down after cleanup
What is the relationship between QZDAOSINIT jobs and QDBSRVnn tasks?
Performance degradation
Top admin tips of 2005
Tuning the auto-tuner
Readers respond to Fast400 settlement

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts