writing to a text file in the ifs with cl

Posted on 2006-07-17
Medium Priority
Last Modified: 2013-11-18
Is there a way to write to a text file in the IFS from a CL program?  Can I create a text file from a CL program?
Question by:dhenderson12
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
LVL 27

Expert Comment

ID: 17126372

It depends on how comfortable you are with CL programming and what version of OS/400 you're running. The direct answer is that V5R4 ILE CL allows you to DCL pointer variables; along with recent release capability to pass parms by reference _and_ by value, that means that the same APIs used by C or RPG or whatever can now be called directly from ILE CL. I.e., if you can do it in any ILE language, you can also do it in CL.

But outside of that, CL has no native ability to write to IFS files.

There are a few techniques that _can_ be used.

CL can do something like CPYTOSTMF and copy records from a physical file to an IFS text file. Records can *REPLACE or *ADD. Your problem then becomes one of writing records to the physical file from CL so that they can be copied.

CL also includes a STRQSH (or just QSH) command if you've installed the Qshell option of OS/400. Qshell utilities can append to text files in a variety of ways.

CL can also call the Change User Space (QUSCHGUS) API to write strings into user spaces. A CL program could call the API any number of times to write "lines" out into the user space. When done, the CPY command can copy the user space into an IFS directory. Or the user space can be accessed just like any IFS text file simply by supplying the path directly to reach the space.

LVL 14

Assisted Solution

daveslater earned 600 total points
ID: 17129777
I had this a few years ago and came to the conclusion that a simple command / program was the easiest way.

I have called mine WRTTXT, with a CL or WRTTXT and an RPG of WRTTXTR

here is the code
             CMD        PROMPT('Write text to a file')                      
             PARM       KWD(TOFILE) TYPE(Q0001) +                            
                        Prompt( 'File')                                      
             PARM       KWD(TEXT) TYPE(*CHAR) LEN(120) MIN(1)                
 Q0001:      QUAL       TYPE(*NAME) MIN(1)                                  
             QUAL       TYPE(*NAME) DFT(*LIBL) SPCVAL((*LIBL *LIBL) +        
                          (*CURLIB *CURLIB)) PROMPT('Library')              


pgm (&filelib &text)                                    
dcl &filelib *char 20                                  
dcl &file *char 10                                      
dcl &lib *char 10                                      
dcl &text *char 120                                    
chgvar &file &filelib                                  
chgvar &lib %sst(&filelib 11 10)                        
Ovrdbf WrtTxt  ToFile(&LIB/&FILE)                      
call wrttxtr &text                                      
dltovr Wrttxt                                          

H DftActGrp(*No)                                                    
H ActGrp(*Caller)                                                  
H Option(*NODEBUGIO)                                                
FWRTTXT    o    f  120        disk                                  
D OutDs           ds           120                                  
D Byte1                          1                                  
c     *entry        plist                                          
c                   parm                    OutDS                  
c                   If        Byte1 = *Hival                        
c                   Eval      *inLr=*on                            
c                   else                                            
c                   write     WrtTxt        OutDs                  
c                   endif                                          
c                   return                                          


and an example on how to use it

DCL  &LR  *CHAR 1   X'FF'                  
crtpf qtemp/wrttxtx rcdlen(100)            
wrttxt qtemp/wrttxtx 'Hi dave '            
wrttxt qtemp/wrttxtx 'how are you?'        
wrttxt qtemp/wrttxtx 'Not too bad '        
wrttxt qtemp/wrttxtx &lr                  
LVL 14

Expert Comment

ID: 17129816
you can use the copytoinpf as tom has already stated.


Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 17131422
Maybe dhenderson12 doesn't actually need to write records to a file from within the CL as he may already have records in a file that he wants to copy to a text file - which Tom has already provided the answer to.

If dhenderson12 wants to write to a file (in the CL) which can then copied to the IFS, maybe he can give a little more info on his exact requirements - ie. where will he get the text from, what is the limit to the record length etc.


Author Comment

ID: 17131515
Thank you all for your replies.  To answer the question as to use, it is for troubleshooting purposes.  Typically I will add print statements to programs that I write so that I can see where a problem is occuring, something like this:

println("before procedure call");
**call the procedure here**
println("back from procedure call");

I am trying to do the same thing with the CL programs that I write.  So I am generating text and appending that text to a file.  
LVL 27

Expert Comment

ID: 17132695

Is there a reason you want to log to a text file? That is, what difference does it make where info is logged? E.g., for general logging on an AS/400 (particularly in CL) I prefer creating a message queue and sending messages to it. The system automatically tracks timestamps and what job sent the messages as well as which program, etc.

Granted, a text file can have an advantage when you want to view it from a PC or in other circumstances. But once the log is created, no matter what its object type, it can almost always be converted to a different object type by some standardized programming. (Even CL programming.)



Author Comment

ID: 17138012
Actually, it doesn't matter if the info is logged to a log file or to a text file.  I just thought there might be a relatively "simple" way to log it to a text file since that's what I normally do with other programs like java.  
LVL 16

Accepted Solution

theo kouwenhoven earned 800 total points
ID: 17139246
Yep the most simple way to write from CL is the Qshell

QSH CMD('echo test 123 >zzz')

In this case  the text "test 123" is written to file zzz evry time you use this file zzz will be replaced

QSH CMD('echo test 456 >>zzz')

The >> is as in DOS an append in stead of a replace


This example writes the text 'Test 123' to file MYFILE on the AS/400 filesystem so look in library MYLIB.

the command string of the QSH command can be buildup inside your CL

LVL 16

Expert Comment

by:theo kouwenhoven
ID: 17139284
Hi again...
 in case of Daves example the QShell option will look like this:

QSH CMD('echo Hi dave > wrttxtx')
QSH CMD('echo how are you? >> wrttxtx')
QSH CMD('echo Not too bad >> wrttxtx')

LVL 27

Assisted Solution

tliotta earned 600 total points
ID: 17141633

Murph supplied a simple example that uses Qshell utilities to write lines to a text file. Other commands and utilities can set your current directory or create text files with particular attributes or make the environment however it's needed.

To expand the idea of a message queue as a "log" receiver --

   crtmsgq  mylib/mylogmsgq

...would create a message queue named MYLOGMSGQ in library MYLIB. Messages would be sent to it from a CL program with:

   sndmsg  'before procedure call'  tomsgq( mylib/mylogmsgq )
   sndmsg  'back from procedure call'  tomsgq( mylib/mylogmsgq )

When you want to review the messages:

   dspmsg  mylib/mylogmsgq

The principle to keep in mind is that CL is a "control language". It isn't technically designed to be an actual programming language. But it's so powerful in general, including being a compiled language and allowing bound procedures, etc., that many of us tend to use it for programming. Most especially, it is guaranteed to be on every AS/400 and it's easily extensible. Learning good CL can pay off at unexpected times.

LVL 16

Expert Comment

by:theo kouwenhoven
ID: 17142504
Hi Tom, dhenderson12,

Yes of cours you are right about MSGQ's etc, but I think the question was to write it to the IFS.
snd not to a MSGQ or a text file in QSYS.LIB

and beside writing to textfiles and copy them to the IFS, I think the QSH is the only way to do it
(Maybe there is some API, I don't know)

I used this QSH option to write EXCEL transferfiles to build some weird EXCEL interface. :-)


LVL 27

Expert Comment

ID: 17142669

That's why I suggested QSH in the first reply. As the thread developed, I wanted to be sure that the OP knew that there were alternatives. Minor note --  exiting to QSH isn't much different from exiting to an RPG or C program -- it still isn't "CL writing to a text file" and it requires installing the Qshell feature as well.

Along those lines, at V5R2 and above, it might even be PASE rather than Qshell. PASE is available for V5R1 if the $100 license was paid.


Author Comment

ID: 17159998
Thanks to you all for helping me.  I am increasing the point value and splitting between murph and tliotta.  You have given me some great options.  I will research PASE a little more.

Author Comment

ID: 17160075
I apologize to Dave Slater for not awarding points to the excellent answer he provided.  Dave, I will contact the admin and get points awarded to you as well.  
LVL 27

Expert Comment

ID: 17171131

I wouldn't be very concerned about PASE just yet. Qshell is a better starting place since it's more established and more familiar to AS/400 programmers. If a shell utility does what you need, I'd get it working in Qshell and later investigate whether another shell is a better choice.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question