Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3567
  • Last Modified:

writing to a text file in the ifs with cl

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?
0
dhenderson12
Asked:
dhenderson12
  • 5
  • 4
  • 3
  • +2
3 Solutions
 
tliottaCommented:
dhenderson12:

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.

Tom
0
 
daveslaterCommented:
Hi
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                                          
                                                       
endpgm                                                  
-------------------------------------------------------------------------------

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

pgm                                        
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                  
                                           
endpgm                                    
0
 
daveslaterCommented:
you can use the copytoinpf as tom has already stated.


CPYTOIMPF FROMFILE(QTEMP/WRTTXT) TOSTMF('/slaterd/myfile')  

Dave
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ShadowProgrammerCommented:
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.

Tony.
0
 
dhenderson12Author Commented:
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.  
0
 
tliottaCommented:
dhenderson12:

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.)

Tom

Tom
0
 
dhenderson12Author Commented:
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.  
0
 
theo kouwenhovenCommented:
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


QSH CMD('echo test 123 >/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/MYMEMBER.MBR')

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


Regards.
0
 
theo kouwenhovenCommented:
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')
           
:-)

0
 
tliottaCommented:
dhenderson12:

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 )
and
   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.

Tom
0
 
theo kouwenhovenCommented:
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. :-)

Regards.

0
 
tliottaCommented:
Murph:

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.

Tom
0
 
dhenderson12Author Commented:
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.
0
 
dhenderson12Author Commented:
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.  
0
 
tliottaCommented:
dhenderson12:

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.

Tom
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now