Solved

writing to a text file in the ifs with cl

Posted on 2006-07-17
16
2,722 Views
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?
0
Comment
Question by:dhenderson12
  • 5
  • 4
  • 3
  • +2
16 Comments
 
LVL 27

Expert Comment

by:tliotta
ID: 17126372
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
 
LVL 14

Assisted Solution

by:daveslater
daveslater earned 150 total points
ID: 17129777
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
 
LVL 14

Expert Comment

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


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

Dave
0
 
LVL 9

Expert Comment

by:ShadowProgrammer
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.

Tony.
0
 

Author Comment

by:dhenderson12
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.  
0
 
LVL 27

Expert Comment

by:tliotta
ID: 17132695
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
 

Author Comment

by:dhenderson12
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.  
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 16

Accepted Solution

by:
theo kouwenhoven earned 200 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


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
 
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')
           
:-)

0
 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 150 total points
ID: 17141633
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
 
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. :-)

Regards.

0
 
LVL 27

Expert Comment

by:tliotta
ID: 17142669
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
 

Author Comment

by:dhenderson12
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.
0
 

Author Comment

by:dhenderson12
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.  
0
 
LVL 27

Expert Comment

by:tliotta
ID: 17171131
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now