Link to home
Start Free TrialLog in
Avatar of james henderson
james hendersonFlag for United States of America

asked on

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?
Avatar of Member_2_276102
Member_2_276102

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
SOLUTION
Avatar of daveslater
daveslater
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can use the copytoinpf as tom has already stated.


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

Dave
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.
Avatar of james henderson

ASKER

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.  
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
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.  
ASKER CERTIFIED SOLUTION
Avatar of Theo Kouwenhoven
Theo Kouwenhoven
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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')
           
:-)

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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